Class: GoogleGeocoding::Response

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

Instance Method Summary collapse

Constructor Details

#initialize(payload, result_class = nil) ⇒ Response

Creates a new response object from the given response body, expects to be a JSON string.

Parameters:

  • (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

Returns:

  • (Boolean)


25
26
27
# File 'lib/google_geocoding/response.rb', line 25

def failure?
  !success?
end

#resultsObject

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

#statusSymbol

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

Returns:

  • (Boolean)


20
21
22
# File 'lib/google_geocoding/response.rb', line 20

def success?
  status == :ok
end