Class: Bomtempo::Coordenate

Inherits:
Object
  • Object
show all
Defined in:
lib/bomtempo/coordenate.rb

Constant Summary collapse

API_GEO =
'https://api.openweathermap.org/geo/1.0/direct'

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Coordenate

Returns a new instance of Coordenate.



7
8
9
10
11
# File 'lib/bomtempo/coordenate.rb', line 7

def initialize(args)
  @city = args[:city]
  @state = args[:state]
  @country = args[:country] || "BR"
end

Instance Method Details

#getObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bomtempo/coordenate.rb', line 13

def get
  return "City is required" unless !@city.nil?
  return "State is required" unless !@state.nil?
  
  parameters = I18n.transliterate [@city, @state, @country].join(',') 
   
  url = "#{API_GEO}?q=#{parameters}&limit=5&appid=#{Class.nesting[-1].get_token}" 
  response = JSON.parse(HTTParty.get(url).body)[0]

  return if response.nil?

  {
    coordenate: {
      lat: response["lat"],
      lon: response["lon"],
    }, 
    city: response["name"]
  }
end