Class: WeatherReport::Weather
- Inherits:
-
Object
- Object
- WeatherReport::Weather
- Defined in:
- lib/weather-report/weather.rb
Instance Attribute Summary collapse
-
#day_after_tomorrow ⇒ Day
readonly
The day after tomorrow.
-
#today ⇒ Day
readonly
The today.
-
#tomorrow ⇒ Day
readonly
The tomorrow.
Class Method Summary collapse
- .parse_proxy(proxy) ⇒ Object
-
.request_cityid(city_name) ⇒ String
The id of given city.
Instance Method Summary collapse
-
#initialize(city_id) ⇒ Weather
constructor
A new instance of Weather.
-
#link ⇒ String
The URL of the requested livedoor weather.
-
#to_h ⇒ Hash
The weather with Hash format.
Constructor Details
#initialize(city_id) ⇒ Weather
Returns a new instance of Weather.
7 8 9 |
# File 'lib/weather-report/weather.rb', line 7 def initialize(city_id) @uri = URI.parse("http://weather.livedoor.com/forecast/webservice/json/v1?city=#{city_id}") end |
Instance Attribute Details
#day_after_tomorrow ⇒ Day (readonly)
Returns the day after tomorrow.
50 51 52 |
# File 'lib/weather-report/weather.rb', line 50 def day_after_tomorrow @day_after_tomorrow end |
#today ⇒ Day (readonly)
Returns the today.
40 41 42 |
# File 'lib/weather-report/weather.rb', line 40 def today @today end |
#tomorrow ⇒ Day (readonly)
Returns the tomorrow.
45 46 47 |
# File 'lib/weather-report/weather.rb', line 45 def tomorrow @tomorrow end |
Class Method Details
.parse_proxy(proxy) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/weather-report/weather.rb', line 23 def self.parse_proxy(proxy) # http://user:pass@host:port のように書かれていることを想定 # パスワードに@とか入ってる場合があるので一番後ろの@でだけsplitする rserver, raccount = (proxy || '').sub(/http:\/\//, '').reverse.split("@", 2) server = rserver.nil? ? "" : rserver.reverse host, port = server.split(":") account = raccount.nil? ? "" : raccount.reverse.split(":") user, pass = account proxy = OpenStruct.new({ "server" => server.empty? ? nil : "http://#{server}", "user" => user.nil? ? "" : user, "pass" => pass.nil? ? "" : pass }) end |
.request_cityid(city_name) ⇒ String
Returns the id of given city.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/weather-report/weather.rb', line 12 def self.request_cityid(city_name) proxy = Weather.parse_proxy(ENV["http_proxy"]) doc = Nokogiri::XML(open("http://weather.livedoor.com/forecast/rss/primary_area.xml", :proxy_http_basic_authentication => [proxy.server, proxy.user, proxy.pass])) doc.search("//city[@title='#{city_name}']").attr("id").value rescue NoMethodError puts "It seems like city #{city_name} does not exist.\nPlease look at http://weather.livedoor.com/forecast/rss/primary_area.xml for city list." exit 1 rescue => e raise WeatherReportError end |
Instance Method Details
#link ⇒ String
Returns the URL of the requested livedoor weather.
55 56 57 58 |
# File 'lib/weather-report/weather.rb', line 55 def link @response ||= read @response["link"] end |
#to_h ⇒ Hash
Returns the weather with Hash format.
61 62 63 64 65 66 67 68 |
# File 'lib/weather-report/weather.rb', line 61 def to_h { "today" => today.to_h, "tomorrow" => tomorrow.to_h, "day_after_tomorrow" => day_after_tomorrow.to_h, "link" => link } end |