Class: MyWeatherForecast
- Inherits:
-
Object
- Object
- MyWeatherForecast
- 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
Instance Attribute Summary collapse
-
#coordinates ⇒ Object
readonly
Returns the value of attribute coordinates.
Instance Method Summary collapse
-
#days ⇒ Object
e.g.
- #friday ⇒ Object (also: #fri)
- #hours ⇒ Object
-
#initialize(*location, api_key: nil, units: :auto, timeout: 3, symbols: true) ⇒ MyWeatherForecast
constructor
A new instance of MyWeatherForecast.
- #monday ⇒ Object (also: #mon)
- #next_3days ⇒ Object
- #next_5days ⇒ Object
- #now ⇒ Object (also: #currently)
- #saturday ⇒ Object (also: #sat)
- #sunday ⇒ Object (also: #sun)
- #thursday ⇒ Object (also: #thu)
- #today ⇒ Object (also: #this)
- #tomorrow ⇒ Object
- #tonight ⇒ Object
- #tuesday ⇒ Object (also: #tue)
- #wednesday ⇒ Object (also: #wed)
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
#coordinates ⇒ Object (readonly)
Returns the value of attribute coordinates.
18 19 20 |
# File 'lib/myweatherforecast.rb', line 18 def coordinates @coordinates end |
Instance Method Details
#days ⇒ Object
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 |
#friday ⇒ Object Also known as: fri
328 |
# File 'lib/myweatherforecast.rb', line 328 def friday() day :friday end |
#hours ⇒ Object
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 |
#monday ⇒ Object Also known as: mon
324 |
# File 'lib/myweatherforecast.rb', line 324 def monday() day :monday end |
#next_3days ⇒ Object
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_5days ⇒ Object
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 |
#now ⇒ Object Also known as: currently
304 305 306 |
# File 'lib/myweatherforecast.rb', line 304 def now() Hourly.new(@forecast, @tlabel) end |
#saturday ⇒ Object Also known as: sat
329 |
# File 'lib/myweatherforecast.rb', line 329 def saturday() day :saturday end |
#sunday ⇒ Object Also known as: sun
330 |
# File 'lib/myweatherforecast.rb', line 330 def sunday() day :sunday end |
#thursday ⇒ Object Also known as: thu
327 |
# File 'lib/myweatherforecast.rb', line 327 def thursday() day :thursday end |
#today ⇒ Object Also known as: this
308 309 310 |
# File 'lib/myweatherforecast.rb', line 308 def today() Daily.new(@forecast, @tlabel) end |
#tomorrow ⇒ Object
320 321 322 |
# File 'lib/myweatherforecast.rb', line 320 def tomorrow() Daily.new(@forecast, @tlabel, 1) end |
#tonight ⇒ Object
314 315 316 |
# File 'lib/myweatherforecast.rb', line 314 def tonight() Daily.new(@forecast, @tlabel).night end |
#tuesday ⇒ Object Also known as: tue
325 |
# File 'lib/myweatherforecast.rb', line 325 def tuesday() day :tuesday end |
#wednesday ⇒ Object Also known as: wed
326 |
# File 'lib/myweatherforecast.rb', line 326 def wednesday() day :wednesday end |