Class: Cowa::OpenWeatherMap

Inherits:
Object
  • Object
show all
Defined in:
lib/cowa/open_weatherMap.rb

Constant Summary collapse

NotFound =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(open_weathermap_api_key, yahoo_api_key) ⇒ OpenWeatherMap

Returns a new instance of OpenWeatherMap.



10
11
12
13
# File 'lib/cowa/open_weatherMap.rb', line 10

def initialize(open_weathermap_api_key, yahoo_api_key)
  @contentsgeocoder = Yapi::OpenLocalPlatform::ContentsGeoCoder.new(yahoo_api_key)
  @api_key = open_weathermap_api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



8
9
10
# File 'lib/cowa/open_weatherMap.rb', line 8

def api_key
  @api_key
end

Instance Method Details

#client(url) ⇒ Object



15
16
17
# File 'lib/cowa/open_weatherMap.rb', line 15

def client url
  open(URI.encode(url)).read
end

#get_icon(icon) ⇒ Object



19
20
21
# File 'lib/cowa/open_weatherMap.rb', line 19

def get_icon icon
  open("http://openweathermap.org/img/w/#{icon}.png")
end

#get_information_latlon(lat, lon) ⇒ Object



30
31
32
33
# File 'lib/cowa/open_weatherMap.rb', line 30

def get_information_latlon lat, lon
  req_url = "http://api.openweathermap.org/data/2.5/weather?lat=#{lat}&lon=#{lon}&APPID=#{@api_key}"
  JSON.parse(client(req_url), symbolize_names: true)
end

#get_information_place(location) ⇒ Object

Raises:



23
24
25
26
27
28
# File 'lib/cowa/open_weatherMap.rb', line 23

def get_information_place location
  json = @contentsgeocoder.contentsGeoCoder(location)
  raise NotFound if json[:YDF][:ResultInfo][:Count] == "0"
  lon, lat = json[:YDF][:Feature][:Geometry][:Coordinates].split(',')
  get_information_latlon lat, lon
end