darksky-ruby 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/darksky +6 -6
- data/darksky-ruby.gemspec +1 -1
- data/lib/darksky-ruby/api.rb +56 -59
- data/lib/darksky-ruby/http.rb +86 -21
- data/lib/darksky-ruby/version.rb +1 -1
- data/lib/darksky-ruby.rb +0 -1
- metadata +4 -5
- data/lib/darksky-ruby/logger.rb +0 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62a900ed0c6248ca0f923c8d21d9313d91710420
|
4
|
+
data.tar.gz: 87a437d159fca922f16601a92dbb947e841e93b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b2b0f2b5e01e70b14512cafe490a317e8c18550aa13a996a27ef8f327a4d08eace4a8616d65d206128e55c4fe013665ffd132ff8d52b941161a28f6405a352b
|
7
|
+
data.tar.gz: 91fe232ff29d2d4e3fe70e6e08c90a64aa20b0ce07ffecc4d548783616befe65652d3e036cd9b873e60638b10b69eebe0e065bee9575174b6b019e56ea6b1f45
|
data/bin/darksky
CHANGED
@@ -13,15 +13,15 @@ opts = Trollop::options do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
if opts[:log_given]
|
16
|
-
|
17
|
-
|
16
|
+
Neko.logger = Logger.new(opts[:log])
|
17
|
+
Neko.logger.level = Logger::WARN
|
18
18
|
end
|
19
19
|
|
20
20
|
if opts[:verbose]
|
21
|
-
|
22
|
-
|
21
|
+
Neko.logger = Logger.new(STDOUT) unless opts[:log_given]
|
22
|
+
Neko.logger.level = Logger::DEBUG
|
23
23
|
end
|
24
|
-
log =
|
24
|
+
log = Neko.logger
|
25
25
|
|
26
26
|
log.debug("Command line arguments: #{opts}")
|
27
27
|
|
@@ -30,7 +30,7 @@ loc ||= ARGV.shift
|
|
30
30
|
|
31
31
|
Trollop::die :loc, "is missing" if loc.nil?
|
32
32
|
|
33
|
-
api =
|
33
|
+
api = DarkskyAPI.new(key: opts[:key])
|
34
34
|
api.blocks = {minutely: false, hourly: false, daily: false, alerts: false, flags: false}
|
35
35
|
|
36
36
|
if opts[:time_given]
|
data/darksky-ruby.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.authors = ['Ken J.']
|
9
9
|
s.email = ['kenjij@gmail.com']
|
10
10
|
s.summary = %q{Pure simple Ruby based Darksky REST library}
|
11
|
-
s.description = %q{Darksky library written in pure Ruby without external
|
11
|
+
s.description = %q{Darksky library written in pure Ruby without external dependency.}
|
12
12
|
s.homepage = 'https://github.com/kenjij/darksky-ruby'
|
13
13
|
s.license = 'MIT'
|
14
14
|
|
data/lib/darksky-ruby/api.rb
CHANGED
@@ -1,77 +1,74 @@
|
|
1
1
|
require 'json'
|
2
2
|
|
3
|
-
|
3
|
+
class DarkskyAPI
|
4
|
+
DARKSKY_URL = 'https://api.darksky.net/'
|
5
|
+
DARKSKY_PATH_TEMPLATE = '/forecast/%{key}/%{loc}'
|
6
|
+
DARKSKY_BLOCK_NAMES = [:currently, :minutely, :hourly, :daily, :alerts, :flags]
|
4
7
|
|
5
|
-
|
8
|
+
attr_accessor :key, :latitude, :longitude, :location, :time, :options
|
6
9
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
attr_accessor :key, :latitude, :longitude, :location, :time, :options
|
12
|
-
|
13
|
-
def initialize(key:, options: {})
|
14
|
-
@key = key
|
15
|
-
@options = options
|
16
|
-
end
|
10
|
+
def initialize(key:, options: {})
|
11
|
+
@key = key
|
12
|
+
@options = options
|
13
|
+
end
|
17
14
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
15
|
+
def forecast(lat: @latitude, lon: @longitude, loc: @location, ts: @time)
|
16
|
+
loc = "#{lat},#{lon}" if lat && lon
|
17
|
+
loc = loc.gsub(/\s+/, '')
|
18
|
+
raise ArgumentError, 'No location given to forecast' if loc.nil?
|
19
|
+
ts = ts.to_i if ts.class == Time
|
20
|
+
request(loc, ts)
|
21
|
+
end
|
24
22
|
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
def timemachine(lat: @latitude, lon: @longitude, loc: @location, ts:)
|
24
|
+
forecast(lat: lat, lon: lon, loc: loc, ts: ts)
|
25
|
+
end
|
28
26
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
27
|
+
def blocks()
|
28
|
+
exc = options[:exclude]
|
29
|
+
exc.nil? ? exc = [] : exc = exc.split(',').map{ |n| n.to_sym }
|
30
|
+
h = {}
|
31
|
+
DARKSKY_BLOCK_NAMES.each { |n| h[n] = !exc.include?(n) }
|
32
|
+
return h
|
33
|
+
end
|
36
34
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
35
|
+
def blocks=(h)
|
36
|
+
exc = DARKSKY_BLOCK_NAMES.select { |n| h[n] == false }
|
37
|
+
options[:exclude] = exc.join(',')
|
38
|
+
end
|
41
39
|
|
42
|
-
|
43
|
-
|
40
|
+
def include_only(inc = [:currently])
|
41
|
+
exc = DARKSKY_BLOCK_NAMES.select { |n| !inc.include?(n) }
|
42
|
+
options[:exclude] = exc.join(',')
|
43
|
+
end
|
44
44
|
|
45
|
-
|
45
|
+
private
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
47
|
+
def request(loc, ts)
|
48
|
+
path = DARKSKY_PATH_TEMPLATE % {key: key, loc: loc}
|
49
|
+
path += ",#{ts}" if ts
|
50
|
+
options.empty? ? o = nil : o = options
|
51
|
+
data = http.get(path: path, params: o)
|
52
|
+
return handle_response_data(data)
|
53
|
+
end
|
54
54
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
end
|
59
|
-
return @http
|
55
|
+
def http
|
56
|
+
unless @http
|
57
|
+
@http = Neko::HTTP.new(DARKSKY_URL)
|
60
58
|
end
|
59
|
+
return @http
|
60
|
+
end
|
61
61
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
62
|
+
def format_path(path)
|
63
|
+
path = '/' + path unless path.start_with?('/')
|
64
|
+
return path + '.json'
|
65
|
+
end
|
66
66
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
end
|
72
|
-
return JSON.parse(data[:body], {symbolize_names: true})
|
67
|
+
def handle_response_data(data)
|
68
|
+
if data[:code] != 200
|
69
|
+
Neko.logger.error("HTTP response error: #{data[:code]}\n#{data[:message]}")
|
70
|
+
return nil
|
73
71
|
end
|
74
|
-
|
72
|
+
return JSON.parse(data[:body], {symbolize_names: true})
|
75
73
|
end
|
76
|
-
|
77
74
|
end
|
data/lib/darksky-ruby/http.rb
CHANGED
@@ -1,13 +1,27 @@
|
|
1
|
+
# NekoHTTP - Pure Ruby HTTP client using net/http
|
2
|
+
#
|
3
|
+
# v.20190829
|
4
|
+
|
1
5
|
require 'net/http'
|
2
6
|
require 'openssl'
|
7
|
+
require 'logger'
|
3
8
|
|
9
|
+
module Neko
|
10
|
+
def self.logger=(logger)
|
11
|
+
@logger = logger
|
12
|
+
end
|
4
13
|
|
5
|
-
|
14
|
+
def self.logger
|
15
|
+
@logger ||= NullLogger.new()
|
16
|
+
end
|
6
17
|
|
7
18
|
class HTTP
|
8
|
-
|
9
19
|
METHOD_HTTP_CLASS = {
|
10
|
-
get: Net::HTTP::Get
|
20
|
+
get: Net::HTTP::Get,
|
21
|
+
put: Net::HTTP::Put,
|
22
|
+
patch: Net::HTTP::Patch,
|
23
|
+
post: Net::HTTP::Post,
|
24
|
+
delete: Net::HTTP::Delete
|
11
25
|
}
|
12
26
|
|
13
27
|
def self.get(url, params)
|
@@ -17,10 +31,18 @@ module Darksky
|
|
17
31
|
return data
|
18
32
|
end
|
19
33
|
|
34
|
+
def self.post_form(url, params)
|
35
|
+
h = HTTP.new(url)
|
36
|
+
data = h.post(params: params)
|
37
|
+
h.close
|
38
|
+
return data
|
39
|
+
end
|
40
|
+
|
20
41
|
attr_reader :init_uri, :http
|
21
|
-
attr_accessor :headers
|
42
|
+
attr_accessor :logger, :headers
|
22
43
|
|
23
44
|
def initialize(url, hdrs = nil)
|
45
|
+
@logger = Neko.logger
|
24
46
|
@init_uri = URI(url)
|
25
47
|
raise ArgumentError, 'Invalid URL' unless @init_uri.class <= URI::HTTP
|
26
48
|
@http = Net::HTTP.new(init_uri.host, init_uri.port)
|
@@ -33,6 +55,22 @@ module Darksky
|
|
33
55
|
return operate(__method__, path: path, params: params, query: query)
|
34
56
|
end
|
35
57
|
|
58
|
+
def post(path: nil, params: nil, body: nil, query: nil)
|
59
|
+
return operate(__method__, path: path, params: params, body: body, query: query)
|
60
|
+
end
|
61
|
+
|
62
|
+
def put(path: nil, params: nil, body: nil, query: nil)
|
63
|
+
return operate(__method__, path: path, params: params, body: body, query: query)
|
64
|
+
end
|
65
|
+
|
66
|
+
def patch(path: nil, params: nil, body: nil, query: nil)
|
67
|
+
return operate(__method__, path: path, params: params, body: body, query: query)
|
68
|
+
end
|
69
|
+
|
70
|
+
def delete(path: nil, params: nil, query: nil)
|
71
|
+
return operate(__method__, path: path, params: params, query: query)
|
72
|
+
end
|
73
|
+
|
36
74
|
def close
|
37
75
|
http.finish if http.started?
|
38
76
|
end
|
@@ -41,12 +79,26 @@ module Darksky
|
|
41
79
|
|
42
80
|
def operate(method, path: nil, params: nil, body: nil, query: nil)
|
43
81
|
uri = uri_with_path(path)
|
44
|
-
|
45
|
-
|
46
|
-
|
82
|
+
case method
|
83
|
+
when :get, :delete
|
84
|
+
if params
|
85
|
+
query = URI.encode_www_form(params)
|
86
|
+
logger.info('Created urlencoded query from params')
|
87
|
+
end
|
88
|
+
uri.query = query
|
89
|
+
req = METHOD_HTTP_CLASS[method].new(uri)
|
90
|
+
when :put, :patch, :post
|
91
|
+
uri.query = query if query
|
92
|
+
req = METHOD_HTTP_CLASS[method].new(uri)
|
93
|
+
if params
|
94
|
+
req.form_data = params
|
95
|
+
logger.info('Created form data from params')
|
96
|
+
elsif body
|
97
|
+
req.body = body
|
98
|
+
end
|
99
|
+
else
|
100
|
+
return nil
|
47
101
|
end
|
48
|
-
uri.query = query
|
49
|
-
req = METHOD_HTTP_CLASS[method].new(uri)
|
50
102
|
data = send(req)
|
51
103
|
data = redirect(method, uri, params: params, body: body, query: query) if data.class <= URI::HTTP
|
52
104
|
return data
|
@@ -61,12 +113,12 @@ module Darksky
|
|
61
113
|
def send(req)
|
62
114
|
inject_headers_to(req)
|
63
115
|
unless http.started?
|
64
|
-
|
116
|
+
logger.info('HTTP session not started; starting now')
|
65
117
|
http.start
|
66
|
-
|
118
|
+
logger.debug("Opened connection to #{http.address}:#{http.port}")
|
67
119
|
end
|
68
|
-
|
69
|
-
|
120
|
+
logger.debug("Sending HTTP #{req.method} request to #{req.path}")
|
121
|
+
logger.debug("Body size: #{req.body.length}") if req.request_body_permitted?
|
70
122
|
res = http.request(req)
|
71
123
|
return handle_response(res)
|
72
124
|
end
|
@@ -76,21 +128,21 @@ module Darksky
|
|
76
128
|
headers.each do |k, v|
|
77
129
|
req[k] = v
|
78
130
|
end
|
79
|
-
|
131
|
+
logger.info('Header injected into HTTP request header')
|
80
132
|
end
|
81
133
|
|
82
134
|
def handle_response(res)
|
83
135
|
if res.connection_close?
|
84
|
-
|
136
|
+
logger.info('HTTP response header says connection close; closing session now')
|
85
137
|
close
|
86
138
|
end
|
87
139
|
case res
|
88
140
|
when Net::HTTPRedirection
|
89
|
-
|
141
|
+
logger.info('HTTP response was a redirect')
|
90
142
|
data = URI(res['Location'])
|
91
143
|
if data.class == URI::Generic
|
92
144
|
data = uri_with_path(res['Location'])
|
93
|
-
|
145
|
+
logger.debug("Full URI object built for local redirect with path: #{data.path}")
|
94
146
|
end
|
95
147
|
# when Net::HTTPSuccess
|
96
148
|
# when Net::HTTPClientError
|
@@ -108,16 +160,29 @@ module Darksky
|
|
108
160
|
|
109
161
|
def redirect(method, uri, params: nil, body: nil, query: nil)
|
110
162
|
if uri.host == init_uri.host && uri.port == init_uri.port
|
111
|
-
|
163
|
+
logger.info("Local #{method.upcase} redirect, reusing HTTP session")
|
112
164
|
new_http = http
|
113
165
|
else
|
114
|
-
|
166
|
+
logger.info("External #{method.upcase} redirect, spawning new HTTP object")
|
115
167
|
new_http = HTTP.new("#{uri.scheme}://#{uri.host}#{uri.path}", headers)
|
116
168
|
end
|
117
|
-
|
169
|
+
case method
|
170
|
+
when :get, :delete
|
171
|
+
data = operate(method, uri, params: params, query: query)
|
172
|
+
when :put, :patch, :post
|
173
|
+
data = new_http.public_send(method, uri, params: params, body: body, query: query)
|
174
|
+
else
|
175
|
+
data = nil
|
176
|
+
end
|
118
177
|
return data
|
119
178
|
end
|
120
|
-
|
121
179
|
end
|
122
180
|
|
181
|
+
class NullLogger < Logger
|
182
|
+
def initialize(*args)
|
183
|
+
end
|
184
|
+
|
185
|
+
def add(*args, &block)
|
186
|
+
end
|
187
|
+
end
|
123
188
|
end
|
data/lib/darksky-ruby/version.rb
CHANGED
data/lib/darksky-ruby.rb
CHANGED
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: darksky-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ken J.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-09-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: Darksky library written in pure Ruby without external
|
13
|
+
description: Darksky library written in pure Ruby without external dependency.
|
14
14
|
email:
|
15
15
|
- kenjij@gmail.com
|
16
16
|
executables:
|
@@ -23,7 +23,6 @@ files:
|
|
23
23
|
- lib/darksky-ruby.rb
|
24
24
|
- lib/darksky-ruby/api.rb
|
25
25
|
- lib/darksky-ruby/http.rb
|
26
|
-
- lib/darksky-ruby/logger.rb
|
27
26
|
- lib/darksky-ruby/trollop.rb
|
28
27
|
- lib/darksky-ruby/version.rb
|
29
28
|
homepage: https://github.com/kenjij/darksky-ruby
|
@@ -46,7 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
46
45
|
version: '0'
|
47
46
|
requirements: []
|
48
47
|
rubyforge_project:
|
49
|
-
rubygems_version: 2.6.
|
48
|
+
rubygems_version: 2.6.14
|
50
49
|
signing_key:
|
51
50
|
specification_version: 4
|
52
51
|
summary: Pure simple Ruby based Darksky REST library
|
data/lib/darksky-ruby/logger.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'logger'
|
2
|
-
|
3
|
-
|
4
|
-
module Darksky
|
5
|
-
|
6
|
-
def self.logger=(logger)
|
7
|
-
@logger = logger
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.logger
|
11
|
-
@logger ||= NullLogger.new()
|
12
|
-
end
|
13
|
-
|
14
|
-
class NullLogger < Logger
|
15
|
-
|
16
|
-
def initialize(*args)
|
17
|
-
end
|
18
|
-
|
19
|
-
def add(*args, &block)
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|