api_recipes 2.6.2 → 2.7.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2ce6553d9d7e1e72dca69781d977310670ac25a3c88b39a974b3b4d5865c0f0f
4
- data.tar.gz: '08ff597bfc3b86e31d4f7d467cf7b0a7f67bd5d08783d9b901ce15f05e0f85ac'
3
+ metadata.gz: 8ac0841819d455adb4d85f88d7858e33cfaa17bf35316811f5a6d7c75879b271
4
+ data.tar.gz: ddacdca3095b42cdc14c51e066a52d237c85dfc0ba79419342168d7667f0b12e
5
5
  SHA512:
6
- metadata.gz: ddfbfdedaba0d774e87c868836f22e493bd0e336e015fa0907939d57756bc6f93b7540836887ea194ef27041b2c4848ccff258b69a9eb59142d591951bfe5827
7
- data.tar.gz: ebca5a942daeb2d98522bb8fc811e1f0ad4b0916e6299a7bf7b6ad3c1b549d3ef28a1ebfe4cbdaf51103513578b97fd4732e730608174b356fafe8ef58a993a1
6
+ metadata.gz: ec9f37f976b33f9386b4f201f4200ce33fdc526510f31984b591823c093c187037c7c8fc6c967fd33094b41f5343b4810ae251a89d3a9922e55b565f7f648503
7
+ data.tar.gz: 4c2f41152de95562744e8b60e8d14ca4adbbd4a88487d89f52149ba9bfd6b9bff3bf830bedc1aee9c59787e541ad5386d151baea552b11e7c63a8eac1e795b85
@@ -32,13 +32,13 @@ module ApiRecipes
32
32
 
33
33
  define_method api_name do
34
34
  configs = ApiRecipes._aprcps_merge_apis_configs(api_name, configs.deep_symbolize_keys)
35
- api = Api.new(api_name, configs)
35
+ api = Api.new(api_name, configs, self)
36
36
  ApiRecipes.copy_global_authorizations_to_api api
37
37
  api
38
38
  end
39
39
  define_singleton_method api_name do
40
40
  configs = ApiRecipes._aprcps_merge_apis_configs(api_name, configs.deep_symbolize_keys)
41
- api = Api.new(api_name, configs)
41
+ api = Api.new(api_name, configs, self)
42
42
  ApiRecipes.copy_global_authorizations_to_api api
43
43
  api
44
44
  end
@@ -105,7 +105,7 @@ module ApiRecipes
105
105
  def self._aprcps_define_global_apis
106
106
  configuration.apis_configs.each do |api_name, api_configs|
107
107
  api_name = api_name.to_sym
108
- _aprcps_global_storage[api_name] = Api.new api_name, api_configs
108
+ _aprcps_global_storage[api_name] = Api.new api_name, api_configs, self
109
109
  define_singleton_method api_name do
110
110
  _aprcps_global_storage[api_name]
111
111
  end
@@ -2,13 +2,14 @@ module ApiRecipes
2
2
  class Api
3
3
 
4
4
  attr_accessor :name, :configs, :authorization, :basic_auth
5
- attr_reader :base_configs
5
+ attr_reader :base_configs, :object
6
6
 
7
- BASE_CONFIGS_KEYS = [:protocol, :host, :port, :api_version, :timeout, :on_bad_code]
7
+ BASE_CONFIGS_KEYS = [:protocol, :host, :port, :api_version, :timeout, :on_bad_code, :verify_with]
8
8
 
9
- def initialize(name, configs)
9
+ def initialize(name, configs, object)
10
10
  @name = name
11
11
  @configs = ApiRecipes::Settings::DEFAULT.merge configs
12
+ @object = object
12
13
 
13
14
  # Generate some_api.some_endpoint methods
14
15
  # e.g. github.users
@@ -3,6 +3,8 @@ module ApiRecipes
3
3
 
4
4
  attr_reader :api, :name, :params, :route, :children
5
5
 
6
+ FORWARDABLE_PARAMS = %i[verify_with].freeze
7
+
6
8
  def initialize(api: nil, name: nil, path: nil, params: {}, request_params: [], &block)
7
9
  @api = api
8
10
  @name = name
@@ -52,6 +54,8 @@ module ApiRecipes
52
54
  # puts "generating children of #{@name}: #{children.inspect}"
53
55
  if children
54
56
  children.each do |ep_name, pars|
57
+ pars = forwardable_params.merge(pars) if pars
58
+ # TODO: Merge pars with params
55
59
  # puts "Creating Endpoint '#{@name}' child '#{ep_name}' passing path #{build_path}"
56
60
  define_singleton_method ep_name do |*request_params, &block|
57
61
  Endpoint.new api: @api, name: ep_name, path: build_path, params: pars, request_params: request_params, &block
@@ -89,7 +93,6 @@ module ApiRecipes
89
93
  end
90
94
  # Merge DEFAULT_ROUTE_ATTRIBUTES with Api base_configs
91
95
  # Then merge the result with provided attributes
92
-
93
96
  @params = Settings::DEFAULT_ROUTE_ATTRIBUTES.inject({}) do |out, key_val|
94
97
  new_val = @api.base_configs[key_val.first]
95
98
  out[key_val.first] = new_val.nil? ? key_val.last : new_val
@@ -126,5 +129,9 @@ module ApiRecipes
126
129
  def required_params_for_path
127
130
  absolute_path.scan(/:(\w+)/).flatten.map { |p| p.to_sym }
128
131
  end
132
+
133
+ def forwardable_params
134
+ params.select { |k, v| FORWARDABLE_PARAMS.include? k }
135
+ end
129
136
  end
130
137
  end
@@ -40,8 +40,10 @@ module ApiRecipes
40
40
  end
41
41
 
42
42
  class ResponseCodeNotAsExpected < Exception
43
- def initialize(path, expected_code = nil, response_code = nil, response_body = nil)
44
- message = "response code for request on route '#{path}' has returned #{response_code}, but #{expected_code} was expected\n\nResponse body:\n #{response_body}"
43
+ def initialize(path, expected_code = nil, response_code = nil, response_body = nil, message: nil)
44
+ unless message
45
+ message = "response code for request on route '#{path}' has returned '#{response_code}', but '#{expected_code}' was expected\n\nResponse body:\n #{response_body}"
46
+ end
45
47
  super(message)
46
48
  end
47
49
  end
@@ -52,22 +52,31 @@ module ApiRecipes
52
52
  end
53
53
 
54
54
  def check_response_code
55
- # If :ok_code property is present, check the response code
56
55
  ok_code = false
57
56
  code = @response.code
58
- if expected_code = attributes[:ok_code]
59
- # If the code does not match, apply the requested strategy
60
- ok_code = true if code == expected_code
57
+ message = nil
58
+
59
+ verify_with = attributes[:verify_with]
60
+ if verify_with && @api.object.respond_to?(verify_with, true )
61
+ ok_code = @api.object.send verify_with, @response
62
+ message = "response for request on route '#{path}' was not valid. Verified with #{@api.object}##{verify_with}.\n\nResponse body:\n #{@response.body}"
61
63
  else
62
- # Default: 200 <= OK < 300
63
- ok_code = true if (code >= 200 && code < 300)
64
- expected_code = '200 <= CODE < 300'
64
+ # If :ok_code property is present, check the response code
65
+ if expected_code = attributes[:ok_code]
66
+ # If the code does not match, apply the requested strategy
67
+ ok_code = true if code == expected_code
68
+ else
69
+ # Default: 200 <= OK < 300
70
+ ok_code = true if @response.status.success?
71
+ expected_code = '200 <= CODE < 300'
72
+ end
65
73
  end
74
+
66
75
  unless ok_code
67
76
  case attributes[:on_bad_code].to_s
68
77
  when 'ignore'
69
78
  when 'raise'
70
- raise ResponseCodeNotAsExpected.new(path, expected_code, code, @response.body)
79
+ raise ResponseCodeNotAsExpected.new(path, expected_code, code, @response.body, message: message)
71
80
  end
72
81
  end
73
82
  end
@@ -9,7 +9,8 @@ module ApiRecipes
9
9
  base_url: nil,
10
10
  timeout: 3,
11
11
  on_bad_code: 'raise',
12
- endpoints: {}
12
+ endpoints: {},
13
+ verify_with: nil
13
14
  }
14
15
 
15
16
  DEFAULT_ROUTE_ATTRIBUTES = {
@@ -18,7 +19,8 @@ module ApiRecipes
18
19
  path: nil,
19
20
  ok_code: nil,
20
21
  timeout: DEFAULT[:timeout],
21
- on_bad_code: DEFAULT[:on_bad_code]
22
+ on_bad_code: DEFAULT[:on_bad_code],
23
+ verify_with: DEFAULT[:verify_with]
22
24
  }
23
25
 
24
26
  AVAILABLE_PARAMS_ENCODINGS = %w(form params json body)
@@ -1,3 +1,3 @@
1
1
  module ApiRecipes
2
- VERSION = '2.6.2'.freeze
2
+ VERSION = '2.7.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_recipes
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.2
4
+ version: 2.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessandro Verlato
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-25 00:00:00.000000000 Z
11
+ date: 2020-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http