Class: MQReader::MQGeocode

Inherits:
Object
  • Object
show all
Defined in:
lib/mq_reader/mq_reader.rb

Overview

Class to handle the response from the mapquest api

Provides methods to get every value from the geocoded address.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_geocode) ⇒ MQGeocode

Returns a new instance of MQGeocode.

Raises:

  • (StandardError)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object (private)

Use method_missing to define accesors for any response attributes that might not be listed in #initialize This accesors are rubyish(underscore notation).



144
145
146
147
# 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

Instance Attribute Details

#cityObject (readonly)

Returns the value of attribute city.



107
108
109
# File 'lib/mq_reader/mq_reader.rb', line 107

def city
  @city
end

#countryObject (readonly)

Returns the value of attribute country.



107
108
109
# File 'lib/mq_reader/mq_reader.rb', line 107

def country
  @country
end

#countyObject (readonly)

Returns the value of attribute county.



107
108
109
# File 'lib/mq_reader/mq_reader.rb', line 107

def county
  @county
end

#latObject (readonly)

Returns the value of attribute lat.



107
108
109
# File 'lib/mq_reader/mq_reader.rb', line 107

def lat
  @lat
end

#lngObject (readonly)

Returns the value of attribute lng.



107
108
109
# File 'lib/mq_reader/mq_reader.rb', line 107

def lng
  @lng
end

#raw_geocodeObject (readonly)

Returns the value of attribute raw_geocode.



107
108
109
# File 'lib/mq_reader/mq_reader.rb', line 107

def raw_geocode
  @raw_geocode
end

#stateObject (readonly)

Returns the value of attribute state.



107
108
109
# File 'lib/mq_reader/mq_reader.rb', line 107

def state
  @state
end

#streetObject (readonly)

Returns the value of attribute street.



107
108
109
# File 'lib/mq_reader/mq_reader.rb', line 107

def street
  @street
end

#zipObject (readonly)

Returns the value of attribute zip.



107
108
109
# File 'lib/mq_reader/mq_reader.rb', line 107

def zip
  @zip
end

Instance Method Details

#address_found?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/mq_reader/mq_reader.rb', line 125

def address_found?
  @raw_geocode['results'].first['locations'].any?
end