Class: MyWeatherForecast

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

Overview

This gem is a wrapper of the forecast_io gem SI explained: en.wikipedia.org/wiki/SI_derived_unit

Defined Under Namespace

Classes: Daily, Hourly

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*location, api_key: nil, units: :auto, timeout: 3, symbols: true) ⇒ MyWeatherForecast

Returns a new instance of MyWeatherForecast.



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
# File 'lib/myweatherforecast.rb', line 20

def initialize(*location, api_key: nil, units: :auto, timeout: 3, \
                                                             symbols: true)
  lat, lon = if location[0].is_a? Array then
  
    location[0]
    
  elsif location.any?
    
    results = Geocoder.search(location.first)
    return puts 'location not found' unless results.any?
    results[0].coordinates
    
  else
    
    h = JSON.parse open('http://jsonip.com/').read
    Geocoder.configure(timeout: timeout)
    results = Geocoder.search(h['ip'])
    return puts 'could not determine location from IP address' unless \
                                                                 results.any?
    results[0].coordinates
                            
  end        

  ForecastIO.api_key = api_key

  params = { units: units.to_s }
  @forecast = ForecastIO.forecast(lat, lon, params: params)

  autounits = @forecast['flags']['units']
  
  @tlabel = if symbols then
    autounits == 'us' ? '°F' : '°C'
  else
    autounits == 'us' ? 'degrees Farenheit' : 'degrees Celcius'
  end
  
  @coordinates = [lat, lon]
  @symbols = symbols

end

Instance Attribute Details

#coordinatesObject (readonly)

Returns the value of attribute coordinates.



18
19
20
# File 'lib/myweatherforecast.rb', line 18

def coordinates
  @coordinates
end

Instance Method Details

#daysObject

e.g. require ‘myweatherforecast’

w = MyWeatherForecast.new api_key: ‘465xxxxxxxxxxxxxx76ea01cbff4’ puts w.days.take 3

Fri: 8° - 14°, Mostly cloudy throughout the day. Sat: 6° - 13°, Light rain until afternoon. Sun: 5° - 12°, Mostly cloudy throughout the day.



293
294
295
# File 'lib/myweatherforecast.rb', line 293

def days()
  (@forecast['daily']['data'].length).times.map {|n| Daily.new(@forecast, @tlabel, n) }
end

#fridayObject Also known as: fri



328
# File 'lib/myweatherforecast.rb', line 328

def friday()    day :friday    end

#hoursObject



297
298
299
300
301
302
# File 'lib/myweatherforecast.rb', line 297

def hours()
  
  len = @forecast['hourly']['data'].length
  len.times.map {|i| Hourly.new @forecast, @tlabel, i}

end

#mondayObject Also known as: mon



324
# File 'lib/myweatherforecast.rb', line 324

def monday()    day :monday    end

#next_3daysObject



340
341
342
343
344
345
346
# File 'lib/myweatherforecast.rb', line 340

def next_3days()
  
  days().take(4)[1..-1].map do |x|
    "%s: %s - %s %s" % [x.time.strftime("%a"), x.tempmin, x.tempmax, x.summary]
  end.join("\n")    
  
end

#next_5daysObject



348
349
350
351
352
353
354
# File 'lib/myweatherforecast.rb', line 348

def next_5days()
  
  days().take(6)[1..-1].map do |x|
    "%s: %s %s" % [x.time.strftime("%a"), x.tempmax, x.summary]  
  end.join("\n")

end

#nowObject Also known as: currently



304
305
306
# File 'lib/myweatherforecast.rb', line 304

def now()
  Hourly.new(@forecast, @tlabel)
end

#saturdayObject Also known as: sat



329
# File 'lib/myweatherforecast.rb', line 329

def saturday()  day :saturday  end

#sundayObject Also known as: sun



330
# File 'lib/myweatherforecast.rb', line 330

def sunday()    day :sunday    end

#thursdayObject Also known as: thu



327
# File 'lib/myweatherforecast.rb', line 327

def thursday()  day :thursday  end

#todayObject Also known as: this



308
309
310
# File 'lib/myweatherforecast.rb', line 308

def today()
  Daily.new(@forecast, @tlabel)
end

#tomorrowObject



320
321
322
# File 'lib/myweatherforecast.rb', line 320

def tomorrow()    
  Daily.new(@forecast, @tlabel, 1)
end

#tonightObject



314
315
316
# File 'lib/myweatherforecast.rb', line 314

def tonight()
  Daily.new(@forecast, @tlabel).night
end

#tuesdayObject Also known as: tue



325
# File 'lib/myweatherforecast.rb', line 325

def tuesday()   day :tuesday   end

#wednesdayObject Also known as: wed



326
# File 'lib/myweatherforecast.rb', line 326

def wednesday() day :wednesday end