Class: DarkskyAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/darksky-ruby/api.rb

Constant Summary collapse

DARKSKY_URL =
'https://api.darksky.net/'
DARKSKY_PATH_TEMPLATE =
'/forecast/%{key}/%{loc}'
DARKSKY_BLOCK_NAMES =
[:currently, :minutely, :hourly, :daily, :alerts, :flags]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, options: {}) ⇒ DarkskyAPI

Returns a new instance of DarkskyAPI.



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

def initialize(key:, options: {})
  @key = key
  @options = options
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



8
9
10
# File 'lib/darksky-ruby/api.rb', line 8

def key
  @key
end

#latitudeObject

Returns the value of attribute latitude.



8
9
10
# File 'lib/darksky-ruby/api.rb', line 8

def latitude
  @latitude
end

#locationObject

Returns the value of attribute location.



8
9
10
# File 'lib/darksky-ruby/api.rb', line 8

def location
  @location
end

#longitudeObject

Returns the value of attribute longitude.



8
9
10
# File 'lib/darksky-ruby/api.rb', line 8

def longitude
  @longitude
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/darksky-ruby/api.rb', line 8

def options
  @options
end

#timeObject

Returns the value of attribute time.



8
9
10
# File 'lib/darksky-ruby/api.rb', line 8

def time
  @time
end

Instance Method Details

#blocksObject



27
28
29
30
31
32
33
# File 'lib/darksky-ruby/api.rb', line 27

def blocks()
  exc = options[:exclude]
  exc.nil? ? exc = [] : exc = exc.split(',').map{ |n| n.to_sym }
  h = {}
  DARKSKY_BLOCK_NAMES.each { |n| h[n] = !exc.include?(n) }
  return h
end

#blocks=(h) ⇒ Object



35
36
37
38
# File 'lib/darksky-ruby/api.rb', line 35

def blocks=(h)
  exc = DARKSKY_BLOCK_NAMES.select { |n| h[n] == false }
  options[:exclude] = exc.join(',')
end

#forecast(lat: @latitude, lon: @longitude, loc: @location, ts: @time) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
# File 'lib/darksky-ruby/api.rb', line 15

def forecast(lat: @latitude, lon: @longitude, loc: @location, ts: @time)
  loc = "#{lat},#{lon}" if lat && lon
  loc = loc.gsub(/\s+/, '')
  raise ArgumentError, 'No location given to forecast' if loc.nil?
  ts = ts.to_i if ts.class == Time
  request(loc, ts)
end

#include_only(inc = [:currently]) ⇒ Object



40
41
42
43
# File 'lib/darksky-ruby/api.rb', line 40

def include_only(inc = [:currently])
  exc = DARKSKY_BLOCK_NAMES.select { |n| !inc.include?(n) }
  options[:exclude] = exc.join(',')
end

#timemachine(lat: @latitude, lon: @longitude, loc: @location, ts:) ⇒ Object



23
24
25
# File 'lib/darksky-ruby/api.rb', line 23

def timemachine(lat: @latitude, lon: @longitude, loc: @location, ts:)
  forecast(lat: lat, lon: lon, loc: loc, ts: ts)
end