Sha256: 79bb08640a61efb99b78b16880d898ea8c56256903b2f2bddeff4efa6e1e4f53
Contents?: true
Size: 1.87 KB
Versions: 1
Compression:
Stored size: 1.87 KB
Contents
require 'json' module Darksky class API DARKSKY_URL = 'https://api.darksky.net/' DARKSKY_PATH_TEMPLATE = '/forecast/%{key}/%{loc}' DARKSKY_BLOCK_NAMES = [:currently, :minutely, :hourly, :daily, :alerts, :flags] attr_accessor :key, :latitude, :longitude, :location, :time, :options def initialize(key:, options: {}) @key = key @options = options end def forecast(lat: @latitude, lon: @longitude, loc: @location, ts: @time) loc = "#{lat},#{lon}" if lat && lon raise ArgumentError, 'No location given to forecast' if loc.nil? ts = ts.to_i if ts.class == Time request(loc, ts) end def timemachine(lat: @latitude, lon: @longitude, loc: @location, ts:) forecast(lat: lat, lon: lon, loc: loc, ts: ts) end def blocks() ex = options[:exclude] ex.nil? ? ex = [] : ex = ex.split(',').map{ |n| n.to_sym } h = {} DARKSKY_BLOCK_NAMES.each { |n| h[n] = !ex.include?(n) } return h end def blocks=(h) ex = DARKSKY_BLOCK_NAMES.select { |n| h[n] == false } options[:exclude] = ex.join(',') end def param(key, val = nil) end private def request(loc, ts) path = DARKSKY_PATH_TEMPLATE % {key: key, loc: loc} path += ",#{ts}" if ts options.empty? ? o = nil : o = options data = http.get(path: path, params: o) return handle_response_data(data) end def http unless @http @http = HTTP.new(DARKSKY_URL) end return @http end def format_path(path) path = '/' + path unless path.start_with?('/') return path + '.json' end def handle_response_data(data) if data[:code] != 200 Darksky.logger.error("HTTP response error: #{data[:code]}\n#{data[:message]}") return nil end return JSON.parse(data[:body], {symbolize_names: true}) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
darksky-ruby-0.0.1 | lib/darksky-ruby/api.rb |