sentofu 0.1.0 → 0.2.0

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
  SHA256:
3
- metadata.gz: ec10af5cec1acf4c593f766637358a1cc323665bf65cde68c5c727c637582f7f
4
- data.tar.gz: e75a6edec73c0c0faa5cc7e9a8657f9d348a25bbcc023d3422b4f62dd9e89a5e
3
+ metadata.gz: 6b7dc29a9728152c3aa6e84c7112e37743b19e5261ede35083a8937e92763376
4
+ data.tar.gz: '009438a0d7a8d47de159407759b396632292bd77457381cf52826bab414236c5'
5
5
  SHA512:
6
- metadata.gz: 7e8145180ed891775ef9cf4358778091d18fe5f4199cf2c87a899de9ec14e5ab3a60d18cfa93314a3417660a3a2cb34757bedcaefc0d6b51f5da862659f9836a
7
- data.tar.gz: 2f3a1bf05a54b680bdf6247ea2c9b72cac64a590586341476d9b90fe2c2b27986bbcd32d55f94e190fda85d13ffca4daa5a005930abc50e3f2855ea642b44ee0
6
+ metadata.gz: aeab8b37e947bd378312f9d6505501884b299e68600d314632729804d58c1fee6da15ebdfe0e9890b82ad21be15906faaa99bd2f173e16c0e037d3c8ed6d0784
7
+ data.tar.gz: 887c2f98f8b238157cef6fc711cced2634c51a26d096d34e5c4e645e10631719dcfbc19a488b5223c19263d7cd19322078c5b4452e402f2e91a507db7238942e
data/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
  # CHANGELOG.md
3
3
 
4
4
 
5
+ ## sentofu 0.2.0 released 2019-05-29
6
+
7
+ * Accept a directory for Sentofu.init(x)
8
+ * Return a Hash error message on JSON parse error
9
+ * Track request elapsed time
10
+
11
+
5
12
  ## sentofu 0.1.0 released 2019-05-23
6
13
 
7
14
  * Initial implementation
data/lib/sentofu/api.rb CHANGED
@@ -49,9 +49,6 @@ module Sentofu
49
49
 
50
50
  def fetch(segment, point, index, query)
51
51
 
52
- #p [ :fetch, segment, '(point)', index, query ]
53
- #p path(segment)
54
- #pp point
55
52
  Thread.current.thread_variable_set(TVP + segment, index) if index
56
53
 
57
54
  q = rectify_query_parameters(point, query)
@@ -63,7 +60,7 @@ module Sentofu
63
60
 
64
61
  return query.merge(path: pa) if query[:debug]
65
62
 
66
- JSON.parse(Sentofu::Http.get(pa, api.token).body)
63
+ Sentofu::Http.get_and_parse(pa, api.token)
67
64
  end
68
65
 
69
66
  def path
@@ -184,7 +181,7 @@ module Sentofu
184
181
 
185
182
  return params.merge(path: pa) if params[:debug]
186
183
 
187
- JSON.parse(Sentofu::Http.get(pa, token).body)
184
+ Sentofu::Http.get_and_parse(pa, token)
188
185
  end
189
186
 
190
187
  def modified
data/lib/sentofu/http.rb CHANGED
@@ -7,54 +7,67 @@ module Sentofu
7
7
 
8
8
  def fetch_token(credentials=nil)
9
9
 
10
- u = URI(Sentofu.auth_uri)
11
-
12
10
  cs = narrow_credentials(credentials)
13
11
 
14
12
  a = Base64.encode64("#{cs.id}:#{cs.secret}").strip
15
- #p a
16
13
 
17
- req = Net::HTTP::Post.new(u.path)
14
+ req = Net::HTTP::Post.new(Sentofu.auth_uri)
18
15
  req.add_field('Content-Type', 'application/json')
19
16
  req.add_field('Authorization', a)
20
17
 
21
18
  req.body = JSON.dump(
22
19
  grant_type: 'password', username: cs.user, password: cs.pass)
23
20
 
24
- res = make_http(u).request(req)
25
-
26
- Sentofu::Token.new(res)
21
+ Sentofu::Token.new(request(Sentofu.auth_uri, req))
27
22
  end
28
23
 
29
24
  def get(uri, token=nil)
30
25
 
31
- u = URI(uri)
26
+ request(uri, make_get_req(uri, token))
27
+ end
28
+
29
+ def request(uri, req)
30
+
31
+ u = uri.is_a?(String) ? URI(uri) : uri
32
+
33
+ t0 = monow
34
+
35
+ t = Net::HTTP.new(u.host, u.port)
36
+ t.use_ssl = (u.scheme == 'https')
37
+ #t.set_debug_output($stdout) if u.to_s.match(/search/)
32
38
 
33
- #t0 = Time.now
34
- res = make_http(u).request(make_get_req(u, token))
35
- #def res.headers; r = {}; each_header { |k, v| r[k] = v }; r; end
36
- #puts "*** GET #{uri} took #{Time.now - t0}s"
39
+ res = t.request(req)
40
+
41
+ class << res; attr_accessor :_elapsed; end
42
+ res._elapsed = monow - t0
37
43
 
38
44
  res
