Class: Weather

Inherits:
Object
  • Object
show all
Includes:
ShellCharacters, ShellColors
Defined in:
lib/bbc/weather.rb

Overview

Lists BBC weather for the requested location

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ShellCharacters

degrees_c, degrees_f, east, north, north_east, north_west, south, south_east, south_west, square_block, symbol_for_compass, west

Methods included from ShellColors

blue, cyan, green, light_green, paint, red, white, yellow

Constructor Details

#initialize(location, io = STDOUT) ⇒ Weather

Returns a new instance of Weather.



11
12
13
14
# File 'lib/bbc/weather.rb', line 11

def initialize(location, io = STDOUT)
  @io = io
  @base_url = "http://open.live.bbc.co.uk/weather/feeds/en/#{location}"
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



9
10
11
# File 'lib/bbc/weather.rb', line 9

def base_url
  @base_url
end

Instance Method Details

#dailyObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bbc/weather.rb', line 49

def daily
  feed = load("#{base_url}/3dayforecast.json")

  location = feed['forecastContent']['location']['locationName']

  @io.puts "\nThe next 3 days in #{location}\n\n"
  @io.puts "Day        Weather           Max#{degrees_c}  Wind"

  feed['forecastContent']['forecasts'].each do |e|
    day = sprintf '%-10s', e['dayName']

    d_weather = e['day']['weatherType']
    d_weather = sprintf '%-12s', d_weather unless d_weather.nil?

    d_temp = e['day']['maxTemperature']['centigrade']
    d_temp = "#{d_temp}\xC2\xB0C" unless d_temp.nil?

    d_wind = e['day']['wind']['directionDesc']
    d_wind_spd = e['day']['wind']['windspeed']['mph']
    d_wind_spd = "#{d_wind_spd}mph" unless d_wind_spd.nil?

    day_desc = sprintf '%-17s %-6s %-5s %-20s', d_weather, d_temp, d_wind_spd, d_wind

    n_weather = e['night']['weatherType']
    n_temp = e['night']['minTemperature']['centigrade']
    n_temp = "#{n_temp}\xC2\xB0C" unless n_temp.nil?

    night_desc = sprintf '%-17s %-3s', n_weather, n_temp

    @io.puts "#{yellow day} #{day_desc} #{cyan 'Night'} #{white night_desc}"
  end
end

#hourlyObject



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
# File 'lib/bbc/weather.rb', line 16

def hourly
  feed = load("#{base_url}/3hourlyforecast.json")

  location = feed['forecastContent']['location']['locationName']

  @io.puts "\nThe next 24 hours in #{location}\n\n"
  @io.puts "Day        Time    Weather            Max#{degrees_c}    Wind"

  feed['forecastContent']['forecasts'].each do |e|

    day = e['dayName'].ljust(10)
    time = e['timeSlot'].ljust(7)
    desc = e['weatherType'].ljust(19)

    temp = e['temperature']['centigrade'].to_s || '?'
    temp = "#{temp}#{degrees_c}".ljust(6)

    wind = e['wind']['directionDesc']
    # wind_dir = e['wind']['direction']
    wind_spd = e['wind']['windspeed']['mph'] || '?'
    wind_spd = "#{wind_spd}mph".ljust(5)

    # visibility = e['visibility']
    # millibars = e['pressureMillibars']
    # humidity = e['humidityPercent']
    # temp_colour = temp_to_color(temp.to_i)

    @io.puts "#{yellow day} #{time} #{desc} #{temp} #{wind_spd} #{wind}"
  end

  @io.puts "\n"
end