Class: WeatherJp::Adapter
- Inherits:
-
Object
- Object
- WeatherJp::Adapter
- Defined in:
- lib/weather_jp/adapter.rb
Defined Under Namespace
Modules: Reader
Constant Summary collapse
- BASE_URL =
'http://weather.service.msn.com/'
- ACTION =
'data.aspx'
- LANG =
'ja-JP'
- SCALE =
'C'
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.get(city_name) ⇒ WeatherJp::Weather?
Returns nil when not found weather forecasts.
Instance Method Summary collapse
- #build_weather(attrs, day_weathers) ⇒ Object
-
#city ⇒ Object
Ruby 2.0.0 version has no ‘Nokogiri::XML::Element#to_h`.
- #day_weather_nodes ⇒ Object
- #day_weathers ⇒ Object
- #get ⇒ Object
-
#initialize(city_name) ⇒ Adapter
constructor
A new instance of Adapter.
- #params ⇒ Object
- #url ⇒ Object
- #weather ⇒ Object
- #weather_node ⇒ Object
- #xml ⇒ Object
- #xml_str ⇒ Object
Constructor Details
#initialize(city_name) ⇒ Adapter
Returns a new instance of Adapter.
21 22 23 |
# File 'lib/weather_jp/adapter.rb', line 21 def initialize(city_name) @name = city_name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'lib/weather_jp/adapter.rb', line 19 def name @name end |
Class Method Details
.get(city_name) ⇒ WeatherJp::Weather?
Returns nil when not found weather forecasts.
9 10 11 |
# File 'lib/weather_jp/adapter.rb', line 9 def get(city_name) new(city_name).get end |
Instance Method Details
#build_weather(attrs, day_weathers) ⇒ Object
29 30 |
# File 'lib/weather_jp/adapter.rb', line 29 def build_weather(attrs, day_weathers) end |
#city ⇒ Object
Ruby 2.0.0 version has no ‘Nokogiri::XML::Element#to_h`.
48 49 50 51 52 53 |
# File 'lib/weather_jp/adapter.rb', line 48 def city @city ||= begin attrs = Hash[weather_node.attributes.map {|k, v| [k, v.value]}] City.new(attrs['weathercityname'], attrs) end end |
#day_weather_nodes ⇒ Object
43 44 45 |
# File 'lib/weather_jp/adapter.rb', line 43 def day_weather_nodes xml.xpath('/weatherdata/weather/*[attribute::skytextday or attribute::skytext]') end |
#day_weathers ⇒ Object
36 37 38 39 40 41 |
# File 'lib/weather_jp/adapter.rb', line 36 def day_weathers day_weather_nodes.each_with_index.map do |n, i| attrs = Hash[n.attributes.map {|k, v| [k, v.value] }] DayWeather.new(attrs, city, i - 1) end end |
#get ⇒ Object
25 26 27 |
# File 'lib/weather_jp/adapter.rb', line 25 def get weather_node ? weather : nil end |
#params ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/weather_jp/adapter.rb', line 71 def params { weadegreetype: SCALE, culture: LANG, weasearchstr: name, } end |
#url ⇒ Object
67 68 69 |
# File 'lib/weather_jp/adapter.rb', line 67 def url "#{BASE_URL}#{ACTION}?#{Rack::Utils.build_query(params)}" end |
#weather ⇒ Object
32 33 34 |
# File 'lib/weather_jp/adapter.rb', line 32 def weather Weather.new(city, day_weathers) end |
#weather_node ⇒ Object
55 56 57 |
# File 'lib/weather_jp/adapter.rb', line 55 def weather_node @weather_node ||= xml.xpath('/weatherdata/weather').first end |
#xml ⇒ Object
59 60 61 |
# File 'lib/weather_jp/adapter.rb', line 59 def xml @xml ||= Nokogiri::XML.parse(xml_str) end |