Class: Katalyst::GoogleApis::Geocoding::SearchService

Inherits:
Object
  • Object
show all
Defined in:
app/services/katalyst/google_apis/geocoding/search_service.rb

Overview

Use Google Maps Geocoding API to find a location from an address.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials:) ⇒ SearchService

Returns a new instance of SearchService.



20
21
22
# File 'app/services/katalyst/google_apis/geocoding/search_service.rb', line 20

def initialize(credentials:)
  @credentials = credentials
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



10
11
12
# File 'app/services/katalyst/google_apis/geocoding/search_service.rb', line 10

def error
  @error
end

#responseObject (readonly)

Returns the value of attribute response.



10
11
12
# File 'app/services/katalyst/google_apis/geocoding/search_service.rb', line 10

def response
  @response
end

#resultObject (readonly)

Returns the value of attribute result.



10
11
12
# File 'app/services/katalyst/google_apis/geocoding/search_service.rb', line 10

def result
  @result
end

Class Method Details

.call(address:, bounds:, credentials: Katalyst::GoogleApis.credentials(scope:)) ⇒ Object



16
17
18
# File 'app/services/katalyst/google_apis/geocoding/search_service.rb', line 16

def self.call(address:, bounds:, credentials: Katalyst::GoogleApis.credentials(scope:))
  new(credentials:).call(address:, bounds:)
end

.scopeObject



12
13
14
# File 'app/services/katalyst/google_apis/geocoding/search_service.rb', line 12

def self.scope
  "https://www.googleapis.com/auth/maps-platform.geocode.address"
end

Instance Method Details

#call(address:, bounds:) ⇒ 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
60
# File 'app/services/katalyst/google_apis/geocoding/search_service.rb', line 24

def call(address:, bounds:)
  @address = address
  @bounds  = bounds

  @response = Curl.get(url, **params) 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_locationObject



66
67
68
# File 'app/services/katalyst/google_apis/geocoding/search_service.rb', line 66

def first_location
  locations&.first
end

#formatted_addressObject



70
71
72
# File 'app/services/katalyst/google_apis/geocoding/search_service.rb', line 70

def formatted_address
  first_location&.dig(:formattedAddress)
end

#inspectObject



85
86
87
# File 'app/services/katalyst/google_apis/geocoding/search_service.rb', line 85

def inspect
  "#<#{self.class.name} result: #{@result.inspect} error: #{@error.inspect}>"
end

#latlngObject



74
75
76
77
78
79
80
81
82
83
# File 'app/services/katalyst/google_apis/geocoding/search_service.rb', line 74

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

#locationsObject



62
63
64
# File 'app/services/katalyst/google_apis/geocoding/search_service.rb', line 62

def locations
  @result&.fetch(:results, nil)
end