Class: WeatherJp::DayWeather

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/weather_jp/day_weather.rb

Constant Summary collapse

KEYS =

Canonical key names for attributes.

%i(text high low rain date date_text date_code city_name city)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs, city, date_code) ⇒ DayWeather

Returns a new instance of DayWeather.

Parameters:



15
16
17
18
19
# File 'lib/weather_jp/day_weather.rb', line 15

def initialize(attrs, city, date_code)
  @attrs = attrs
  @city = city
  @date_code = date_code
end

Instance Attribute Details

#cityWeatherJp::City (readonly)

Returns the current value of city.

Returns:



4
5
6
# File 'lib/weather_jp/day_weather.rb', line 4

def city
  @city
end

#date_codeInteger (readonly)

Current is 0, today is 1, tomorrow is 2

Returns:

  • (Integer)

    the current value of date_code



4
5
6
# File 'lib/weather_jp/day_weather.rb', line 4

def date_code
  @date_code
end

Instance Method Details

#city_nameString

Returns:

  • (String)


22
23
24
# File 'lib/weather_jp/day_weather.rb', line 22

def city_name
  city.name
end

#dateDate?

Returns:

  • (Date, nil)


67
68
69
# File 'lib/weather_jp/day_weather.rb', line 67

def date
  raw_date ? Date.parse(raw_date) : nil
end

#date_textString? Also known as: day

Japanese date text from current day.

Returns:

  • (String, nil)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/weather_jp/day_weather.rb', line 73

def date_text
  case date_code
  when -1
    ""
  when 0
    "今日"
  when 1
    "明日"
  when 2
    "明後日"
  else
    "#{date_code}日後"
  end
end

#highInteger? Also known as: max_temp

Returns:

  • (Integer, nil)


33
34
35
# File 'lib/weather_jp/day_weather.rb', line 33

def high
  get('high').try(:to_i)
end

#lowInteger? Also known as: min_temp

Returns:

  • (Integer, nil)


38
39
40
# File 'lib/weather_jp/day_weather.rb', line 38

def low
  get('low').try(:to_i)
end

#precipInteger? Also known as: rain

Returns:

  • (Integer, nil)


43
44
45
# File 'lib/weather_jp/day_weather.rb', line 43

def precip
  get('precip').try(:to_i)
end

#temperatureInteger?

Only available for current weather.

Returns:

  • (Integer, nil)


49
50
51
# File 'lib/weather_jp/day_weather.rb', line 49

def temperature
  get('temperature').try(:to_i)
end

#textString Also known as: forecast

Sky status. i.e. ‘sunny’ or ‘晴れ’.

Returns:

  • (String)


28
29
30
# File 'lib/weather_jp/day_weather.rb', line 28

def text
  @attrs['skytext'] || @attrs['skytextday']
end

#to_hashHash

Returns:

  • (Hash)


104
105
106
107
108
# File 'lib/weather_jp/day_weather.rb', line 104

def to_hash
  h = Hash[KEYS.map {|k| [k, public_send(k)] }]
  h[:city] = h[:city].to_hash
  h
end

#to_sString

Returns:

  • (String)


89
90
91
92
93
94
95
96
97
98
# File 'lib/weather_jp/day_weather.rb', line 89

def to_s
  word ="#{date_text}"
  word << "#{city_name}の天気は"
  word << "#{text}"
  word << ", 最高気温#{high}" if high
  word << ", 最低気温#{low}" if low
  word << ", 降水確率は#{precip}%" if precip
  word << "です。"
  word
end

#wind_speedInteger?

Only available for current weather.

Returns:

  • (Integer, nil)


55
56
57
# File 'lib/weather_jp/day_weather.rb', line 55

def wind_speed
  get('windspeed').try(:to_i)
end

#wind_textString?

Only available for current weather. i.e. ‘風向: 北北西 / 風速: 20 マイル’

Returns:

  • (String, nil)


62
63
64
# File 'lib/weather_jp/day_weather.rb', line 62

def wind_text
  get('winddisplay')
end