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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d4ac34888af5202ce65d67d0611455fd1bbe786
4
- data.tar.gz: 2c7f6b11e8ad2eb056a7b2399be312ad834f8800
3
+ metadata.gz: b02da06373fedf97739a9ecec7942d83f7d68d1a
4
+ data.tar.gz: 95cfcde547f877f1699b07bc6643bcd98ede53ad
5
5
  SHA512:
6
- metadata.gz: bcc5bd5089efd35cbc15146fab4dc7f48fc38ecf6b84ce41e292c827a84fca5915d7df27535add568b01ac706f52df68e1e8c921d5da47a4445d90a64c77a378
7
- data.tar.gz: eba55cbe3023f372036c6b7251df20da26a0d9786ba43e6cf28c5b94b7d5a3d9065a9bb428e279bf5a37d01bbf303ad52def5c97bd85aa9ebd52b1f288fad5c8
6
+ metadata.gz: 90ffc1144c2886f1815cfc42e9a6dc415c0d67a33a66df07bf52ba0e8ffa6fababd5d1d5de76bb6ffdf0696681aa65ed38550a0f16d3143014e9397fa53cd1ff
7
+ data.tar.gz: 8efbc3bc34e4ea3d0fdd54bf7e95e82696c815d117dcdf450e1ca10a9fbeeed32ec52a265d200a4bf37adf0c3d448da8a17fc4a10c28d426f21e712756b4516d
@@ -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
@@ -40,4 +40,5 @@ Gem::Specification.new do |spec|
40
40
  spec.add_development_dependency "virtus-matchers"
41
41
  spec.add_development_dependency "vcr", "~> 3.0"
42
42
  spec.add_development_dependency "webmock"
43
+ spec.add_development_dependency "dry-validation"
43
44
  end
@@ -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
- request_args = opts[:args].each_with_object({}).
16
+ request_partial_args = opts[:args].each_with_object({}).
17
17
  with_index { |(arg, hash), i| hash[arg] = args[i] }
18
18
  else
19
- request_args = args.first || {}
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
- request = request_class.new(default_request_opts.merge(request_args))
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
@@ -1,3 +1,3 @@
1
1
  module APIClientBase
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  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.1.0
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-17 00:00:00.000000000 Z
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: