class WeatherByDcq::Forecast

Public Class Methods

display_curr_table(location) click to toggle source
# 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) click to toggle source
# 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_list() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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