Class: Katalyst::GoogleApis::Geocoding::ReverseService
- Inherits:
-
Object
- Object
- Katalyst::GoogleApis::Geocoding::ReverseService
- Defined in:
- app/services/katalyst/google_apis/geocoding/reverse_service.rb
Overview
Use Google Maps Geocoding API to find a location from lat/lng.
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Class Method Summary collapse
Instance Method Summary collapse
- #call(latlng:) ⇒ Object
- #first_location ⇒ Object
- #formatted_address ⇒ Object
-
#initialize(credentials:) ⇒ ReverseService
constructor
A new instance of ReverseService.
- #inspect ⇒ Object
- #latlng ⇒ Object
- #locations ⇒ Object
Constructor Details
#initialize(credentials:) ⇒ ReverseService
Returns a new instance of ReverseService.
20 21 22 |
# File 'app/services/katalyst/google_apis/geocoding/reverse_service.rb', line 20 def initialize(credentials:) @credentials = credentials end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
10 11 12 |
# File 'app/services/katalyst/google_apis/geocoding/reverse_service.rb', line 10 def error @error end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
10 11 12 |
# File 'app/services/katalyst/google_apis/geocoding/reverse_service.rb', line 10 def response @response end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
10 11 12 |
# File 'app/services/katalyst/google_apis/geocoding/reverse_service.rb', line 10 def result @result end |
Class Method Details
.call(latlng:, credentials: Katalyst::GoogleApis.credentials(scope:)) ⇒ Object
16 17 18 |
# File 'app/services/katalyst/google_apis/geocoding/reverse_service.rb', line 16 def self.call(latlng:, credentials: Katalyst::GoogleApis.credentials(scope:)) new(credentials:).call(latlng:) end |
.scope ⇒ Object
12 13 14 |
# File 'app/services/katalyst/google_apis/geocoding/reverse_service.rb', line 12 def self.scope "https://www.googleapis.com/auth/maps-platform.geocode.location" end |
Instance Method Details
#call(latlng:) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/services/katalyst/google_apis/geocoding/reverse_service.rb', line 24 def call(latlng:) @latlng = latlng @response = Curl.get(url) do |http| http.headers["Content-Type"] = "application/json; UTF-8" @credentials.apply!(http.headers) end if %r{^application/json}.match?(@response.content_type) @result = JSON.parse(response.body, symbolize_names: true) else raise GoogleApis::Error.new( code: response.response_code, status: Rack::Utils::HTTP_STATUS_CODES[response.response_code], message: "Unexpected HTTP response received (#{response.response_code}, #{@response.content_type})", ) end if result[:error].present? api_error = result.fetch(:error) raise GoogleApis::Error.new( code: api_error.fetch(:code, response.response_code), status: api_error.fetch(:status, Rack::Utils::HTTP_STATUS_CODES[response.response_code]), message: api_error.fetch(:message, "Unexpected API error"), details: api_error.fetch(:details, nil), ) end self rescue StandardError => e @error = e raise ensure report_error end |
#first_location ⇒ Object
65 66 67 |
# File 'app/services/katalyst/google_apis/geocoding/reverse_service.rb', line 65 def first_location locations&.first end |
#formatted_address ⇒ Object
69 70 71 |
# File 'app/services/katalyst/google_apis/geocoding/reverse_service.rb', line 69 def formatted_address first_location&.dig(:formattedAddress) end |
#inspect ⇒ Object
84 85 86 |
# File 'app/services/katalyst/google_apis/geocoding/reverse_service.rb', line 84 def inspect "#<#{self.class.name} result: #{@result.inspect} error: #{@error.inspect}>" end |
#latlng ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'app/services/katalyst/google_apis/geocoding/reverse_service.rb', line 73 def latlng location = first_location&.dig(:location) return if location.blank? latitude = location[:latitude] longitude = location[:longitude] return if latitude.nil? || longitude.nil? [latitude, longitude].join(",") end |
#locations ⇒ Object
61 62 63 |
# File 'app/services/katalyst/google_apis/geocoding/reverse_service.rb', line 61 def locations @result&.fetch(:results, nil) end |