Class: WeatherByDcq::Forecast

Inherits:
Object
  • Object
show all
Defined in:
lib/weather_by_dcq/forecast.rb

Class Method Summary collapse

Class Method Details

.display_curr_table(location) ⇒ Object



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
# File 'lib/weather_by_dcq/forecast.rb', line 21

def self.display_curr_table(location)
  ## Add TTY Spinner
  spinner = TTY::Spinner.new('[:spinner] Fetch from API', format: :bouncing_ball, clear: true)
  30.times do
    spinner.spin
    sleep(0.1)
  end
  spinner.success

  @forecast = WeatherByDcq::Request.fetch(location)
  puts ''
  puts '🌐 ' + @forecast['timezone']

  temperature = fetch_curr_data('temperature').to_i
  @temperature = ((temperature - 32) / 1.8).round(2)
  apparentTemperature = fetch_curr_data('apparentTemperature').to_i
  @apparentTemperature = ((apparentTemperature - 32) / 1.8).round(2)

  curr_table = TTY::Table.new ["\u{1F321} Temp.: #{@temperature}°C",
                               "\u{1F321} Heat Index: #{@apparentTemperature}°C"],
                              [["\u{1F4A6} Humidity: #{fetch_curr_data('humidity')}\u{0025}",
                                "🌀  Wind Speed: #{fetch_curr_data('windSpeed')}mph"]]
  puts curr_table.render(:ascii, padding: [0, 2])
  puts ''
end

.display_details(num) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/weather_by_dcq/forecast.rb', line 58

def self.display_details(num)
  puts ''
  display_forecast_list
  puts ''

  num -= 1
  date = Time.at(@forecast['daily']['data'][num]['time'])
  puts ''
  puts "                         Detailed forecast on #{date.strftime('%a')} - #{date.month}/#{date.day}".colorize(:blue)
  puts ' ========================================================================='.colorize(:blue)

  hi = fetch_data(num, 'temperatureHigh').to_i
  @hi = ((hi - 32) / 1.8).round(2)
  lo = fetch_data(num, 'temperatureLow').to_i
  @lo = ((lo - 32) / 1.8).round(2)
  hea = fetch_data(num, 'apparentTemperatureHigh').to_i
  @hea = ((hea - 32) / 1.8).round(2)

  # Build table
  detail_table = TTY::Table.new do |t|
    t << ["\u{1F321}  High: #{@hi}°C",
          "\u{1F321}  Low: #{@lo}°C",
          "\u{1F321}  Heat Index: #{@hea}°C"]
    t << ["🌀 Wind: #{fetch_data(num, 'windSpeed').to_i}mph",
          "🌦  Precip Prob.: #{(fetch_data(num, 'precipProbability') * 100).to_i}\u{0025}",
          "🌧  Precip.: #{fetch_data(num, 'precipType').to_s.capitalize}"]
    t << ["💧 Humidity: #{(fetch_data(num, 'humidity') * 100).to_i}\u{0025}",
          "🌇 Sunrise: #{fetch_sun_data(num, 'sunriseTime')}AM", "🌆 Sunset: #{fetch_sun_data(num, 'sunsetTime')}PM"]
  end

  puts "                          #{fetch_data(num, 'summary')}".colorize(:red)
  puts ''
  @icon = fetch_data(num, 'icon')
  if @icon.eql? 'rain'
    puts "                             \u{2614}" + "  #{@icon}".colorize(:yellow)
  elsif @icon.eql?'clear-day'
    puts "                             \u{1F324}" + "  #{@icon}".colorize(:yellow)
  elsif @icon.eql?'fog'
    puts "                             \u{1F32B}" + "  #{@icon}".colorize(:yellow)
  else
    puts "                             \u{1F325}" + "  #{@icon}".colorize(:yellow)
  end

  puts detail_table.render(:ascii, padding: [0, 2]) { |renderer|
    renderer.border.separator = :each_row
    renderer.border.style = :cyan
  }
end

.display_forecast_listObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/weather_by_dcq/forecast.rb', line 47

def self.display_forecast_list
  @forecast['daily'].each do |key, value|
    next unless key == 'data'

    value.each.with_index do |n, index|
      date = Time.at(n['time'])
      puts "#{index + 1})".colorize(:blue) + " #{date.strftime('%a')} - #{date.month}/#{date.day}"
    end
  end
end

.fetch_curr_data(key_data) ⇒ Object



8
9
10
# File 'lib/weather_by_dcq/forecast.rb', line 8

def self.fetch_curr_data(key_data)
  @forecast['currently'][key_data]
end

.fetch_data(num, key_data) ⇒ Object



12
13
14
# File 'lib/weather_by_dcq/forecast.rb', line 12

def self.fetch_data(num, key_data)
  @forecast['daily']['data'][num][key_data]
end

.fetch_sun_data(num, key_data) ⇒ Object



16
17
18
19
# File 'lib/weather_by_dcq/forecast.rb', line 16

def self.fetch_sun_data(num, key_data)
  time = Time.at(@forecast['daily']['data'][num][key_data])
  time.strftime('%I:%M ')
end