Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require 'tlaw' | ||
|
||
module Reality | ||
module Describers | ||
class DarkSky < Abstract::Base | ||
def initialize(key) | ||
@api = API.new(api_key: key) | ||
end | ||
|
||
def perform_query(params) | ||
lat, lng = case params | ||
when Array | ||
params | ||
when Geo::Coord | ||
params.latlng | ||
when Entity | ||
(params['coordinates'] || params['coordinate location'])&.latlng | ||
when /^\d+(?:\.\d+)?[,\| ;]\d+(?:\.\d+)?$/ | ||
params.split(/[,\| ;]/).map(&:to_f) | ||
end | ||
|
||
@api.current(lat, lng).yield_self { |response| | ||
eid = response['currently.time'] # TODO: Find what to use as ID. | ||
response.map { |key, value| | ||
obs(eid, key, value) | ||
} | ||
}.yield_self(&method(:make_entities)) | ||
end | ||
|
||
private | ||
|
||
def prefix | ||
'darksky' | ||
end | ||
|
||
class API < TLAW::API | ||
define do | ||
base 'https://api.darksky.net/forecast/{api_key}' | ||
|
||
param :api_key, required: true | ||
|
||
endpoint :current, '/{lat},{lng}?exclude=hourly,flags,daily,minutely' do | ||
param :lat, :to_f, required: true, desc: 'Latitude' | ||
param :lng, :to_f, required: true, desc: 'Longitude' | ||
end | ||
|
||
endpoint :forecast, '/{lat},{lng},{timestamp}?exclude=hourly,flags,daily,minutely' do | ||
param :lat, :to_f, required: true, desc: 'Latitude' | ||
param :lng, :to_f, required: true, desc: 'Longitude' | ||
param :timestamp, :to_i, required: true, desc: 'Timestamp' | ||
end | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
|
||
# TODO: Create ENV['DARK_SKY_APPID'] | ||
# Reality.describers['darksky'] = Reality.describers['dark_sky'] = Reality::Describers::DarkSky.new(ENV['DARK_SKY_APPID']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'bundler/setup' | ||
$LOAD_PATH.unshift 'lib' | ||
require 'reality' | ||
require 'reality/describers/dark_sky' | ||
module Reality::Describers; end | ||
require 'pp' | ||
|
||
SECRET_KEY = '19590209ae532860d1b9b2efcbcf5dcb' | ||
|
||
lat = ARGV[0] | ||
lng = ARGV[1] | ||
|
||
source = Reality::Describers::DarkSky.new(SECRET_KEY) | ||
|
||
pp response = source.perform_query(ARGV) | ||
pp response.first.observations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters