api_client_base 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +31 -0
- data/api_client_base.gemspec +1 -0
- data/lib/api_client_base.rb +1 -0
- data/lib/api_client_base/client/class_methods.rb +6 -3
- data/lib/api_client_base/services/validate.rb +23 -0
- data/lib/api_client_base/version.rb +1 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b02da06373fedf97739a9ecec7942d83f7d68d1a
|
4
|
+
data.tar.gz: 95cfcde547f877f1699b07bc6643bcd98ede53ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90ffc1144c2886f1815cfc42e9a6dc415c0d67a33a66df07bf52ba0e8ffa6fababd5d1d5de76bb6ffdf0696681aa65ed38550a0f16d3143014e9397fa53cd1ff
|
7
|
+
data.tar.gz: 8efbc3bc34e4ea3d0fdd54bf7e95e82696c815d117dcdf450e1ca10a9fbeeed32ec52a265d200a4bf37adf0c3d448da8a17fc4a10c28d426f21e712756b4516d
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
5
5
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
6
6
|
|
7
|
+
## [1.2.0] - 2017-02-22
|
8
|
+
### Added
|
9
|
+
- raise ArgumentError when validation schema is available and it has errors
|
10
|
+
|
7
11
|
## [1.1.0] - 2017-02-17
|
8
12
|
### Added
|
9
13
|
- `Client` inherits attributes from gem namespace. No need to specify these attributes again.
|
data/README.md
CHANGED
@@ -158,6 +158,37 @@ module MyGem
|
|
158
158
|
end
|
159
159
|
```
|
160
160
|
|
161
|
+
##### Validation
|
162
|
+
|
163
|
+
APIClientBase supports validation so that your calls fail early before even hitting the server. The validation library that this supports (by default, and the only one) is dry-validations.
|
164
|
+
|
165
|
+
Given this request:
|
166
|
+
|
167
|
+
```ruby
|
168
|
+
module MyGem
|
169
|
+
class GetUserRequest
|
170
|
+
attribute :user_id, Integer
|
171
|
+
end
|
172
|
+
end
|
173
|
+
```
|
174
|
+
|
175
|
+
Create a dry-validations schema following this pattern:
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
module MyGem
|
179
|
+
GetUserRequestSchema = Dry::Validation.Schema do
|
180
|
+
required(:user_id).filled(:int?)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
```
|
184
|
+
|
185
|
+
This will raise an error when you call the method:
|
186
|
+
|
187
|
+
```
|
188
|
+
client = MyGem::Client.new #...
|
189
|
+
client.get_user(user_id: nil) # -> this raises an ArgumentError "[user_id: 'must be filled']"
|
190
|
+
```
|
191
|
+
|
161
192
|
#### Responses
|
162
193
|
|
163
194
|
```ruby
|
data/api_client_base.gemspec
CHANGED
data/lib/api_client_base.rb
CHANGED
@@ -5,6 +5,7 @@ require "active_support/inflector"
|
|
5
5
|
require "gem_config"
|
6
6
|
require "virtus"
|
7
7
|
require "api_client_base/version"
|
8
|
+
require "api_client_base/services/validate"
|
8
9
|
require "api_client_base/base"
|
9
10
|
require "api_client_base/base/class_methods"
|
10
11
|
require "api_client_base/client"
|
@@ -13,17 +13,20 @@ module APIClientBase
|
|
13
13
|
response_class = namespace.const_get(response_class_name)
|
14
14
|
|
15
15
|
if opts[:args].is_a?(Array)
|
16
|
-
|
16
|
+
request_partial_args = opts[:args].each_with_object({}).
|
17
17
|
with_index { |(arg, hash), i| hash[arg] = args[i] }
|
18
18
|
else
|
19
|
-
|
19
|
+
request_partial_args = args.first || {}
|
20
20
|
end
|
21
21
|
|
22
22
|
default_request_opts_method =
|
23
23
|
self.class.api_client_base_client_options[:default_opts]
|
24
24
|
default_request_opts = send(default_request_opts_method)
|
25
|
+
request_args = default_request_opts.merge(request_partial_args)
|
25
26
|
|
26
|
-
|
27
|
+
Validate.(request_class, request_args)
|
28
|
+
|
29
|
+
request = request_class.new(request_args)
|
27
30
|
raw_response = request.()
|
28
31
|
response_class.new(raw_response: raw_response)
|
29
32
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module APIClientBase
|
2
|
+
class Validate
|
3
|
+
|
4
|
+
def self.call(klass, attrs)
|
5
|
+
errors = errors_of(klass, attrs)
|
6
|
+
fail(ArgumentError, errors.to_json) if errors.any?
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def self.errors_of(klass, attrs)
|
12
|
+
schema = schema_of(klass)
|
13
|
+
return [] if schema.nil?
|
14
|
+
schema.(attrs).errors
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.schema_of(klass)
|
18
|
+
"#{klass.name}Schema".constantize
|
19
|
+
rescue NameError
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api_client_base
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ramon Tayag
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -136,6 +136,20 @@ dependencies:
|
|
136
136
|
- - ">="
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: dry-validation
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
139
153
|
description: Abstractions to help author API wrappers in Ruby.
|
140
154
|
email:
|
141
155
|
- ramon.tayag@gmail.com
|
@@ -163,6 +177,7 @@ files:
|
|
163
177
|
- lib/api_client_base/client/class_methods.rb
|
164
178
|
- lib/api_client_base/request.rb
|
165
179
|
- lib/api_client_base/response.rb
|
180
|
+
- lib/api_client_base/services/validate.rb
|
166
181
|
- lib/api_client_base/version.rb
|
167
182
|
homepage: https://github.com/imacchiato/api_client-ruby
|
168
183
|
licenses:
|