api_recipes 0.7.1 → 1.0.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: dfd5a3d85babd54baa4d9001c52362cc5dfff4421cf3f9e7a2417e78b3f5eb2b
4
- data.tar.gz: 316bd1cefa28669906a4e87a56ef7e64ed14369cf2099262ad5ed85fa0544b05
3
+ metadata.gz: fc2b26e8c66139a2ff051ef066bb512b389a48bcbfa2ba2b391f1dc29719426d
4
+ data.tar.gz: 281afb372a91edac6bbc1bc6335da2fb413e856ed7f32cff476f9c527d4dc459
5
5
  SHA512:
6
- metadata.gz: 28bd374672f163b8912794f12e33bbaaa9168f03940651dda9fba2bb88899b5202ea51cace5ed0a814508eef4dcbb195ac32826590c12d1e5d91a31f66a93d84
7
- data.tar.gz: 0bbb7e6ec0b1f02254449edfb03731b05927e5ac3bdc51a3fc2c2a89ae8ccbe79238f70ebe022978611ca4e49a0e9b1b95c96a3c4977360f36686424371b492e
6
+ metadata.gz: 610ff709c864f8cb71f29f42a0a50cd75318e0ce184801f8aa40102d35301b937afdd1c3a56381f783e405763aad2ac4886205809c2b26a07d73a9433e368c81
7
+ data.tar.gz: a3a0cd7ac6a1b7aaea7fe3f5989d4e1c7b071f6d7b18ffc99b4fbff56689114407310f37e459cee183a56b78529f51fe39b59712b4dc5cbd31577fc7e451b2b7
@@ -18,6 +18,5 @@ Gem::Specification.new do |spec|
18
18
  spec.require_paths = ['lib']
19
19
  spec.required_ruby_version = '> 2.2.0'
20
20
 
21
- spec.add_dependency 'oj', '~> 3.7.0'
22
21
  spec.add_dependency 'http', '~> 4.1.1'
23
22
  end
@@ -1,11 +1,11 @@
1
1
  require 'http'
2
- require 'oj'
3
2
 
4
3
  require 'api_recipes/utils'
5
4
  require 'api_recipes/exceptions'
6
5
  require 'api_recipes/configuration'
7
6
  require 'api_recipes/resource'
8
7
  require 'api_recipes/endpoint'
8
+ require 'api_recipes/response'
9
9
  require 'api_recipes/settings'
10
10
 
11
11
  module ApiRecipes
@@ -26,16 +26,18 @@ module ApiRecipes
26
26
  if self.respond_to? endpoint_name
27
27
  raise EndpointNameClashError.new(self, endpoint_name)
28
28
  else
29
+ ep = Endpoint.new(endpoint_name, configs)
30
+ ApiRecipes.copy_global_authorizations_to_endpoint ep
31
+ ApiRecipes._aprcps_thread_storage[endpoint_name] = {}
32
+ ApiRecipes._aprcps_thread_storage[endpoint_name][self] = ep
33
+
29
34
  define_method endpoint_name do
30
35
  unless ApiRecipes._aprcps_thread_storage[endpoint_name]
31
36
  ApiRecipes._aprcps_thread_storage[endpoint_name] = {}
32
37
  end
33
38
  unless ApiRecipes._aprcps_thread_storage[endpoint_name][self.class]
34
- ep = Endpoint.new(endpoint_name, configs)
35
- ApiRecipes.copy_global_authorizations_to_endpoint ep
36
- ApiRecipes._aprcps_thread_storage[endpoint_name][self.class] = ep
39
+ ApiRecipes._aprcps_thread_storage[endpoint_name][self.class] = ep.clone
37
40
  end
38
-
39
41
  ApiRecipes._aprcps_thread_storage[endpoint_name][self.class]
40
42
  end
41
43
  define_singleton_method endpoint_name do
@@ -43,11 +45,8 @@ module ApiRecipes
43
45
  ApiRecipes._aprcps_thread_storage[endpoint_name] = {}
