api_recipes 2.5.0 → 2.7.1

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: febe7d307961dd407eae97ef0519280abf463e896f8a83471d3242b6f8a7b8a1
4
- data.tar.gz: 15df723f847728e9c433223eef128069204aa15344856a4388847860c45304f2
3
+ metadata.gz: 3c49acd995e584ae959722b5bccb248e1c532289e0be89ebda5e1f59d6a81e8e
4
+ data.tar.gz: 92dbb6ce976e6a22d9f5a8b54e5c0fd62d585afb2b0222fe2cf99f885364d52d
5
5
  SHA512:
6
- metadata.gz: 94627389adf247362789b3213f6ec2f45c4443e43c4212ec1fffa96f4796c4de9c097288ef0d328d657c98eb8590fa6908e89f77f080c09be1b0cb0b9c7f44d9
7
- data.tar.gz: a887790e2ac8c3ec7853d2a24735f6c79a7761287e76265854963e80f18320b63bb598365ba75fb316064ebf9c424569e09160f4dc3eb79ce4c09e6326e1972f
6
+ metadata.gz: f327fb50389e63e750cc6c349af86ad791b27196da80cd78434afbb050868cd56e0b91e9825abc62d621a8adf1afeb8b18ac1c04b72cfdea99ad516526752726
7
+ data.tar.gz: 1763a9298481a31923acd934dde45da4f3e105537e35182f65d189eaa7f0128ebfbb96d7409fbf7c6974d389bb495e07d21edae6cf145c6b011520769ad571c6
@@ -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
@@ -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
@@ -1,7 +1,7 @@
1
1
  module ApiRecipes
2
2
  class Configuration
3
3
 
4
- attr_accessor :log_to, :log_level
4
+ attr_accessor :log_to, :log_level, :print_urls
5
5
 
6
6
  def apis_configs=(configs = {})
7
7
  raise ArgumentError, 'apis_configs must be an Hash' unless configs.is_a? Hash
@@ -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
@@ -29,6 +31,10 @@ module ApiRecipes
29
31
  end
30
32
  end
31
33
 
34
+ def url
35
+ @route.url
36
+ end
37
+
32
38
  private
33
39
 
34
40
  def generate_route
@@ -48,6 +54,8 @@ module ApiRecipes
48
54
  # puts "generating children of #{@name}: #{children.inspect}"
49
55
  if children
50
56
  children.each do |ep_name, pars|
57
+ pars = forwardable_params.merge(pars) if pars
58
+ # TODO: Merge pars with params
51
59
  # puts "Creating Endpoint '#{@name}' child '#{ep_name}' passing path #{build_path}"
52
60
  define_singleton_method ep_name do |*request_params, &block|
53
61
  Endpoint.new api: @api, name: ep_name, path: build_path, params: pars, request_params: request_params, &block
@@ -85,7 +93,6 @@ module ApiRecipes
85
93
  end
86
94
  # Merge DEFAULT_ROUTE_ATTRIBUTES with Api base_configs
87
95
  # Then merge the result with provided attributes
88
-
89
96
  @params = Settings::DEFAULT_ROUTE_ATTRIBUTES.inject({}) do |out, key_val|
90
97
  new_val = @api.base_configs[key_val.first]
91
98
  out[key_val.first] = new_val.nil? ? key_val.last : new_val
@@ -122,5 +129,9 @@ module ApiRecipes
122
129
  def required_params_for_path
123
130
  absolute_path.scan(/:(\w+)/).flatten.map { |p| p.to_sym }
124
131
  end
132
+
133
+ def forwardable_params
134
+ params.select { |k, v| FORWARDABLE_PARAMS.include? k }
135
+ end
125
136
  end
126
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
@@ -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
 
@@ -1,7 +1,7 @@
1
1
  module ApiRecipes
2
2
  class Route
3
3
 
4
- attr_reader :request, :response
4
+ attr_reader :request, :response, :url
5
5
  attr_accessor :request_params, :attributes, :path
6
6
 
7
7
  def initialize(api: nil, endpoint: nil, path: nil, attributes: {}, req_pars: {})
@@ -10,7 +10,7 @@ module ApiRecipes
10
10
  @path = path.to_s
11
11
  @attributes = attributes
12
12
  self.request_params = req_pars
13
- @uri = nil
13
+ @url = nil
14
14
 
15
15
  prepare_request
16
16
  end
@@ -28,7 +28,7 @@ module ApiRecipes
28
28
  end
29
29
 
30
30
  def start_request(&block)
31
- original_response = @request.send http_verb, @uri, request_params
31
+ original_response = @request.send http_verb, @url, request_params
32
32
  @response = Response.new original_response, attributes
33
33
  check_response_code
34
34
 
@@ -41,35 +41,42 @@ module ApiRecipes
41
41
 
42
42
  private
43
43
 
44
- def build_uri_from_path
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)
71
- when 'return_false'
72
- return false
79
+ raise ResponseCodeNotAsExpected.new(path, expected_code, code, @response.body, message: message)
73
80
  end
74
81
  end
75
82
  end
@@ -83,17 +90,17 @@ module ApiRecipes
83
90
  end
84
91
 
85
92
  def port
86
- settings[:port] || case settings[:protocol]
87
- when 'http'
88
- 80
89
- when 'https'
90
- 443
91
- end
93
+ settings[:port].to_s || case settings[:protocol].to_s
94
+ when 'http'
95
+ '80'
96
+ when 'https'
97
+ '443'
98
+ end
92
99
  end
93
100
 
94
101
  def prepare_request
95
- @uri = build_uri_from_path
96
- puts @uri if ApiRecipes.print_urls
102
+ @url = build_url_from_path
103
+ puts @url if ApiRecipes.configuration.print_urls
97
104
 
98
105
  @request = request_with_auth
99
106
  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.5.0'.freeze
2
+ VERSION = '2.7.1'.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.5.0
4
+ version: 2.7.1
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-09-30 00:00:00.000000000 Z
11
+ date: 2020-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -62,8 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
62
  - !ruby/object:Gem::Version
63
63
  version: '0'
64
64
  requirements: []
65
- rubyforge_project:
66
- rubygems_version: 2.7.7
65
+ rubygems_version: 3.0.8
67
66
  signing_key:
68
67
  specification_version: 4
69
68
  summary: Consume HTTP APIs with style