Skip to content

Instantly share code, notes, and snippets.

@skamithi
Created May 20, 2012 03:16
Show Gist options
  • Save skamithi/2739019 to your computer and use it in GitHub Desktop.
Save skamithi/2739019 to your computer and use it in GitHub Desktop.
mocking geocode in my rails3 project
## /spec/support/mock_geo.rb
## MockGeo takes an address in the following format and can produce
## fake google geocode output
## All country and state information resolves to North Carolina, USA
## Include "gem jbuilder in :test and :development group" if not already in the project.
## Example
## Output for address string *MUST* be in this format
## a = '102 My Street, My Town, , NC, 99903' OR
# a = '102 my street, my town, nc'
## b = MockGeo.new(:addr => a)
## c = b.create_mock
#
class MockGeo
attr_accessor :address, :city, :street_number, :postal_code, :street_name
def initialize(options = {})
@address = options[:addr]
street,@city,abbr_state,@postal_code = @address.split(',')
if street
street_array = street.split(/\s+/)
if street_array && street_array.count > 1
@street_number = street_array[0]
street_array.shift
@street_name = street_array.join(' ')
end
end
if @postal_code.blank?
@postal_code = '27711'
end
# strip strings
@street_name.strip! if @street_name
@street_number.strip! if @street_number
@address.strip! if @address
@postal_code.strip! if @postal_code
end
def create_mock
Jbuilder.encode do |json|
json.status 'OK'
json.results do |json|
json.child! do |json|
json.types ['street_address']
json.formatted_address self.address
json.address_components do |json|
json.child! do |json|
json.long_name self.street_number
json.name self.street_number
json.types ['street_number']
end
json.child! do |json|
json.long_name self.street_name
json.short_name self.street_name
json.types %w{route}
end
json.child! do |json|
json.long_name self.city
json.short_name self.city
json.types %w{locality political}
end
json.child! do |json|
json.long_name 'North Carolina'
json.short_name 'North Carolina'
json.types %w{administrative_area_level_2 political}
end
json.child! do |json|
json.long_name 'North Carolina'
json.short_name 'NC'
json.types %w{administrative_area_level_1 political}
end
json.child! do |json|
json.long_name 'United States'
json.short_name 'US'
json.types %w{country political}
end
json.child! do |json|
json.long_name self.postal_code
json.short_name self.postal_code
json.types %w{postal_code}
end
end
json.geometry do |json|
json.location do |json|
json.lat "40.7503#{Random.rand(999)}".to_f
json.lng "-73.9996#{Random.rand(999)}".to_f
end
json.viewport do |json|
json.southwest do |json|
json.lat "40.7503#{Random.rand(999)}".to_f
json.lng "-73.9996#{Random.rand(999)}".to_f
end
json.northeast do |json|
json.lat "40.7503#{Random.rand(999)}".to_f
json.lng "-73.9996#{Random.rand(999)}".to_f
end
end
json.location_type 'ROOFTOP'
end
end
end
end
end
end
# /spec/support/mock_geocode.rb
require Rails.root.join('spec', 'support', 'mock_geo')
module Geocoder
module Lookup
class Base
private #-----------------------------------------------------------------
def read_fixture(file)
File.read(File.join("spec", "fixtures", file)).strip.gsub(/\n\s*/, "")
end
end
class Google < Base
private #-----------------------------------------------------------------
def fetch_raw_data(query, reverse = false)
raise TimeoutError if query == "timeout"
raise SocketError if query == "socket_error"
mock_geo = MockGeo.new(:addr => query)
mock_geo.create_mock
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment