darksky-ruby 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2de496ac9a489ecdb83e761649f6540bba2f94f3
4
- data.tar.gz: aaf3e305a49d24c740c4ad9a6114b25cbd96b214
3
+ metadata.gz: 62a900ed0c6248ca0f923c8d21d9313d91710420
4
+ data.tar.gz: 87a437d159fca922f16601a92dbb947e841e93b7
5
5
  SHA512:
6
- metadata.gz: c045f53b13b0e9c514557df793c13b5f148910bb544969178373ff3cfe3d8246d1beaa7094a62ff83fc7e74b024de5efafb6b70b20ac4673a0e76179f6468de5
7
- data.tar.gz: bf159e9875b718220b04094347ec0a3c6a964d6368f5c9cf6e622bb081d61ed4962e1d9077ca01d08d18609b03063893625b7feefdbb5e8af08f21a162807d13
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
- Darksky.logger = Logger.new(opts[:log])
17
- Darksky.logger.level = Logger::WARN
16
+ Neko.logger = Logger.new(opts[:log])
17
+ Neko.logger.level = Logger::WARN
18
18
  end
19
19
 
20
20
  if opts[:verbose]
21
- Darksky.logger = Logger.new(STDOUT) unless opts[:log_given]
22
- Darksky.logger.level = Logger::DEBUG
21
+ Neko.logger = Logger.new(STDOUT) unless opts[:log_given]
22
+ Neko.logger.level = Logger::DEBUG
23
23
  end
24
- log = Darksky.logger
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 = Darksky::API.new(key: opts[:key])
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 dependancy.}
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
 
@@ -1,77 +1,74 @@
1
1
  require 'json'
2
2
 
3
- module Darksky
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
- class API
8
+ attr_accessor :key, :latitude, :longitude, :location, :time, :options
6
9
 
7
- DARKSKY_URL = 'https://api.darksky.net/'
8
- DARKSKY_PATH_TEMPLATE = '/forecast/%{key}/%{loc}'
9
- DARKSKY_BLOCK_NAMES = [:currently, :minutely, :hourly, :daily, :alerts, :flags]
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
- def forecast(lat: @latitude, lon: @longitude, loc: @location, ts: @time)
19
- loc = "#{lat},#{lon}" if lat && lon
20
- raise ArgumentError, 'No location given to forecast' if loc.nil?
21
- ts = ts.to_i if ts.class == Time
22
- request(loc, ts)
23
- end
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
- def timemachine(lat: @latitude, lon: @longitude, loc: @location, ts:)
26
- forecast(lat: lat, lon: lon, loc: loc, ts: ts)
27
- end
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
- def blocks()
30
- ex = options[:exclude]
31
- ex.nil? ? ex = [] : ex = ex.split(',').map{ |n| n.to_sym }
32
- h = {}
33
- DARKSKY_BLOCK_NAMES.each { |n| h[n] = !ex.include?(n) }
34
- return h
35
- end
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
- def blocks=(h)
38
- ex = DARKSKY_BLOCK_NAMES.select { |n| h[n] == false }
39
- options[:exclude] = ex.join(',')
40
- end
35
+ def blocks=(h)
36
+ exc = DARKSKY_BLOCK_NAMES.select { |n| h[n] == false }
37
+ options[:exclude] = exc.join(',')
38
+ end
41
39
 
42
- def param(key, val = nil)
43
- end
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
- private
45
+ private
46
46
 
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
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
- def http
56
- unless @http
57
- @http = HTTP.new(DARKSKY_URL)
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
- def format_path(path)
63
- path = '/' + path unless path.start_with?('/')
64
- return path + '.json'
65
- end
62
+ def format_path(path)
63
+ path = '/' + path unless path.start_with?('/')
64
+ return path + '.json'
65
+ end
66
66
 
67
- def handle_response_data(data)
68
- if data[:code] != 200
69
- Darksky.logger.error("HTTP response error: #{data[:code]}\n#{data[:message]}")
70
- return nil
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
@@ -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
- module Darksky
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
- if params
45
- query = URI.encode_www_form(params)
46
- Darksky.logger.info('Created urlencoded query from params')
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
- Darksky.logger.info('HTTP session not started; starting now')
116
+ logger.info('HTTP session not started; starting now')
65
117
  http.start
66
- Darksky.logger.debug("Opened connection to #{http.address}:#{http.port}")
118
+ logger.debug("Opened connection to #{http.address}:#{http.port}")
67
119
  end
68
- Darksky.logger.debug("Sending HTTP #{req.method} request to #{req.path}")
69
- Darksky.logger.debug("Body size: #{req.body.length}") if req.request_body_permitted?
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
- Darksky.logger.info('Header injected into HTTP request header')
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
- Darksky.logger.info('HTTP response header says connection close; closing session now')
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
- Darksky.logger.info('HTTP response was a redirect')
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
- Darksky.logger.debug("Full URI object built for local redirect with path: #{data.path}")
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
- Darksky.logger.info("Local #{method.upcase} redirect, reusing HTTP session")
163
+ logger.info("Local #{method.upcase} redirect, reusing HTTP session")
112
164
  new_http = http
113
165
  else
114
- Darksky.logger.info("External #{method.upcase} redirect, spawning new HTTP object")
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
- data = operate(method, uri, params: params, query: query)
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
@@ -1,5 +1,5 @@
1
1
  module Darksky
2
2
 
3
- Version = '0.0.1'
3
+ Version = '0.0.2'
4
4
 
5
5
  end
data/lib/darksky-ruby.rb CHANGED
@@ -1,3 +1,2 @@
1
- require 'darksky-ruby/logger'
2
1
  require 'darksky-ruby/api'
3
2
  require 'darksky-ruby/http'
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.1
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: 2017-07-22 00:00:00.000000000 Z
11
+ date: 2019-09-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Darksky library written in pure Ruby without external dependancy.
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.12
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
@@ -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