antbird 0.2.2 → 0.3.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: 3e856fa219aa6977816eb17109225439b602a073ca0481b00bb2e474dcd757e0
4
- data.tar.gz: 802baa6e45a28da47204e6e8fe47d9b9bb66ffd4d5a061e958d8ddc68008a96e
3
+ metadata.gz: 8d6972953a2ec1f37d057b6a9dd477593fc06a11b7f343f8c57d0f0fa9e31ee1
4
+ data.tar.gz: bd672bd59f69c2c6d77f90714da1112f441a8d6372d1ce3a9ae641778e2ee2f6
5
5
  SHA512:
6
- metadata.gz: d6c020d3139d94edd7f4b354bf892f422e5fd6bfaab2250f05de237ca817c7f89397cd3f09628a63fcb7b64e46141062d460dea8525f0592048fcf448c0d3e9b
7
- data.tar.gz: 8259edb19088e64d0c5d4cc751870710f801ab64fb4dbc91bfbe1083a4657ed56b0da3a34134209e69864f839cd2fdda5865e13c6e394422d68d6251fad94512
6
+ metadata.gz: b2fea4c7232a0b7209a4c4729ee4ffcd7c4a985de2bf9ec718919b8c01577528fba618ea68a4811aebea5ec47c964baf6b8e0c668327afcf15679d84ee3a3bbc
7
+ data.tar.gz: 04c28e273287890b585b500ce7733399057602a8ff52f8924445317d25d680134b944fbcfdc2950a0648bf8425e2e89fb06ae3255dc25683a670ad4babe560a1
@@ -34,7 +34,7 @@ jobs:
34
34
  - uses: actions/checkout@v2
35
35
  - uses: ruby/setup-ruby@v1
36
36
  with:
37
- ruby-version: 2.6
37
+ ruby-version: 2.7
38
38
  bundler-cache: true
39
39
  - run: bundle install --jobs 4 --retry 3
40
40
  - name: Wait for elasticsearch
@@ -26,7 +26,7 @@ module Antbird
26
26
  end
27
27
  attr_reader :scope, :url, :version
28
28
  attr_reader :read_timeout, :open_timeout, :adapter
29
- attr_reader :api_specs
29
+ attr_reader :api_specs, :last_request
30
30
 
31
31
  def scoped(new_scope = {})
32
32
  Client.new(
@@ -43,11 +43,13 @@ module Antbird
43
43
 
44
44
  body = extract_body(params)
45
45
  scopes = extract_scopes(params)
46
+ forced_method = extract_method(params)
46
47
 
47
48
  # Greedy match
48
49
  api_path = nil
50
+ api_path_template = nil
49
51
  path_methods = nil
50
- api_spec['url']['paths'].sort { |a, b| b.size <=> a.size }.each do |path|
52
+ sort_url_paths(api_spec['url']['paths']).each do |path|
51
53
  if path.is_a?(Hash)
52
54
  path_methods = path['methods']
53
55
  path = path['path']
@@ -57,19 +59,40 @@ module Antbird
57
59
  scopes[$1.to_sym] || @scope[$1.to_sym] || match
58
60
  end
59
61
 
60
- break api_path = embeded unless embeded.include?('{')
62
+ unless embeded.include?('{')
63
+ api_path_template = path
64
+ api_path = embeded
65
+ break
66
+ end
61
67
  end
62
68
 
63
69
  unless api_path
64
70
  raise "API path not found: paths: #{api_spec['url']['paths']}, scope: #{@scope}"
65
71
  end
66
72
 
67
- methods = (Array(path_methods) + Array(api_spec['methods'])).map(&:downcase).map(&:to_sym)
68
- method = methods.include?(:post) ? :post : methods.first
73
+ methods = (path_methods || api_spec['methods']).map { |m| m.downcase.to_sym }
74
+ method =
75
+ if forced_method
76
+ forced_method
77
+ elsif methods.include?(:put)
78
+ :put
79
+ elsif methods.include?(:post)
80
+ :post
81
+ else
82
+ methods.first
83
+ end
69
84
 
70
85
  read_timeout = params.delete(:read_timeout)
71
86
  params.reject! { |_, v| v.nil? }
72
87
 
88
+ @last_request = {
89
+ method: method,
90
+ forced_method: forced_method,
91
+ api_path: api_path,
92
+ api_path_template: api_path_template,
93
+ params: params
94
+ }
95
+
73
96
  response =
74
97
  case method
75
98
  when :head
@@ -116,6 +139,10 @@ module Antbird
116
139
  response.body
117
140
  end
118
141
 
142
+ def extract_method(params)
143
+ params.delete(:method)&.to_sym
144
+ end
145
+
119
146
  def extract_scopes(params)
120
147
  scopes = {}
121
148
  [:index, :type, :id].each do |s|
@@ -165,6 +192,18 @@ module Antbird
165
192
 
166
193
  private
167
194
 
195
+ # NOTE: stable sort
196
+ def sort_url_paths(url_paths)
197
+ i = 0
198
+ url_paths.sort_by do |path|
199
+ if path.is_a?(Hash)
200
+ [-path['path'].count('{'), (path['deprecated'] ? 1 : 0), i += 1]
201
+ else
202
+ [-path.count('{'), i += 1]
203
+ end
204
+ end
205
+ end
206
+
168
207
  def handle_errors!(response)
169
208
  if response.status >= 500
170
209
  raise ServerError, response
@@ -1,3 +1,3 @@
1
1
  module Antbird
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: antbird
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - fukayatsu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-17 00:00:00.000000000 Z
11
+ date: 2020-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday