class DarkSky::Location
Attributes
@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?
@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
@example
location = DarkSky::Location.new [45, -90] location.language #=> :en
@since 0.1.2 @return [Symbol] what language is used
@example
location = DarkSky::Location.new [45, -90] location.location #=> [45, -90]
@since 0.1.0 @return [Array<Numeric>] coordinates of object and data
@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
@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
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
@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
@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
@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
@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
@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
@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
@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
@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