Class: Cowa::WeatherHacks
- Inherits:
-
Object
- Object
- Cowa::WeatherHacks
- Defined in:
- lib/cowa/weather_hacks.rb
Instance Attribute Summary collapse
-
#rss ⇒ Object
Returns the value of attribute rss.
Instance Method Summary collapse
- #client(url) ⇒ Object
- #forecast(location) ⇒ Object
- #get_city(pref) ⇒ Object
- #get_id(city, location) ⇒ Object
- #get_pref(location) ⇒ Object
-
#initialize(api_key) ⇒ WeatherHacks
constructor
A new instance of WeatherHacks.
- #locationToRss(location) ⇒ Object
- #xml_to_json(xml) ⇒ Object
Constructor Details
#initialize(api_key) ⇒ WeatherHacks
Returns a new instance of WeatherHacks.
11 12 13 14 |
# File 'lib/cowa/weather_hacks.rb', line 11 def initialize(api_key) @contentsgeocoder = Yapi::OpenLocalPlatform::ContentsGeoCoder.new(api_key) self.rss = xml_to_json(open(URI.encode("http://weather.livedoor.com/forecast/rss/primary_area.xml")).read)[:rss][:channel][:source][:pref] end |
Instance Attribute Details
#rss ⇒ Object
Returns the value of attribute rss.
9 10 11 |
# File 'lib/cowa/weather_hacks.rb', line 9 def rss @rss end |
Instance Method Details
#client(url) ⇒ Object
70 71 72 |
# File 'lib/cowa/weather_hacks.rb', line 70 def client url return open(URI.encode(url)).read end |
#forecast(location) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/cowa/weather_hacks.rb', line 74 def forecast location rss = locationToRss(location) return nil if rss.nil? req_url = "http://weather.livedoor.com/forecast/webservice/json/v1?city=#{rss}" return JSON.parse(client(req_url), symbolize_names: true) end |
#get_city(pref) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/cowa/weather_hacks.rb', line 50 def get_city pref self.rss().each do |t| if t[:title] == pref return t[:city] end end return nil end |
#get_id(city, location) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/cowa/weather_hacks.rb', line 59 def get_id(city, location) unless city.nil? city.each do |c| return c[:id] if c[:title] == location end return city[0][:id] else return nil end end |
#get_pref(location) ⇒ Object
44 45 46 47 48 |
# File 'lib/cowa/weather_hacks.rb', line 44 def get_pref location json = @contentsgeocoder.contentsGeoCoder(location) return nil if json[:YDF][:ResultInfo][:Count] == "0" return json[:YDF][:Feature][:Property][:AddressElement] end |
#locationToRss(location) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cowa/weather_hacks.rb', line 21 def locationToRss location pref = get_pref location return nil if pref.nil? if pref[0][:Name] == "北海道" if location == "北海道" return self.rss[3][:city][0][:id] end for i in 0..4 self.rss[i][:city].each do |c| return c[:id] if pref[1][:Name].include?(c[:title]) end end return nil end unless pref.nil? city = get_city pref[0][:Name] else city = get_city location end id = get_id(city, location) return id end |
#xml_to_json(xml) ⇒ Object
16 17 18 19 |
# File 'lib/cowa/weather_hacks.rb', line 16 def xml_to_json xml doc = Hash.from_xml xml return JSON.parse(doc.to_json, symbolize_names: true) end |