api-response-presenter 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +57 -4
- data/api_response_presenter.gemspec +2 -1
- data/lib/api_response/processor/failure.rb +5 -1
- data/lib/api_response/version.rb +1 -1
- metadata +18 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc3133cf4ef59c353137dc02d8d34e431cb1f7f57360549206bc477e8cef8164
|
4
|
+
data.tar.gz: 5aa97d07e425e3f178934418c5ff025a3f2cf90c3f4217144f167f55daa6a87a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e72be12e09674208d9e763ce8ceb3067dc2d7ed305afdbc984e9f4870f56f22a36c70075efc4cefaf33774bcf13cb70631c807bd76ed418ebb52e74a68bc0039
|
7
|
+
data.tar.gz: 9a13ab7903e5c322f3760ee2b324ed695bad33c50b825a06ad4ec58bf4bbe16500c44066f6b2bba51726d3c0fb48171ba3d755f4802327fceb7bcdab54548fe9
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
[](https://coveralls.io/github/golifox/api-response-presenter)
|
4
4
|
[](https://inch-ci.org/github/golifox/api-response-presenter)
|
5
5
|
|
6
|
-
The `api-response-presenter` gem provides a flexible and easy-to-use interface for
|
6
|
+
The `api-response-presenter` gem provides a flexible and easy-to-use interface for presenting API responses using Faraday or
|
7
7
|
RestClient with the possibility to configure global settings or per-instance settings. It leverages
|
8
8
|
the `Dry::Configurable` for configurations, ensuring high performance and full test coverage.
|
9
9
|
|
@@ -35,7 +35,7 @@ gem install api-response-presenter
|
|
35
35
|
|
36
36
|
### Configuration
|
37
37
|
|
38
|
-
You can configure
|
38
|
+
You can configure `api-response-presenter` globally in an initializer or setup block:
|
39
39
|
|
40
40
|
```ruby
|
41
41
|
# config/initializers/api_response.rb
|
@@ -88,6 +88,30 @@ end
|
|
88
88
|
|
89
89
|
```
|
90
90
|
|
91
|
+
Also you can create decorator for using functionality e.g.
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
module ApiResponseHandler # or ExternalApiBaseClass
|
95
|
+
private def with_presentation(response, **, &)
|
96
|
+
ApiResponse::Presenter.call(response, **, &)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class ExtenalApiService < ExternalApiBaseClass
|
101
|
+
# or include ApiResponseHandler
|
102
|
+
|
103
|
+
...
|
104
|
+
|
105
|
+
def get_external_data(*, **, &)
|
106
|
+
response = get('/data', *)
|
107
|
+
|
108
|
+
with_presentation(response, **, &)
|
109
|
+
end
|
110
|
+
|
111
|
+
...
|
112
|
+
end
|
113
|
+
```
|
114
|
+
|
91
115
|
### Config options
|
92
116
|
|
93
117
|
- `ApiResponse.config.adapter`: response adapter that you are using.
|
@@ -174,8 +198,12 @@ class EmployeeApiService
|
|
174
198
|
def self.get_employees(monad: false, adapter: :faraday, **options)
|
175
199
|
# or (params, presenter_options = {})
|
176
200
|
response = Faraday.get('https://api.example.com/data', params) # => body: "{\"data\": [{\"id\": 1, \"name\": \"John\"}]}"
|
177
|
-
|
178
|
-
|
201
|
+
|
202
|
+
page = options.fetch(:page, 1)
|
203
|
+
per = options.fetch(:per, 5)
|
204
|
+
|
205
|
+
ApiResponse::Presenter.call(response, monad: monad, adapter: adapter) do |c|
|
206
|
+
c.extract_from_body = ->(body) { Kaminari.paginate_array(body[:data]).page(page).per(per) }
|
179
207
|
c.struct = Employee
|
180
208
|
c.default_return_value = []
|
181
209
|
end
|
@@ -249,6 +277,31 @@ or
|
|
249
277
|
ApiResponse::Presenter.call(response, success_processor: MyClass, failure_processor: MyClass, parser: MyClass)
|
250
278
|
```
|
251
279
|
|
280
|
+
NOTE: If you are using Faraday with Oj middleware to parse json body already, you should redefine parser like this (in next gem version will be available configuring parsing (on/off))
|
281
|
+
|
282
|
+
```ruby
|
283
|
+
# config/initializers/api_response.rb
|
284
|
+
|
285
|
+
require 'api_response'
|
286
|
+
|
287
|
+
class EmptyParser
|
288
|
+
attr_reader :response, :config
|
289
|
+
|
290
|
+
def initialize(response, config: nil)
|
291
|
+
@response = response
|
292
|
+
@config = config
|
293
|
+
end
|
294
|
+
|
295
|
+
def call
|
296
|
+
response.body
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
ApiResponse.configure do |config|
|
301
|
+
config.parser = EmptyParser
|
302
|
+
end
|
303
|
+
```
|
304
|
+
|
252
305
|
#### Options
|
253
306
|
|
254
307
|
Also you can add custom options to `ApiResponse.config.options = {}` and use it in your processor or parser:
|
@@ -25,9 +25,10 @@ Gem::Specification.new do |s|
|
|
25
25
|
s.add_runtime_dependency 'dry-initializer', '~> 3.0'
|
26
26
|
s.add_runtime_dependency 'dry-monads', '~> 1.6'
|
27
27
|
s.add_runtime_dependency 'dry-types', '~> 1.5'
|
28
|
-
s.add_runtime_dependency 'oj', '~> 3.
|
28
|
+
s.add_runtime_dependency 'oj', '~> 3.16.3'
|
29
29
|
s.add_runtime_dependency 'zeitwerk', '~> 2.4'
|
30
30
|
|
31
|
+
s.add_runtime_dependency 'dry-struct', '~> 1.5'
|
31
32
|
s.add_development_dependency 'rake'
|
32
33
|
s.add_development_dependency 'rspec'
|
33
34
|
end
|
@@ -32,7 +32,7 @@ module ApiResponse
|
|
32
32
|
|
33
33
|
def build_error_monad
|
34
34
|
status = config.default_status || prepare_status(response)
|
35
|
-
error = config.
|
35
|
+
error = config.error_json ? response_body : build_error
|
36
36
|
error_key = config.default_error_key || response_body.fetch(:error_key, nil)
|
37
37
|
|
38
38
|
Failure({error: error, error_key: error_key, status: status})
|
@@ -58,6 +58,10 @@ module ApiResponse
|
|
58
58
|
config.default_status
|
59
59
|
end
|
60
60
|
end
|
61
|
+
|
62
|
+
def build_error
|
63
|
+
config.default_error || response_body
|
64
|
+
end
|
61
65
|
end
|
62
66
|
end
|
63
67
|
end
|
data/lib/api_response/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-response-presenter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Rybolovlev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-03-
|
11
|
+
date: 2024-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-configurable
|
@@ -72,14 +72,14 @@ dependencies:
|
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
75
|
+
version: 3.16.3
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
82
|
+
version: 3.16.3
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: zeitwerk
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '2.4'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: dry-struct
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.5'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.5'
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
112
|
name: rake
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|