class MyWeatherForecast
This gem is a wrapper of the forecast_io gem SI explained: en.wikipedia.org/wiki/SI_derived_unit
Attributes
coordinates[R]
Public Class Methods
new(*location, api_key: nil, units: :auto, timeout: 3, \ symbols: true)
click to toggle source
# 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 URI.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
Public Instance Methods
days()
click to toggle source
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.
# File lib/myweatherforecast.rb, line 293 def days() (@forecast['daily']['data'].length).times.map {|n| Daily.new(@forecast, @tlabel, n) } end
friday()
click to toggle source
# File lib/myweatherforecast.rb, line 328 def friday() day :friday end
Also aliased as: fri
hours()
click to toggle source
# File lib/myweatherforecast.rb, line 297 def hours() len = @forecast['hourly']['data'].length len.times.map {|i| Hourly.new @forecast, @tlabel, i} end
monday()
click to toggle source
# File lib/myweatherforecast.rb, line 324 def monday() day :monday end
Also aliased as: mon
next_3days()
click to toggle source
# File lib/myweatherforecast.rb, line 340 def next_3days() days().take(4)[1..-1].map do |x| "%s: %s - %s #{x.emoji} %s" % [x.time.strftime("%a"), x.tempmin, x.tempmax, x.summary] end.join("\n") end
next_5days()
click to toggle source
# File lib/myweatherforecast.rb, line 349 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
now()
click to toggle source
# File lib/myweatherforecast.rb, line 304 def now() Hourly.new(@forecast, @tlabel) end
Also aliased as: currently
saturday()
click to toggle source
# File lib/myweatherforecast.rb, line 329 def saturday() day :saturday end
Also aliased as: sat
sunday()
click to toggle source
# File lib/myweatherforecast.rb, line 330 def sunday() day :sunday end
Also aliased as: sun
thursday()
click to toggle source
# File lib/myweatherforecast.rb, line 327 def thursday() day :thursday end
Also aliased as: thu
today()
click to toggle source
# File lib/myweatherforecast.rb, line 308 def today() Daily.new(@forecast, @tlabel) end
Also aliased as: this
tomorrow()
click to toggle source
# File lib/myweatherforecast.rb, line 320 def tomorrow() Daily.new(@forecast, @tlabel, 1) end
tonight()
click to toggle source
# File lib/myweatherforecast.rb, line 314 def tonight() Daily.new(@forecast, @tlabel).night end
tuesday()
click to toggle source
# File lib/myweatherforecast.rb, line 325 def tuesday() day :tuesday end
Also aliased as: tue
wednesday()
click to toggle source
# File lib/myweatherforecast.rb, line 326 def wednesday() day :wednesday end
Also aliased as: wed
Private Instance Methods
day(name)
click to toggle source
# File lib/myweatherforecast.rb, line 359 def day(name) name = (name.to_s + '?').to_sym d = 0 d += 1 until Time.at(@forecast['daily']['data'][d].time).method(name).call Time.at(@forecast['daily']['data'][d].time) Daily.new(@forecast, @tlabel, d) end