class GeoSeeder::Location

Constants

ADDRESS_COMPONENT_TYPES
API_KEY

Attributes

city[R]
country[R]
county[R]
formatted_address[R]
lat[R]
lng[R]
neighborhood[R]
region[R]
state[R]
street[R]
street_number[R]
zip_code[R]

Public Class Methods

address(latlng) click to toggle source
# File lib/geo_seeder.rb, line 78
def self.address(latlng)
  response = api_request({latlng: latlng})
  location = response.count > 0 ? GeoSeeder::Location.new(response.first) : nil
  return nil if location && location.formatted_address == "Foo Bar, San Francisco, CA 94115, USA"
  # Google returns a dummy address for bad latlng requests.
  location
end
latlng(address_string) click to toggle source
# File lib/geo_seeder.rb, line 86
def self.latlng(address_string)
  response = api_request({address: address_string})
  location = response.count > 0 ? GeoSeeder::Location.new(response.first) : nil
  return nil if location && location.formatted_address == "Foo Bar, San Francisco, CA 94115, USA"
  location
end
new(options) click to toggle source
# File lib/geo_seeder.rb, line 27
def initialize(options)
  @formatted_address = options["formatted_address"]
  @lat = options["geometry"]["location"]["lat"]
  @lng = options["geometry"]["location"]["lng"]
  
  options["address_components"].each do |comp|
    type = comp["types"].first
    ln = comp["long_name"]
    
    if ADDRESS_COMPONENT_TYPES[type]
      eval("#{ADDRESS_COMPONENT_TYPES[type]} = \"#{ln}\"")
    end
  end
end
random(defaults = {}) click to toggle source
# File lib/geo_seeder.rb, line 57
def self.random(defaults = {})
  options = {
    center: "Brooklyn, NY",
    quantity: 1,
    radius: 1
  }.merge(defaults)
  
  results = []
  
  while results.count < options[:quantity]
    rand_address = address(rand_coords(options[:center], options[:radius]))
    
    if rand_address && rand_address.street_number
      results << rand_address
    end
  end
  
  results
end

Protected Class Methods

api_request(options = {}) click to toggle source
# File lib/geo_seeder.rb, line 106
def self.api_request(options = {})
  # options must either contain an address string or a latlng array
  if options[:latlng]
    options[:latlng] = options[:latlng][0].to_s + "," + options[:latlng][1].to_s
  end

  q_vals = {
    sensor: "false", 
    key: API_KEY
  }.merge(options)

  query_string = Addressable::URI.new(
    :scheme => "https",
    :host => "maps.googleapis.com",
    :path => "maps/api/geocode/json",
    :query_values => q_vals
  ).to_s
 
  JSON.parse(RestClient.get(query_string))["results"]
end
rand_coords(center, radius) click to toggle source
# File lib/geo_seeder.rb, line 94
def self.rand_coords(center, radius)
  center_pt = nil
  until center_pt
    center_pt = latlng(center)
  end
  center_coords = [center_pt.lat, center_pt.lng]
  diff = radius * 1.0 / 70.0
  center_coords.map do |coord|
    (coord + rand(-diff..diff)).round(7)
  end
end

Public Instance Methods

all() click to toggle source
# File lib/geo_seeder.rb, line 46
def all
  addr_hash = {}
  
  instance_variables.each do |iv|
    variable = iv.to_s[1..-1]
    addr_hash[variable.to_sym] = eval("self.#{variable}")
  end  
  
  addr_hash
end
coordinates() click to toggle source
# File lib/geo_seeder.rb, line 42
def coordinates
  [self.lat, self.lng]
end