44
46
  end
45
47
  unless ApiRecipes._aprcps_thread_storage[endpoint_name][self]
46
- ep = Endpoint.new(endpoint_name, configs)
47
- ApiRecipes.copy_global_authorizations_to_endpoint ep
48
- ApiRecipes._aprcps_thread_storage[endpoint_name][self] = ep
48
+ ApiRecipes._aprcps_thread_storage[endpoint_name][self] = ep.clone
49
49
  end
50
-
51
50
  ApiRecipes._aprcps_thread_storage[endpoint_name][self]
52
51
  end
53
52
  end
@@ -28,6 +28,13 @@ module ApiRecipes
28
28
  end
29
29
  end
30
30
 
31
+ class ProvidedObjectNotAsResponseData < Exception
32
+ def initialize(object_class, data_class)
33
+ message = "provided object #{object_class} is not compatible with response data that is of type #{data_class}"
34
+ super(message)
35
+ end
36
+ end
37
+
31
38
  class ResponseCodeNotAsExpected < Exception
32
39
  attr_reader :resource, :route, :expected_code, :response_code, :response_body
33
40
 
@@ -1,6 +1,8 @@
1
1
  module ApiRecipes
2
2
  class Resource
3
3
 
4
+ attr_reader :request, :response
5
+
4
6
  def initialize(name, endpoint, routes = {})
5
7
  @name = name
6
8
  @routes = routes
@@ -9,6 +11,18 @@ module ApiRecipes
9
11
  generate_routes
10
12
  end
11
13
 
14
+ def fill(object)
15
+ data = @response.parse
16
+ if block_given?
17
+ tap do
18
+ try_to_fill object, data
19
+ yield object, data, @response.status
20
+ end
21
+ else
22
+ try_to_fill object, data
23
+ end
24
+ end
25
+
12
26
  private
13
27
 
14
28
  def build_path(route_name, route_attributes, provided_params)
@@ -24,9 +38,28 @@ module ApiRecipes
24
38
  return path, provided_params
25
39
  end
26
40
 
27
- def build_request
41
+ def build_request(route, route_attributes, *pars)
42
+ unless route_attributes
43
+ route_attributes = {}
44
+ end
45
+ # Merge route attributes with defaults and deep clone route attributes
46
+ route_attributes = Marshal.load(Marshal.dump(Settings::DEFAULT_ROUTE_ATTRIBUTES.merge(route_attributes).deep_symbolize_keys))
47
+
48
+ params = pars.extract_options!
49
+ path, residual_params = build_path(route, route_attributes, params)
50
+ residual_params = nil unless residual_params.any?
51
+ uri = build_uri_from path
28
52
 
53
+ @request = request_with_auth
54
+ @response = @request.send(route_attributes[:method], uri, encode_residual_params(route_attributes, residual_params))
55
+ check_response_code route, route_attributes, @response
29
56
 
57
+ if block_given?
58
+ data = @response.parse
59
+ tap { yield data, @response.status }
60
+ else
61
+ self
62
+ end
30
63
  end
31
64
 
32
65
  def build_uri_from(path)
@@ -42,15 +75,15 @@ module ApiRecipes
42
75
  def check_response_code(route, route_attributes, response)
43
76
  # Check if :ok_code is present, check the response
44
77
  if ok_code = route_attributes[:ok_code]
45
- code = response.code
78
+ code = response.status.code
46
79
  # If the code does not match, apply the requested strategy (see FAIL_OPTIONS)
47
80
  unless code == ok_code
48
81
  case settings[:on_nok_code]
49
- when :do_nothing
50
- when :raise
51
- raise ResponseCodeNotAsExpected.new(nil, @name, route, ok_code, code, response.body)
52
- when :return_false
53
- return false
82
+ when :do_nothing
83
+ when :raise
84
+ raise ResponseCodeNotAsExpected.new(nil, @name, route, ok_code, code, response.body)
85
+ when :return_false
86
+ return false
54
87
  end
