lib/weather-report/weather.rb in weather-report-0.3.2 vs lib/weather-report/weather.rb in weather-report-0.3.3

- old
+ new

@@ -8,13 +8,30 @@ @uri = URI.parse("http://weather.livedoor.com/forecast/webservice/json/v1?city=#{city_id}") end # @return [String] the id of given city def self.request_cityid(city_name) - doc = Nokogiri::XML(open("http://weather.livedoor.com/forecast/rss/primary_area.xml")) + 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 => e raise WeatherReportError + end + + 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 # @return [Day] the today def today @today ||= Day.new(forecasts, "今日")