Class: WeatherReport::Weather

Inherits:
Object
  • Object
show all
Defined in:
lib/weather-report/weather.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_tomorrowDay (readonly)

Returns the day after tomorrow.

Returns:

  • (Day)

    the day after tomorrow



48
49
50
# File 'lib/weather-report/weather.rb', line 48

def day_after_tomorrow
  @day_after_tomorrow
end

#todayDay (readonly)

Returns the today.

Returns:

  • (Day)

    the today



38
39
40
# File 'lib/weather-report/weather.rb', line 38

def today
  @today
end

#tomorrowDay (readonly)

Returns the tomorrow.

Returns:

  • (Day)

    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(":")
   = raccount.nil? ? "" : raccount.reverse.split(":")
  user, pass = 

  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.

Returns:

  • (String)

    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

Returns the URL of the requested livedoor weather.

Returns:

  • (String)

    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_hHash

Returns the weather with Hash format.

Returns:

  • (Hash)

    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