Class: DarkSky::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/darksky-api/day.rb,
lib/darksky-api/current.rb,
lib/darksky-api/location.rb

Defined Under Namespace

Classes: Current, Day

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location = [0, 0], cache_duration: 300, units: :auto, language: :en, prefetch: false) ⇒ Location

Returns a new instance of Location.

Parameters:

  • location ([Numeric, Numeric]) (defaults to: [0, 0])

    coordinates to get data of

  • cache_duration (Numeric) (defaults to: 300)

    requests within this many seconds will be parsed on existing data

  • units (Symbol | String) (defaults to: :auto)

    what unit system to use

  • language (Symbol | String) (defaults to: :en)

    what language to return results in

  • prefetch (Boolean) (defaults to: false)

    immediately perform an API request upon initialization?



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_durationNumeric

Returns how long is data valid for before a new request is made?.

Examples:

getter

location = DarkSky::Location.new [45, -90]
location.cache_duration #=> 300

setter

location = DarkSky::Location.new [45, -90]
location.cache_duration = 600
location.cache_duration #=> 600

Returns:

  • (Numeric)

    how long is data valid for before a new request is made?

Since:

  • 0.1.0



43
44
45
# File 'lib/darksky-api/location.rb', line 43

def cache_duration
  @cache_duration
end

#currentCurrent (readonly)

Returns class containing data for current time and location.

Examples:

location = DarkSky::Location.new [45, -90]
location.current #=> DarkSky::Location::Current

Returns:

  • (Current)

    class containing data for current time and location

Since:

  • 0.1.0



18
19
20
# File 'lib/darksky-api/location.rb', line 18

def current
  @current
end

#languageSymbol (readonly)

Returns what language is used.

Examples:

location = DarkSky::Location.new [45, -90]
location.language #=> :en

Returns:

  • (Symbol)

    what language is used

Since:

  • 0.1.2



32
33
34
# File 'lib/darksky-api/location.rb', line 32

def language
  @language
end

#locationArray<Numeric> (readonly)

Returns coordinates of object and data.

Examples:

location = DarkSky::Location.new [45, -90]
location.location #=> [45, -90]

Returns:

  • (Array<Numeric>)

    coordinates of object and data

Since:

  • 0.1.0



11
12
13
# File 'lib/darksky-api/location.rb', line 11

def location
  @location
end

#unitsSymbol (readonly)

Returns what unit system is being used.

Examples:

location = DarkSky::Location.new [45, -90]
location.units #=> :auto

Returns:

  • (Symbol)

    what unit system is being used

Since:

  • 0.1.2



25
26
27
# File 'lib/darksky-api/location.rb', line 25

def units
  @units
end

Instance Method Details

#full_dataHash

update cache if necessary and get latest data

Examples:

location = DarkSky::Location.new [45, -90]
location.full_data

Returns:

  • (Hash)

    raw data (in full) from DarkSky

Since:

  • 0.1.0



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_daysDay

Returns class containing data for given day.

Examples:

location = DarkSky::Location.new [45, -90]
location.in_2_days #=> DarkSky::Location::Day

Returns:

  • (Day)

    class containing data for given day

Since:

  • 0.1.3



286
287
288
# File 'lib/darksky-api/day.rb', line 286

def in_2_days
  Day.new self, 2
end

#in_3_daysDay

Returns class containing data for given day.

Examples:

location = DarkSky::Location.new [45, -90]
location.in_3_days #=> DarkSky::Location::Day

Returns:

  • (Day)

    class containing data for given day

Since:

  • 0.1.3



295
296
297
# File 'lib/darksky-api/day.rb', line 295

def in_3_days
  Day.new self, 3
end

#in_4_daysDay

Returns class containing data for given day.

Examples:

location = DarkSky::Location.new [45, -90]
location.in_4_days #=> DarkSky::Location::Day

Returns:

  • (Day)

    class containing data for given day

Since:

  • 0.1.3



304
305
306
# File 'lib/darksky-api/day.rb', line 304

def in_4_days
  Day.new self, 4
end

#in_5_daysDay

Returns class containing data for given day.

Examples:

location = DarkSky::Location.new [45, -90]
location.in_5_days #=> DarkSky::Location::Day

Returns:

  • (Day)

    class containing data for given day

Since:

  • 0.1.3



313
314
315
# File 'lib/darksky-api/day.rb', line 313

def in_5_days
  Day.new self, 5
end

#in_6_daysDay

Returns class containing data for given day.

Examples:

location = DarkSky::Location.new [45, -90]
location.in_6_days #=> DarkSky::Location::Day

Returns:

  • (Day)

    class containing data for given day

Since:

  • 0.1.3



322
323
324
# File 'lib/darksky-api/day.rb', line 322

def in_6_days
  Day.new self, 6
end

#in_7_daysDay

Returns class containing data for given day.

Examples:

location = DarkSky::Location.new [45, -90]
location.in_7_days #=> DarkSky::Location::Day

Returns:

  • (Day)

    class containing data for given day

Since:

  • 0.1.3



331
332
333
# File 'lib/darksky-api/day.rb', line 331

def in_7_days
  Day.new self, 7
end

#todayDay Also known as: in_0_days

Returns class containing data for given day.

Examples:

location = DarkSky::Location.new [45, -90]
location.today #=> DarkSky::Location::Day

Returns:

  • (Day)

    class containing data for given day

Since:

  • 0.1.3



265
266
267
# File 'lib/darksky-api/day.rb', line 265

def today
  Day.new self, 0
end

#tomorrowDay Also known as: in_1_day, in_1_days

Returns class containing data for given day.

Examples:

location = DarkSky::Location.new [45, -90]
location.tomorrow #=> DarkSky::Location::Day

Returns:

  • (Day)

    class containing data for given day

Since:

  • 0.1.3



275
276
277
# File 'lib/darksky-api/day.rb', line 275

def tomorrow
  Day.new self, 1
end