class Services::Infrastructure::Adapters::Geocode::GoogleGeocodeResponse
Google Geocode API response
Wraps the raw JSON from the Geocode API and exposes:
-
latitude / longitude — from
geometry.location -
zipcode — from the first
postal_codeinaddress_components -
formatted_address— from the first result’sformatted_address(canonical place name) -
source —
:api_responseor:cached_response
Example
response = GoogleGeocodeResponse.new(api_json, :api_response) response.latitude # => -23.5505 response.zipcode # => "01310"
Attributes
Public Class Methods
Source
# File app/services/infrastructure/adapters/geocode/google_geocode_response.rb 28 def initialize(data, source) 29 raise ArgumentError, "Data cannot be nil" if data.nil? 30 raise ArgumentError, "Source must be a symbol" unless source.is_a?(Symbol) 31 32 @data = data 33 @source = source 34 end
Builds a response from raw data and source.
-
data — Hash from the
GeocodeAPI (must not benil). -
source — Symbol
:api_responseor:cached_response.
Public Instance Methods
Source
# File app/services/infrastructure/adapters/geocode/google_geocode_response.rb 53 def formatted_address 54 result["formatted_address"]&.presence 55 end
Canonical place name from the first result’s formatted_address, or nil.
Source
# File app/services/infrastructure/adapters/geocode/google_geocode_response.rb 37 def latitude 38 result.dig("geometry", "location", "lat") 39 end
Latitude from the first result’s geometry.location.lat.
Source
# File app/services/infrastructure/adapters/geocode/google_geocode_response.rb 42 def longitude 43 result.dig("geometry", "location", "lng") 44 end
Longitude from the first result’s geometry.location.lng.
Source
# File app/services/infrastructure/adapters/geocode/google_geocode_response.rb 47 def zipcode 48 component = result["address_components"]&.find { |c| c["types"]&.include?("postal_code") } 49 component&.dig("short_name") 50 end
Postal code from the first result’s address_components (type postal_code), or nil.