Created
April 5, 2012 17:11
-
-
Save dannymcc/2312571 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| desc "Import Service Provider Location" | |
| task :fetch_serviceproviderlocation => :environment do | |
| # Logs into service provider and retrieved latitude and longitude. | |
| require 'rubygems' | |
| require 'mechanize' | |
| require 'logger' | |
| require 'nokogiri' | |
| # Create a new mechanize object | |
| agent = Mechanize.new | |
| # Load the Peoplesafe website | |
| page = agent.get("http://serviceprovider.co.uk") | |
| # Select the first form | |
| form = agent.page.forms.first | |
| form.username = 'USERNAME' | |
| form.password = 'PASSWORD' | |
| # Submit the form | |
| page = form.submit form.buttons.first | |
| script = page.at('/html/head/script[not(@src)]') | |
| # Find the first <script> in the head that does not have src="..." | |
| #script = html.at_xpath('/html/head/script[not(@src)]') | |
| # Use a regex to find the correct code parts in the JS, using named captures | |
| parts = script.text.match(/new GLatLng\(\s*(?<lat>.+?)\s*,\s*(?<long>.+?)\s*\)/) | |
| p parts[:lat], parts[:long] | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment