class DarkSky::Location

Attributes

cache_duration[RW]

@example getter

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

@example setter

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

@since 0.1.0 @return [Numeric] how long is data valid for before a new request is made?

current[R]

@example

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

@since 0.1.0 @return [Current] class containing data for current time and location

language[R]

@example

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

@since 0.1.2 @return [Symbol] what language is used

location[R]

@example

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

@since 0.1.0 @return [Array<Numeric>] coordinates of object and data

units[R]

@example

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

@since 0.1.2 @return [Symbol] what unit system is being used

Public Class Methods

new( location = [0, 0], cache_duration: 300, units: :auto, language: :en, prefetch: false ) click to toggle source

@param location [[Numeric, Numeric]] coordinates to get data of @param [Numeric] cache_duration requests within this many seconds will be parsed on existing data @param [Symbol | String] units what unit system to use @param [Symbol | String] language what language to return results in @param [Boolean] prefetch immediately perform an API request upon initialization?

# 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

Public Instance Methods

full_data() click to toggle source

update cache if necessary and get latest data @example

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

@since 0.1.0 @return [Hash] raw data (in full) from DarkSky

# 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_0_days()
Alias for: today
in_1_day()
Alias for: tomorrow
in_1_days()
Alias for: tomorrow
in_2_days() click to toggle source

@example

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

@since 0.1.3 @return [Day] class containing data for given day

# File lib/darksky-api/day.rb, line 286
def in_2_days
  Day.new self, 2
end
in_3_days() click to toggle source

@example

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

@since 0.1.3 @return [Day] class containing data for given day

# File lib/darksky-api/day.rb, line 295
def in_3_days
  Day.new self, 3
end
in_4_days() click to toggle source

@example

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

@since 0.1.3 @return [Day] class containing data for given day

# File lib/darksky-api/day.rb, line 304
def in_4_days
  Day.new self, 4
end
in_5_days() click to toggle source

@example

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

@since 0.1.3 @return [Day] class containing data for given day

# File lib/darksky-api/day.rb, line 313
def in_5_days
  Day.new self, 5
end
in_6_days() click to toggle source

@example

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

@since 0.1.3 @return [Day] class containing data for given day

# File lib/darksky-api/day.rb, line 322
def in_6_days
  Day.new self, 6
end
in_7_days() click to toggle source

@example

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

@since 0.1.3 @return [Day] class containing data for given day

# File lib/darksky-api/day.rb, line 331
def in_7_days
  Day.new self, 7
end
today() click to toggle source

@example

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

@since 0.1.3 @return [Day] class containing data for given day

# File lib/darksky-api/day.rb, line 265
def today
  Day.new self, 0
end
Also aliased as: in_0_days
tomorrow() click to toggle source

@example

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

@since 0.1.3 @return [Day] class containing data for given day

# File lib/darksky-api/day.rb, line 275
def tomorrow
  Day.new self, 1
end
Also aliased as: in_1_day, in_1_days