class DarkSkyAPI
Constants
- DARKSKY_BLOCK_NAMES
- DARKSKY_PATH_TEMPLATE
- DARKSKY_URL
- VERSION
Attributes
key[RW]
latitude[RW]
location[RW]
longitude[RW]
options[RW]
time[RW]
Public Class Methods
new(key:, options: {})
click to toggle source
# File lib/darksky-ruby/api.rb, line 16 def initialize(key:, options: {}) @key = key @options = options end
Public Instance Methods
blocks()
click to toggle source
# File lib/darksky-ruby/api.rb, line 33 def blocks exc = options[:exclude] exc = (exc.nil? ? [] : exc.split(',').map { |n| n.to_sym }) h = {} DARKSKY_BLOCK_NAMES.each { |n| h[n] = !exc.include?(n) } h end
blocks=(h)
click to toggle source
# File lib/darksky-ruby/api.rb, line 41 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)
click to toggle source
# File lib/darksky-ruby/api.rb, line 21 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])
click to toggle source
# File lib/darksky-ruby/api.rb, line 46 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:)
click to toggle source
# File lib/darksky-ruby/api.rb, line 29 def timemachine(lat: @latitude, lon: @longitude, loc: @location, ts:) forecast(lat: lat, lon: lon, loc: loc, ts: ts) end
Private Instance Methods
format_path(path)
click to toggle source
# File lib/darksky-ruby/api.rb, line 65 def format_path(path) path = '/' + path unless path.start_with?('/') path + '.json' end
handle_response_data(data)
click to toggle source
# File lib/darksky-ruby/api.rb, line 70 def handle_response_data(data) if data[:code] != 200 msg = "HTTP response error: #{data[:code]}\n#{data[:message]}" Neko.logger.error(msg) return nil end JSON.parse(data[:body], symbolize_names: true) end
http()
click to toggle source
# File lib/darksky-ruby/api.rb, line 61 def http @http ||= Neko::HTTP.new(DARKSKY_URL) end
request(loc, ts)
click to toggle source
# File lib/darksky-ruby/api.rb, line 53 def request(loc, ts) path = DARKSKY_PATH_TEMPLATE % {key: key, loc: loc} path += ",#{ts}" if ts o = (options.empty? ? nil : options) data = http.get(path: path, params: o) handle_response_data(data) end