class WeatherReport::Weather
Attributes
day_after_tomorrow[R]
today[R]
tomorrow[R]
Public Class Methods
new(city_id)
click to toggle source
# File lib/weather-report/weather.rb, line 6 def initialize(city_id) @uri = URI.parse("https://weather.tsukumijima.net/api/forecast?city=#{city_id}") end
parse_proxy(proxy)
click to toggle source
# File lib/weather-report/weather.rb, line 20 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)
click to toggle source
@return [String] the id of given city
# File lib/weather-report/weather.rb, line 11 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
Public Instance Methods
link()
click to toggle source
@return [String] the URL of the requested livedoor weather
# File lib/weather-report/weather.rb, line 52 def link @response ||= read @response["link"] end
to_h()
click to toggle source
@return [Hash] the weather with Hash format
# File lib/weather-report/weather.rb, line 58 def to_h { "today" => today.to_h, "tomorrow" => tomorrow.to_h, "day_after_tomorrow" => day_after_tomorrow.to_h, "link" => link } end
Private Instance Methods
forecasts()
click to toggle source
# File lib/weather-report/weather.rb, line 69 def forecasts @forecasts ||= read end
read()
click to toggle source
# File lib/weather-report/weather.rb, line 73 def read @response ||= JSON.parse(@uri.read) rescue => e raise Error end