Class: Airports::GeonamesApi

Inherits:
Object
  • Object
show all
Defined in:
lib/movlog/geonames_api.rb

Overview

Service for transformation of location and find near airport

Constant Summary collapse

GEONAMES_URL =
'http://api.geonames.org'
SEARCH_URL =
[GEONAMES_URL, 'searchJSON'].join('/')
FIND_NEARBY_URL =
[GEONAMES_URL, 'findNearbyJSON'].join('/')

Class Method Summary collapse

Class Method Details

.configObject



16
17
18
19
20
21
# File 'lib/movlog/geonames_api.rb', line 16

def self.config
  return @config if @config
  @config = {
    username: ENV['GEONAMES_USERNAME']
  }
end

.config=(credentials) ⇒ Object



11
12
13
14
# File 'lib/movlog/geonames_api.rb', line 11

def self.config=(credentials)
  @config = {} unless @config
  @config.update(credentials)
end

.geo_info(location) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/movlog/geonames_api.rb', line 23

def self.geo_info(location)
  search_response = HTTP.get(
    SEARCH_URL,
    params: {
      username: config[:username],
      q: location,
      fuzzy: 0.5,
      maxRows: 1
    }
  )
  JSON.parse(search_response.to_s)['geonames'].first
end

.near_airports(lat:, lng:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/movlog/geonames_api.rb', line 36

def self.near_airports(lat:, lng:)
  findnearby_response = HTTP.get(
    FIND_NEARBY_URL,
    params: {
      username: config[:username],
      fcode: 'AIRP',
      lat: lat, lng: lng,
      radius: 200, maxRows: 50
    }
  )
  JSON.parse(findnearby_response.to_s)['geonames']
end