Class: GoogleGeocoding::Response
- Inherits:
-
Object
- Object
- GoogleGeocoding::Response
- Defined in:
- lib/google_geocoding/response.rb
Instance Method Summary collapse
-
#failure? ⇒ Boolean
Return whether response failed to resolved the geolocation.
-
#initialize(payload, result_class = nil) ⇒ Response
constructor
Creates a new response object from the given response body, expects to be a JSON string.
-
#results ⇒ Object
Return the types of the result.
-
#status ⇒ Symbol
Return the status code included in the server response.
-
#success? ⇒ Boolean
Return whether response successfully resolved the geolocation.
Constructor Details
#initialize(payload, result_class = nil) ⇒ Response
Creates a new response object from the given response body, expects to be a JSON string.
6 7 8 9 |
# File 'lib/google_geocoding/response.rb', line 6 def initialize(payload, result_class = nil) @data = JSON.parse(payload) @result_class = result_class || GoogleGeocoding.const_get(:Result) end |
Instance Method Details
#failure? ⇒ Boolean
Return whether response failed to resolved the geolocation
25 26 27 |
# File 'lib/google_geocoding/response.rb', line 25 def failure? !success? end |
#results ⇒ Object
Return the types of the result
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/google_geocoding/response.rb', line 30 def results @result ||= @data["results"].map do |result_data| result = @result_class.new( :types => result_data["types"], :address => result_data["formatted_address"], :coordinates => result_data["geometry"]["location"].values_at("lat", "lng"), :precision => result_data["geometry"]["location_type"] ) result_data["address_components"].each do |addr_comp_data| result << AddressComponent.new(:short_name => addr_comp_data["short_name"], :long_name => addr_comp_data["long_name"], :types => addr_comp_data["types"]) end result end end |
#status ⇒ Symbol
Return the status code included in the server response.
15 16 17 |
# File 'lib/google_geocoding/response.rb', line 15 def status @status ||= @data["status"].downcase.to_sym end |
#success? ⇒ Boolean
Return whether response successfully resolved the geolocation
20 21 22 |
# File 'lib/google_geocoding/response.rb', line 20 def success? status == :ok end |