55
88
  end
56
89
  end
@@ -61,12 +94,12 @@ module ApiRecipes
61
94
  if Settings::AVAILABLE_PARAMS_ENCODINGS.include? route_attributes[:encode_params_as].to_s
62
95
  { route_attributes[:encode_params_as].to_sym => residual_params }
63
96
  else
64
- # default to query string params (get) or json (other methods)
97
+ # Default to query string params (get) or json (other methods)
65
98
  case route_attributes[:method].to_sym
66
- when :get
67
- { params: residual_params }
68
- when :post, :put, :patch, :delete
69
- { json: residual_params }
99
+ when :get
100
+ { params: residual_params }
101
+ when :post, :put, :patch, :delete
102
+ { json: residual_params }
70
103
  end
71
104
  end
72
105
  end
@@ -85,7 +118,7 @@ module ApiRecipes
85
118
  end
86
119
  unless respond_to? route.to_sym
87
120
  define_singleton_method route.to_sym do |*params, &block|
88
- start_request route, attrs, *params, &block
121
+ build_request route, attrs, *params, &block
89
122
  end
90
123
  else
91
124
  raise RouteNameClashWithExistentMethod.new(@name, route)
@@ -100,10 +133,10 @@ module ApiRecipes
100
133
 
101
134
  def port
102
135
  settings[:port] || case settings[:protocol]
103
- when 'http'
104
- 80
105
- when 'https'
106
- 443
136
+ when 'http'
137
+ 80
138
+ when 'https'
139
+ 443
107
140
  end
108
141
  end
109
142
 
@@ -131,27 +164,29 @@ module ApiRecipes
131
164
  @endpoint.configs
132
165
  end
133
166
 
134
- def start_request(route, route_attributes, *pars)
135
- unless route_attributes
136
- route_attributes = {}
167
+ def try_to_fill(object, data)
168
+ case data
169
+ when Hash
170
+ res = fill_object_with object, data
171
+ when Array
172
+ res = []
173
+ data.each do |element|
174
+ res << fill_object_with(object.new, element)
175
+ end
137
176
  end
138
- # Merge route attributes with defaults and deep clone route attributes
139
- route_attributes = Marshal.load(Marshal.dump(Settings::DEFAULT_ROUTE_ATTRIBUTES.merge(route_attributes).deep_symbolize_keys))
140
177
 
141
- params = pars.extract_options!
142
- path, residual_params = build_path(route, route_attributes, params)
143
- residual_params = nil unless residual_params.any?
144
- uri = build_uri_from path
145
-
146
- response = request_with_auth.send(route_attributes[:method], uri, encode_residual_params(route_attributes, residual_params))
147
- check_response_code route, route_attributes, response
178
+ res
179
+ end
148
180
 
149
- if block_given?
150
- body = Oj.load(response.body.to_s)
151
- yield body, response.code, response.reason
152
- else
153
- response
181
+ def fill_object_with(object, data)
182
+ data.each do |key, value|
183
+ begin
184
+ object.send "#{key}=", value
185
+ rescue
186
+ end
154
187
  end
188
+
189
+ object
155
190
  end
156
191
  end
157
192
  end
@@ -0,0 +1,5 @@
1
+ module ApiRecipes
2
+ class Response < HTTP::Response
3
+
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module ApiRecipes
2
- VERSION = '0.7.1'
2
+ VERSION = '1.0.0'
3
3
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_recipes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 1.0.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: 2019-07-15 00:00:00.000000000 Z
11
+ date: 2019-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: oj
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 3.7.0
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: 3.7.0
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: http
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -72,6 +58,7 @@ files:
72
58
  - lib/api_recipes/endpoint.rb
73
59
  - lib/api_recipes/exceptions.rb
74
60
  - lib/api_recipes/resource.rb
61
+ - lib/api_recipes/response.rb
75
62
  - lib/api_recipes/settings.rb
76
63
  - lib/api_recipes/utils.rb
77
64
  - lib/api_recipes/version.rb