Class: GitFit::Geo::NominatimClient

Inherits:
Object
  • Object
show all
Defined in:
lib/git_fit/geo/nominatim_client.rb

Constant Summary collapse

BASE_URL =
'https://nominatim.openstreetmap.org/reverse'
USER_AGENT =
'git-fit/1.0 (geo-detector)'

Instance Method Summary collapse

Constructor Details

#initializeNominatimClient

Returns a new instance of NominatimClient.



13
14
15
16
17
# File 'lib/git_fit/geo/nominatim_client.rb', line 13

def initialize
  @mutex = Mutex.new
  @last_request = 0.0
  @min_interval = 1.0
end

Instance Method Details

#reverse_geocode(lat, lng) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/git_fit/geo/nominatim_client.rb', line 19

def reverse_geocode(lat, lng)
  rate_limit
  uri = URI(BASE_URL)
  uri.query = URI.encode_www_form(
    lat: lat,
    lon: lng,
    format: 'jsonv2',
    addressdetails: 1,
    zoom: 10,
  )
  resp = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    req = Net::HTTP::Get.new(uri)
    req['User-Agent'] = USER_AGENT
    http.request(req)
  end
  body = JSON.parse(resp.body)
  if body['error']
    raise "Nominatim error: #{body["error"]}"
  end
  parse_response(body)
end