Create a gist now

Instantly share code, notes, and snippets.

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 =
# Load the Peoplesafe website
page = agent.get("")
# Select the first form
form =
form.username = 'USERNAME'
form.password = 'PASSWORD'
# Submit the form
page = form.submit form.buttons.first
script ='/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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment