Class: DarkSky::Location
- Inherits:
-
Object
- Object
- DarkSky::Location
- Defined in:
- lib/darksky-api/day.rb,
lib/darksky-api/current.rb,
lib/darksky-api/location.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#cache_duration ⇒ Numeric
How long is data valid for before a new request is made?.
-
#current ⇒ Current
readonly
Class containing data for current time and location.
-
#language ⇒ Symbol
readonly
What language is used.
-
#location ⇒ Array<Numeric>
readonly
Coordinates of object and data.
-
#units ⇒ Symbol
readonly
What unit system is being used.
Instance Method Summary collapse
-
#full_data ⇒ Hash
update cache if necessary and get latest data.
-
#in_2_days ⇒ Day
Class containing data for given day.
-
#in_3_days ⇒ Day
Class containing data for given day.
-
#in_4_days ⇒ Day
Class containing data for given day.
-
#in_5_days ⇒ Day
Class containing data for given day.
-
#in_6_days ⇒ Day
Class containing data for given day.
-
#in_7_days ⇒ Day
Class containing data for given day.
-
#initialize(location = [0, 0], cache_duration: 300, units: :auto, language: :en, prefetch: false) ⇒ Location
constructor
A new instance of Location.
-
#today ⇒ Day
(also: #in_0_days)
Class containing data for given day.
-
#tomorrow ⇒ Day
(also: #in_1_day, #in_1_days)
Class containing data for given day.
Constructor Details
#initialize(location = [0, 0], cache_duration: 300, units: :auto, language: :en, prefetch: false) ⇒ Location
Returns a new instance of Location.
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 |
# File 'lib/darksky-api/location.rb', line 50 def initialize( location = [0, 0], cache_duration: 300, units: :auto, language: :en, prefetch: false ) # initial value to avoid errors @cache_time = 1 # initialize instance variables from args & kwargs @location = location @cache_duration = cache_duration # in seconds @language = language.to_sym @units = units.to_sym # aliases for some unit systems @units = :uk2 if @units == :uk @units = :ca if @units == :canada # initialize classes for namespace @current = Current.new self # perform API request if prefetch is true full_data if prefetch end |
Instance Attribute Details
#cache_duration ⇒ Numeric
Returns how long is data valid for before a new request is made?.
43 44 45 |
# File 'lib/darksky-api/location.rb', line 43 def cache_duration @cache_duration end |
#current ⇒ Current (readonly)
Returns class containing data for current time and location.
18 19 20 |
# File 'lib/darksky-api/location.rb', line 18 def current @current end |
#language ⇒ Symbol (readonly)
Returns what language is used.
32 33 34 |
# File 'lib/darksky-api/location.rb', line 32 def language @language end |
#location ⇒ Array<Numeric> (readonly)
Returns coordinates of object and data.
11 12 13 |
# File 'lib/darksky-api/location.rb', line 11 def location @location end |
#units ⇒ Symbol (readonly)
Returns what unit system is being used.
25 26 27 |
# File 'lib/darksky-api/location.rb', line 25 def units @units end |
Instance Method Details
#full_data ⇒ Hash
update cache if necessary and get latest data
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/darksky-api/location.rb', line 83 def full_data if (Time.now - @cache_time).to_i >= @cache_duration response = RestClient.get "https://api.darksky.net/forecast/#{DarkSky.key}/#{@location.join ','}", params: { units: @units, lang: @language } @data = JSON.parse response.body, symbolize_names: true @cache_time = Time.now end @data end |
#in_2_days ⇒ Day
Returns class containing data for given day.
286 287 288 |
# File 'lib/darksky-api/day.rb', line 286 def in_2_days Day.new self, 2 end |
#in_3_days ⇒ Day
Returns class containing data for given day.
295 296 297 |
# File 'lib/darksky-api/day.rb', line 295 def in_3_days Day.new self, 3 end |
#in_4_days ⇒ Day
Returns class containing data for given day.
304 305 306 |
# File 'lib/darksky-api/day.rb', line 304 def in_4_days Day.new self, 4 end |
#in_5_days ⇒ Day
Returns class containing data for given day.
313 314 315 |
# File 'lib/darksky-api/day.rb', line 313 def in_5_days Day.new self, 5 end |
#in_6_days ⇒ Day
Returns class containing data for given day.
322 323 324 |
# File 'lib/darksky-api/day.rb', line 322 def in_6_days Day.new self, 6 end |
#in_7_days ⇒ Day
Returns class containing data for given day.
331 332 333 |
# File 'lib/darksky-api/day.rb', line 331 def in_7_days Day.new self, 7 end |