Class: Journal::Weather

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, zip, temp_in) ⇒ Weather

Initialize the weather object, contacting API and parsing out conditions and forecast

Parameters:

Raises:

  • (StandardError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/journal-cli/weather.rb', line 14

def initialize(api, zip, temp_in)
  Journal.date.localtime
  res = if Journal.date.strftime("%Y-%m-%d") == Time.now.strftime("%Y-%m-%d")
    `curl -SsL 'http://api.weatherapi.com/v1/forecast.json?key=#{api}&q=#{zip}&aqi=no'`
  else
    `curl -SsL 'http://api.weatherapi.com/v1/history.json?key=#{api}&q=#{zip}&aqi=no&dt=#{Journal.date.strftime("%Y-%m-%d")}'`
  end

  data = JSON.parse(res)

  raise StandardError, "invalid JSON response" if data.nil?

  raise StandardError, "missing forecast" unless data["forecast"]

  temp_key = /^c/.match?(temp_in) ? "temp_c" : "temp_f"

  if Journal.date.strftime("%Y-%m-%d") == Time.now.strftime("%Y-%m-%d")
    raise StandardError, "missing conditions" unless data["current"]

    curr_temp = data["current"][temp_key]
    curr_condition = data["current"]["condition"]["text"]
  else
    time = Journal.date.strftime("%Y-%m-%d %H:00")
    hour = data["forecast"]["forecastday"][0]["hour"].find { |h| h["time"].to_s =~ /#{time}/ }
    curr_temp = hour[temp_key]
    curr_condition = hour["condition"]["text"]
  end

  forecast = data["forecast"]["forecastday"][0]

  moon_phase = forecast["astro"]["moon_phase"]

  day = forecast["date"]
  high = /^c/.match?(temp_in) ? forecast["day"]["maxtemp_c"] : forecast["day"]["maxtemp_f"]
  low = /^c/.match?(temp_in) ? forecast["day"]["mintemp_c"] : forecast["day"]["mintemp_f"]
  condition = forecast["day"]["condition"]["text"]

  hours = forecast["hour"]
  temps = [
    { temp: hours[8][temp_key], condition: hours[8]["condition"]["text"] },
    { temp: hours[10][temp_key], condition: hours[10]["condition"]["text"] },
    { temp: hours[12][temp_key], condition: hours[12]["condition"]["text"] },
    { temp: hours[14][temp_key], condition: hours[14]["condition"]["text"] },
    { temp: hours[16][temp_key], condition: hours[16]["condition"]["text"] },
    { temp: hours[18][temp_key], condition: hours[18]["condition"]["text"] },
    { temp: hours[19][temp_key], condition: hours[20]["condition"]["text"] }
  ]

  @data = {
    day: day,
    high: high,
    low: low,
    temp: curr_temp,
    condition: condition,
    current_condition: curr_condition,
    temps: temps,
    moon_phase: moon_phase
  }
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/journal-cli/weather.rb', line 5

def data
  @data
end

Instance Method Details

#currentString

Get current conditon

Returns:

  • (String)

    condition as string (54 and Sunny)



103
104
105
# File 'lib/journal-cli/weather.rb', line 103

def current
  "#{@data[:temp]} and #{@data[:current_condition]}"
end

#forecastString

Get daily forecast

Returns:

  • (String)

    daily forecast as string (Sunny 65/80)



113
114
115
# File 'lib/journal-cli/weather.rb', line 113

def forecast
  "#{@data[:condition]} #{@data[:high]}/#{@data[:low]}"
end

#moonString

Get moon phase

Returns:



93
94
95
# File 'lib/journal-cli/weather.rb', line 93

def moon
  @data[:moon_phase]
end

#to_dataHash

Convert weather object to hash

Returns:

  • (Hash)

    Data representation of the object.



79
80
81
82
83
84
85
86
# File 'lib/journal-cli/weather.rb', line 79

def to_data
  {
    high: @data[:high],
    low: @data[:low],
    condition: @data[:current_condition],
    moon_phase: @data[:moon_phase]
  }
end

#to_markdownString

Markdown representation of data, including hourly forecast and conditions

Returns:

  • (String)

    Markdown representation of the weather object.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/journal-cli/weather.rb', line 134

def to_markdown
  output = []

  output << "Forecast for #{@data[:day]}: #{forecast}  "
  output << "Currently: #{current}  "
  output << "Moon Phase: #{moon}  "
  output << ""

  # Hours
  hours_text = %w[8am 10am 12pm 2pm 4pm 6pm 8pm]
  step_out = ["|"]
  @data[:temps].each_with_index do |_h, i|
    width = @data[:temps][i][:condition].length + 1
    step_out << format("%#{width}s |", hours_text[i])
  end

  output << step_out.join("")

  # table separator
  step_out = ["|"]
  @data[:temps].each do |temp|
    width = temp[:condition].length + 1
    step_out << "#{"-" * width}-|"
  end

  output << step_out.join("")

  # Conditions
  step_out = ["|"]
  @data[:temps].each do |temp|
    step_out << format(" %s |", temp[:condition])
  end

  output << step_out.join("")

  # Temps
  step_out = ["|"]
  @data[:temps].each do |temp|
    width = temp[:condition].length + 1
    step_out << format("%#{width}s |", temp[:temp])
  end

  output << step_out.join("")

  output.join("\n")
end

#to_sString

Weather condition and forecast

Returns:

  • (String)

    string representation of the weather object.



123
124
125
# File 'lib/journal-cli/weather.rb', line 123

def to_s
  "#{@data[:temp].round} and #{@data[:current_condition]} (#{@data[:high].round}/#{@data[:low].round})"
end