39
45
  end
40
46
 
41
47
  def get_and_parse(uri, token=nil)
42
48
 
43
- JSON.parse(get(uri, token).body)
44
- end
49
+ res = get(uri, token)
50
+ r = JSON.parse(res.body)
51
+ r[:_elapsed] = res._elapsed
45
52
 
46
- protected
53
+ r
47
54
 
48
- def make_http(uri)
55
+ rescue JSON::ParserError => pe
49
56
 
50
- #p uri.to_s
51
- t = Net::HTTP.new(uri.host, uri.port)
52
- t.use_ssl = (uri.scheme == 'https')
53
- #t.set_debug_output($stdout) if uri.to_s.match(/search/)
57
+ h = {}
58
+ h[:code] = res.code.to_i
59
+ h[:message] = WEBrick::HTTPStatus.reason_phrase(res.code)
60
+ h[:error_class] = pe.class.to_s
61
+ h[:error_message] = pe.message
62
+ h[:body] = res.body unless res.body.index('</html>')
54
63
 
55
- t
64
+ h
56
65
  end
57
66
 
67
+ protected
68
+
69
+ def monow; Process.clock_gettime(Process::CLOCK_MONOTONIC); end
70
+
58
71
  def make_get_req(uri, token)
59
72
 
60
73
  req = Net::HTTP::Get.new(uri.to_s)
@@ -65,7 +78,6 @@ module Sentofu
65
78
  req.set_header('Accept', 'application/json')
66
79
 
67
80
  req.set_header('Authorization', token.header_value) if token
68
- #pp req.instance_variable_get(:@header)
69
81
 
70
82
  req
71
83
  end
@@ -93,6 +105,7 @@ module Sentofu
93
105
  def initialize(res)
94
106
 
95
107
  @h = JSON.parse(res.body)
108
+ @h[:_elapsed] = res._elapsed
96
109
  @expires_at = Time.now + @h['expires_in']
97
110
  end
98
111
 
data/lib/sentofu.rb CHANGED
@@ -3,6 +3,7 @@ require 'json'
3
3
  require 'yaml'
4
4
  require 'time'
5
5
  require 'base64'
6
+ require 'webrick' # for the http code to message mapping
6
7
  require 'ostruct'
7
8
  require 'net/http'
8
9
 
@@ -13,7 +14,7 @@ require 'sentofu/explo'
13
14
 
14
15
  module Sentofu
15
16
 
16
- VERSION = '0.1.0'
17
+ VERSION = '0.2.0'
17
18
 
18
19
  USER_AGENT =
19
20
  "Sentofu #{Sentofu::VERSION} - " +
@@ -31,6 +32,38 @@ module Sentofu
31
32
 
32
33
  def init(versions=%w[ common:1.0.0 company:1.0.0 markets:1.0.0 ])
33
34
 
35
+ if versions.is_a?(String) && File.directory?(versions)
36
+ init_from_dir(versions)
37
+ else
38
+ init_from_swagger(versions)
39
+ end
40
+ end
41
+
42
+ def credentials=(cs)
43
+
44
+ apis.each { |_, api| api.credentials = cs }
45
+ end
46
+
47
+ protected
48
+
49
+ def init_from_dir(dir)
50
+
51
+ paths = Dir[File.join(dir, 'api_*.yaml')]
52
+
53
+ fail RuntimeError.new("no api yaml files under #{dir.inspect}") \
54
+ if paths.empty?
55
+
56
+ paths.each do |path|
57
+
58
+ api_name = path.match(/api_([^_]+)_\d/)[1]
59
+ api_spec = YAML.load(File.read(path))
60
+
61
+ init_api(api_name, api_spec)
62
+ end
63
+ end
64
+
65
+ def init_from_swagger(versions)
66
+
34
67
  vers = split_versions(versions)
35
68
 
36
69
  vers << %w[ auth * ] unless vers.find { |n, _| n == 'auth' }
@@ -58,23 +91,21 @@ module Sentofu
58
91
  spec = Sentofu::Http.get_and_parse(u)
59
92
  spec[:meta] = meta
60
93
 
61
- if api_name == 'auth'
62
- @auth_uri = spec['servers'][0]['url'] + spec['paths'].keys.first
63
- else
64
- api = Sentofu::Api.new(api_name, spec)
65
- Sentofu.define_singleton_method(api_name) { api }
66
- @apis[api_name] = api
67
- end
94
+ init_api(api_name, spec)
68
95
  end
69
96
  end
70
97
 
71
- def credentials=(cs)
98
+ def init_api(name, spec)
72
99
 
73
- apis.each { |_, api| api.credentials = cs }
100
+ if name == 'auth'
101
+ @auth_uri = spec['servers'][0]['url'] + spec['paths'].keys.first
102
+ else
103
+ api = Sentofu::Api.new(name, spec)
104
+ Sentofu.define_singleton_method(name) { api }
105
+ @apis[name] = api
106
+ end
74
107
  end
75
108
 
76
- protected
77
-
78
109
  def split_versions(vs)
79
110
 
80
111
  case vs
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sentofu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-23 00:00:00.000000000 Z
11
+ date: 2019-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec