api_recipes 2.6.0 → 2.8.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: 977a895fe8c06312c73575391e759f6bd23c91fbdad1d26dfb5bb66dfc494b09
4
- data.tar.gz: 507c600f1ab08ea1bcec035d8a5e60178da5d08c4e5f49beb84fba64829f39b5
3
+ metadata.gz: b8b743f62651355bb59b3d641637d8df6b28a860d5000a3b684758a7d1de5c47
4
+ data.tar.gz: d2febad009985547a7a148e1b07b193d568b76bd75a2064a9c55257652ce7997
5
5
  SHA512:
6
- metadata.gz: a4bdfe19e5994165e5d2ab7256b66887c54951f2df93cd44417b4c019f10979532188ff21f581fbfb5ff8528c9d04063eda93326cefeda396a26a4d397c5d5dd
7
- data.tar.gz: b67d63a82f8199e9c38bf043a0d3ba192d7613b34a3955d7aef0ef6df0d5a20017fce3be9083be8e0a697a6e96d7fcdfecb37b0aed3f43742f3600c7deb429cc
6
+ metadata.gz: 6a4b6882211bbae0c27f546267674e501df253eb9a22303fadf0bbf85dfd26a2ada943f813ae1660164c20e3dae68d3afd61caf132e6e40be04bf1f62e9cfcd4
7
+ data.tar.gz: e5429acaf4fbcaa1f5e83090d8ede9dcee36253a61b8d17b5e79fb5c4c4178115b677d91ac39e8c145af63dd1813e904834201fddbc930e45e07b3d8df3455b9
data/lib/api_recipes.rb CHANGED
@@ -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
@@ -58,6 +58,7 @@ module ApiRecipes
58
58
  unless @configuration
59
59
  @configuration = Configuration.new
60
60
  end
61
+
61
62
  @configuration
62
63
  end
63
64
 
@@ -105,7 +106,7 @@ module ApiRecipes
105
106
  def self._aprcps_define_global_apis
106
107
  configuration.apis_configs.each do |api_name, api_configs|
107
108
  api_name = api_name.to_sym
108
- _aprcps_global_storage[api_name] = Api.new api_name, api_configs
109
+ _aprcps_global_storage[api_name] = Api.new api_name, api_configs, self
109
110
  define_singleton_method api_name do
110
111
  _aprcps_global_storage[api_name]
111
112
  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
@@ -16,6 +17,7 @@ module ApiRecipes
16
17
  if endpoints && endpoints.is_a?(Hash)
17
18
  endpoints.each do |ep_name, params|
18
19
  unless respond_to? ep_name
20
+ # TODO: store endpoints, routes and routes_urls
19
21
  define_singleton_method ep_name do |*request_params, &block|
20
22
  # puts "API params: #{params}"
21
23
  Endpoint.new api: self, name: ep_name, path: path, params: params, request_params: request_params, &block
@@ -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,16 @@ 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
+
44
+ attr_reader :path, :expected_code, :response
45
+
46
+ def initialize(path, expected_code = nil, response = nil, message: nil)
47
+ @path = path
48
+ @expected_code = expected_code
49
+ @response = response
50
+ unless message
51
+ message = "response code for request on route '#{@path}' has returned '#{@response.code}', but '#{@expected_code}' was expected\n\nResponse body:\n #{@response.body}"
52
+ end
45
53
  super(message)
46
54
  end
47
55
  end
@@ -11,6 +11,7 @@ module ApiRecipes
11
11
  def data
12
12
  return @data unless @data.nil?
13
13
 
14
+ # TODO: rescue nil?
14
15
  @data = @original_response.parse
15
16
  end
16
17
 
@@ -43,31 +43,40 @@ module ApiRecipes
43
43
 
44
44
  def build_url_from_path
45
45
  attrs = {
46
- scheme: settings[:protocol],
46
+ scheme: settings[:protocol].to_s,
47
47
  host: settings[:host],
48
48
  port: port,
49
49
  path: path
50
50
  }
51
- URI::Generic.build attrs
51
+ URI::Generic.build2 attrs
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, @response, message: message)
71
80
  end
72
81
  end
73
82
  end
@@ -81,12 +90,12 @@ module ApiRecipes
81
90
  end
82
91
 
83
92
  def port
84
- settings[:port] || case settings[:protocol]
85
- when 'http'
86
- 80
87
- when 'https'
88
- 443
89
- end
93
+ settings[:port].to_s || case settings[:protocol].to_s
94
+ when 'http'
95
+ '80'
96
+ when 'https'
97
+ '443'
98
+ end
90
99
  end
91
100
 
92
101
  def prepare_request
@@ -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.0'.freeze
2
+ VERSION = '2.8.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.0
4
+ version: 2.8.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-10-22 00:00:00.000000000 Z
11
+ date: 2021-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
62
  - !ruby/object:Gem::Version
63
63
  version: '0'
64
64
  requirements: []
65
- rubygems_version: 3.1.4
65
+ rubygems_version: 3.0.8
66
66
  signing_key:
67
67
  specification_version: 4
68
68
  summary: Consume HTTP APIs with style