public
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.