Skip to content

Instantly share code, notes, and snippets.

@dannymcc
Created January 7, 2012 14:52
Show Gist options
  • Select an option

  • Save dannymcc/1574942 to your computer and use it in GitHub Desktop.

Select an option

Save dannymcc/1574942 to your computer and use it in GitHub Desktop.

Revisions

  1. Danny McClelland created this gist Jan 7, 2012.
    42 changes: 42 additions & 0 deletions incoming_calls.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    desc "Import incoming calls"
    task :fetch_incomingcalls => :environment do

    # Logs into manage.phoneprovider.co.uk and retrieved list of incoming calls.
    require 'rubygems'
    require 'mechanize'
    require 'logger'

    # Create a new mechanize object
    agent = Mechanize.new { |a| a.log = Logger.new(STDERR) }

    # Load the phoneprovider website
    page = agent.get("https://manage.phoneprovider.co.uk/login")

    # Select the first form
    form = agent.page.forms.first
    form.username = 'username'
    form.password = 'password'

    # Submit the form
    page = form.submit form.buttons.first

    # Click on link called Call Logs
    page = agent.page.link_with(:text => "Call Logs").click

    # Click on link called Incoming Calls
    page = agent.page.link_with(:text => "Incoming Calls").click

    # Prints out table rows
    # puts ('table > tr')

    # Print out the body as a test
    # puts page.body

    # StackOverflow Snippet
    Nokogiri::HTML(html).xpath("//table/tbody/tr[not(@class)]").collect do |row|
    timestamp = row.at("td[1]").text.strip
    source = row.at("td[2]").text.strip
    destination = row.at("td[3]").text.strip
    duration = row.at("td[4]").text.strip
    {:timestamp => timestamp, :source => source, :destination => destination, :duration => duration}
    end