Class: GeoPack::UrbanMapping

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/geo_pack/urban_mapping.rb

Constant Summary collapse

URBAN_MAPPING_API =
{
  'getNeighborhoodsByLatLng' => [:lat, :lng],
  'getNearestNeighborhood'   => [:lat, :lng],
  'getNeighborhoodsByExtent' => [:swlat, :swlng, :nelat, :nelng],
  'getNeighborhoodsByAddress'=> [:street, :city, :state, :country],
  'getNeighborhoodsByCityStateCountry' => [:city, :state, :country],
  'getNeighborhoodsByPostalCode' => [:postalcode],
  'getNeighborhoodsByName' => [:name],
  'getNeighborhoodDetail' => [:neighborhoodId],
  'getNeighborhoodRelationships' => [:neighborhoodId]
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apikey, secret) ⇒ UrbanMapping

Returns a new instance of UrbanMapping.



24
25
26
27
# File 'lib/geo_pack/urban_mapping.rb', line 24

def initialize(apikey, secret)
  @apikey = apikey
  @secret = secret
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/geo_pack/urban_mapping.rb', line 29

def method_missing(method, *args)
  if URBAN_MAPPING_API.has_key?( method )
    query(method, *args)
  else
    super
  end
end

Class Method Details

.query(options = {}) ⇒ Object



37
38
39
40
# File 'lib/geo_pack/urban_mapping.rb', line 37

def self.query(options= {})
  um = self.new(options[:apikey])
  return um.query(options)
end

Instance Method Details

#check_requirements(options) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/geo_pack/urban_mapping.rb', line 72

def check_requirements(options)
  raise( 'apikey required for urbanmapping' ) if @apikey.nil?
  raise( 'method required for urbanmapping' ) if options[:method].nil?
  raise( "method #{options[:method]} urbanmapping" ) unless URBAN_MAPPING_API.has_key?( options[:method] )
  #missing_params = []
  #URBAN_MAPPING_API[options[:method]].each {|param| missing_params<<param unless options.has_key?(param) }
  #raise( "#{options[:method]} requires #{missing_params.join(", ")} parameters" ) unless missing_params.empty?
end

#generate_sigObject



81
82
83
84
85
86
# File 'lib/geo_pack/urban_mapping.rb', line 81

def generate_sig
  md5_input_string = @apikey + @secret + Time.now.to_i.to_s
  d = Digest::MD5.new

  return d.hexdigest(md5_input_string)
end

#munge_key_names(options = {}) ⇒ Object

so we can use prettier options than urbanmapping provides



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/geo_pack/urban_mapping.rb', line 54

def munge_key_names(options={})
  options_mapping = {
    :latitude => :lat,
    :longitude => :lng,
    :long => :lng,
    :zip => :postalcode,
    :zipcode => :postalcode,
    :neighborhood_id => :neighborhoodId}

    options.map do |key,value|
      if options_mapping.has_key? key
        options[options_mapping[key]] = options.delete key
      end
    end

    return options
end

#query(options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/geo_pack/urban_mapping.rb', line 43

def query(options={})
  check_requirements(options)

  method = options.delete(:method)
  options.merge!({:apikey => @apikey})
  options.merge!({:sig => generate_sig})

  UrbanMapping.get("http://api1.urbanmapping.com/neighborhoods/rest/" + method, :query => options)
end