class MQReader::MQGeocode
Class to handle the response from the mapquest api
Provides methods to get every value from the geocoded address.
Attributes
city[R]
country[R]
county[R]
lat[R]
lng[R]
raw_geocode[R]
state[R]
street[R]
zip[R]
Public Class Methods
new(json_geocode)
click to toggle source
# File lib/mq_reader/mq_reader.rb, line 109 def initialize(json_geocode) @raw_geocode = JSON.parse(json_geocode) raise StandardError, "The request raised an error: #{@raw_geocode['info']['messages']}" if @raw_geocode['info']['statuscode'] != 0 return if @raw_geocode['results'].first['locations'].empty? @lng,@lat = lat_lng_values @street = value('street') @city = value('adminArea5') @county = value('adminArea4') @state = value('adminArea3') @country = value('adminArea1') @zip = value('postalCode') @type = value('type') @geocode_quality = value('geocodeQuality') @side_of_street = value('sideOfStreet') end
Public Instance Methods
address_found?()
click to toggle source
# File lib/mq_reader/mq_reader.rb, line 125 def address_found? @raw_geocode['results'].first['locations'].any? end
Private Instance Methods
lat_lng_values()
click to toggle source
Get the latitude and longitude from the response
# File lib/mq_reader/mq_reader.rb, line 137 def lat_lng_values latLng = value('latLng') return [latLng['lng'],latLng['lat']] end
method_missing(method_sym, *arguments, &block)
click to toggle source
Use method_missing
to define accesors for any response attributes that might not be listed in initialize This accesors are rubyish(underscore notation).
Calls superclass method
# File lib/mq_reader/mq_reader.rb, line 144 def method_missing(method_sym, *arguments, &block) result = send(:value, BaseClass.camelize_string(method_sym.to_s)) result.empty? ? super : result end
value(field)
click to toggle source
Get the value of a field from the respond
# File lib/mq_reader/mq_reader.rb, line 132 def value(field) @raw_geocode['results'].first['locations'].first[field] end