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("https://weather.tsukumijima.net/api/forecast?city=#{city_id}") end |
Instance Attribute Details
#day_after_tomorrow ⇒ Day (readonly)
Returns the day after tomorrow.
48 49 50 |
# File 'lib/weather-report/weather.rb', line 48 def day_after_tomorrow @day_after_tomorrow end |
#today ⇒ Day (readonly)
Returns the today.
38 39 40 |
# File 'lib/weather-report/weather.rb', line 38 def today @today end |
#tomorrow ⇒ Day (readonly)
Returns the tomorrow.
43 44 45 |
# File 'lib/weather-report/weather.rb', line 43 def tomorrow @tomorrow end |
Class Method Details
.parse_proxy(proxy) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/weather-report/weather.rb', line 21 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 |
# File 'lib/weather-report/weather.rb', line 12 def self.request_cityid(city_name) raise ArgumentError, "City name must be String." unless city_name.kind_of?(String) proxy = Weather.parse_proxy(ENV["http_proxy"]) doc = Nokogiri::XML(URI.open("https://weather.tsukumijima.net/primary_area.xml", :proxy_http_basic_authentication => [proxy.server, proxy.user, proxy.pass])) doc.search("//city[@title='#{city_name}']").attr("id").value rescue NoMethodError raise NoCityError, "It seems like city #{city_name} does not exist.\nPlease look at https://weather.tsukumijima.net/primary_area.xml for city list." end |
Instance Method Details
#link ⇒ String
Returns the URL of the requested livedoor weather.
53 54 55 56 |
# File 'lib/weather-report/weather.rb', line 53 def link @response ||= read @response["link"] end |
#to_h ⇒ Hash
Returns the weather with Hash format.
59 60 61 62 63 64 65 66 |
# File 'lib/weather-report/weather.rb', line 59 def to_h { "today" => today.to_h, "tomorrow" => tomorrow.to_h, "day_after_tomorrow" => day_after_tomorrow.to_h, "link" => link } end |