rest-in-peace 6.0.3 → 6.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c4599314ea114e10d9395921b8e21da3e354b08
4
- data.tar.gz: c434cdd4712d9f840cee88414efe160e3ce3cea3
3
+ metadata.gz: a4d674276d057a2269ed9800608659da1a067abe
4
+ data.tar.gz: ed4b76287fe7c704e32b3c4c4e618d56aa113b60
5
5
  SHA512:
6
- metadata.gz: 6313f2c23659f5ddeee5f374b49758e43c7b59695cab2802cc3e6e026b920865b57e6b718abdf91e84dd6f785d902d3a102a0f96b3024396a66bb645996b5241
7
- data.tar.gz: 8b24c6ce07f299b9bc3c26870f4b4385757be82e04eb503244ffd29afa6e397752b6a8e7d6e8f1568f6c679cc833f8d4b3bccb61bf71c9e88d753dec75457c56
6
+ metadata.gz: 2d904832dd9e7d6ba2e17c09717f1adbe1fb43567e8e20eaf692cb3e713b36490cba74f2058563fafb106c203256f029e31e5156301dab553c66d499c969d9fd
7
+ data.tar.gz: a585f7ecfea0b4ed82de53ebd430dced0c70479b306b196dca1d43dff5eadd13aef4878735893ee1ed8cb9a38eadea70c46146f59aff274f1a92bce922f564e3
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  Gemfile.lock
2
2
  coverage
3
3
  *.gem
4
+ .bundle
@@ -1 +1 @@
1
- 2.3.3
1
+ 2.3.4
@@ -5,12 +5,15 @@ rvm:
5
5
  - 2.1.2
6
6
  - 2.2.5
7
7
  - 2.3.1
8
+ - 2.3.4
9
+ - 2.4.1
8
10
  gemfile:
9
11
  - gemfiles/Gemfile.activemodel-3.2.x
10
12
  - gemfiles/Gemfile.activemodel-4.0.x
11
13
  - gemfiles/Gemfile.activemodel-4.1.x
12
14
  - gemfiles/Gemfile.activemodel-4.2.x
13
15
  - gemfiles/Gemfile.activemodel-5.0.x
16
+ - gemfiles/Gemfile.activemodel-5.1.x
14
17
  script:
15
18
  - bundle exec rake spec
16
19
  before_install: gem install bundler
@@ -20,3 +23,14 @@ matrix:
20
23
  gemfile: gemfiles/Gemfile.activemodel-5.0.x
21
24
  - rvm: 2.1.2
22
25
  gemfile: gemfiles/Gemfile.activemodel-5.0.x
26
+ - rvm: 2.0.0
27
+ gemfile: gemfiles/Gemfile.activemodel-5.1.x
28
+ - rvm: 2.1.2
29
+ gemfile: gemfiles/Gemfile.activemodel-5.1.x
30
+ notifications:
31
+ email: false
32
+ slack:
33
+ on_success: always
34
+ on_failure: always
35
+ rooms:
36
+ secure: VKDJrfgm1c9OUaQPyMutsVzMCwZzBQh7qxVGJnsdNOHOLtVrc6dQrC6+w7Zc1Py5H5pUU+roKi2n5rVnhNjHf4xukGEdDpI0JBBgYSZjl9b/RbxBsHYG9Z8tUQ0EZ7Bd9H3eH2LHQvjoEcvvUJ6I2BCbXlPCrMovfEB3ILQ8Ryo=
data/README.md CHANGED
@@ -10,18 +10,51 @@ A ruby REST client that lets you feel like in heaven when consuming APIs.
10
10
 
11
11
  gem 'rest-in-peace'
12
12
 
13
- 2. Choose which http adapter you want to use
13
+ 2. Choose which HTTP client you want to use
14
14
 
15
15
  gem 'faraday'
16
+
17
+ 3. Choose which HTTP adapter you want to use
18
+
19
+ gem 'excon'
16
20
 
17
21
  ## Usage
18
22
 
19
23
  ### HTTP Client Library
20
24
 
21
- There is no dependency on a specific HTTP client library but the client has been tested with [Faraday](https://github.com/lostisland/faraday) only. You can use any other client library as long as it has the same API as Faraday.
25
+ This gem depends on the HTTP client library [Faraday](https://github.com/lostisland/faraday). REST-in-Peace has been tested in combination
26
+ with [Faraday](https://github.com/lostisland/faraday) and [Excon](https://github.com/excon/excon) only.
22
27
 
23
28
  ### Configuration
24
29
 
30
+ #### HTTP Client
31
+
32
+ You need to configure and specify the HTTP client library to use. You can either specify a block (for lazy loading) or a client instance directly.
33
+
34
+ ```ruby
35
+ class Resource
36
+ include RESTinPeace
37
+
38
+ rest_in_peace do
39
+ use_api ->() do
40
+ ::Faraday.new(url: 'http://rip.dev', headers: { 'Accept' => 'application/json' }) do |faraday|
41
+ faraday.request :json
42
+ faraday.response :json
43
+ faraday.adapter :excon # make requests with Excon
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ class ResourceTwo
50
+ include RESTinPeace
51
+
52
+ rest_in_peace do
53
+ use_api ->() { MyClient.api }
54
+ end
55
+ end
56
+ ```
57
+
25
58
  #### Attributes
26
59
 
27
60
  You need to specify all the attributes which should be read out of the parsed JSON. You have to specify whether an attribute
@@ -40,7 +73,7 @@ end
40
73
 
41
74
  #### API Endpoints
42
75
 
43
- You need to define all the API endpoints you want to consume with `RESTinPeace`. Currently the four HTTP verbs `GET`, `POST`, `PATCH` and `DELETE` are supported.
76
+ You need to define all the API endpoints you want to consume with `REST-in-Peace`. Currently the five verbs `GET`, `POST`, `PUT`, `PATCH` and `DELETE` are supported.
44
77
 
45
78
  There are two sections where you can specify endpoints: `resource` and `collection`. `collection` supports the HTTP verb `GET` only.
46
79
 
@@ -55,28 +88,6 @@ rest_in_peace do
55
88
  end
56
89
  ```
57
90
 
58
- #### HTTP Client Configuration
59
-
60
- You need to specify the HTTP client library to use. You can either specify a block (for lazy loading) or a client instance directly.
61
-
62
- ```ruby
63
- class Resource
64
- include RESTinPeace
65
-
66
- rest_in_peace do
67
- use_api ->() { Faraday.new(url: 'http://rip.dev') }
68
- end
69
- end
70
-
71
- class ResourceTwo
72
- include RESTinPeace
73
-
74
- rest_in_peace do
75
- use_api Faraday.new(url: 'http://rip.dev')
76
- end
77
- end
78
- ```
79
-
80
91
  #### Resource
81
92
 
82
93
  If you define anything inside the `resource` block, it will define a method on the instances of the class:
@@ -160,6 +171,7 @@ For easy interoperability with Rails, there is the ability to include ActiveMode
160
171
 
161
172
  ```ruby
162
173
  require 'rest_in_peace'
174
+ require 'faraday'
163
175
 
164
176
  module MyClient
165
177
  class Fabric
@@ -184,11 +196,14 @@ module MyClient
184
196
  end
185
197
  ```
186
198
 
187
- #### Complete Configuration
199
+ ### Complete Configurations
188
200
 
189
- ```ruby
201
+ #### Configured Fabric class with all dependencies
202
+
203
+ ```ruby
190
204
  require 'my_client/paginator'
191
205
  require 'rest_in_peace'
206
+ require 'faraday'
192
207
 
193
208
  module MyClient
194
209
  class Fabric
@@ -220,6 +235,72 @@ module MyClient
220
235
  end
221
236
  ```
222
237
 
238
+ #### Configured Fabric class using Faraday
239
+
240
+ ```ruby
241
+ require 'my_client/paginator'
242
+ require 'rest_in_peace'
243
+ require 'faraday'
244
+
245
+ module MyClient
246
+ class Fabric
247
+ include RESTinPeace
248
+
249
+ rest_in_peace do
250
+ use_api ->() do
251
+ ::Faraday.new(url: 'http://localhost:3001', headers: { 'Accept' => 'application/json' }) do |faraday|
252
+ faraday.request :json
253
+ faraday.response :json
254
+ faraday.adapter :excon
255
+ end
256
+ end
257
+
258
+ attributes do
259
+ read :id
260
+ write :name
261
+ end
262
+
263
+ resource do
264
+ patch :save, '/fabrics/:id'
265
+ post :create, '/fabrics'
266
+ delete :destroy, '/fabrics/:id'
267
+ get :reload, '/fabrics/:id'
268
+ end
269
+
270
+ collection do
271
+ get :all, '/fabrics', paginate_with: MyClient::Paginator
272
+ get :find, '/fabrics/:id'
273
+ end
274
+
275
+ acts_as_active_model
276
+ end
277
+ end
278
+ end
279
+ ```
280
+
281
+ #### CRUD options example of use
282
+ ```ruby
283
+ # CREATE
284
+ fabric = MyClient::Fabric.new(name: 'my new fabric')
285
+ fabric.create # calls "POST /fabrics"
286
+ fabric.reload # calls "GET /fabrics/1"
287
+
288
+ # READ
289
+ last_fabric = MyClient::Fabric.find(id: fabric.id) # calls "GET /fabrics/1"
290
+
291
+ # UPDATE - first way
292
+ updated_fabric = last_fabric.update_attributes(name: 'first way fabric')
293
+ updated_fabric.save # calls "PATCH /fabrics/1"
294
+
295
+ # UPDATE - second way
296
+ updated_fabric = last_fabric.update(name: 'second way fabric') # calls "PATCH /fabrics/1"
297
+
298
+ # DELETE
299
+ updated_fabric.destroy # calls "DELETE /fabrics/1"
300
+ ```
301
+ Method `update_attributes` sets updated value, `save` method stores all changes. This change can be done with calling
302
+ single line method `update` which do the both things.
303
+
223
304
  ## Helpers
224
305
 
225
306
  ### SSL Configuration for Faraday
data/VERSION CHANGED
@@ -1 +1 @@
1
- 6.0.3
1
+ 6.0.4
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '../'
4
+
5
+ gem 'activemodel', '~> 5.1.2'
@@ -1,4 +1,5 @@
1
1
  require 'rest_in_peace/errors'
2
+ require 'addressable/uri'
2
3
 
3
4
  module RESTinPeace
4
5
  class TemplateSanitizer
@@ -16,10 +17,14 @@ module RESTinPeace
16
17
  return @url if @url
17
18
  @url = @url_template.dup
18
19
  tokens.each do |token|
19
- param = @params.delete(token.to_sym)
20
+ param = @params[token.to_sym]
20
21
  param ||= @klass.send(token) if @klass.respond_to?(token)
21
22
  raise IncompleteParams, "No parameter for token :#{token} found" unless param
22
- @url.sub!(%r{:#{token}}, CGI.escape(param.to_s))
23
+ if @params.include?(token.to_sym)
24
+ @url.sub!(%r{:#{token}}, CGI.escape(param.to_s))
25
+ else
26
+ @url.sub!(%r{:#{token}}, Addressable::URI.parse(param.to_s).normalize.to_s)
27
+ end
23
28
  end
24
29
  @url
25
30
  end
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.require_paths = ['lib']
19
19
 
20
20
  s.add_runtime_dependency 'activemodel', '>= 3.2', '< 6'
21
+ s.add_runtime_dependency 'addressable', '~> 2.5'
21
22
 
22
23
  s.add_development_dependency 'rake', '~> 10.0'
23
24
  s.add_development_dependency 'rspec', '~> 3.0'
@@ -5,7 +5,6 @@ describe RESTinPeace::TemplateSanitizer do
5
5
  let(:attributes) { {} }
6
6
  let(:template_sanitizer) { RESTinPeace::TemplateSanitizer.new(url_template, params, klass) }
7
7
 
8
-
9
8
  context 'with class' do
10
9
  let(:klass) { OpenStruct }
11
10
 
@@ -18,6 +17,12 @@ describe RESTinPeace::TemplateSanitizer do
18
17
  specify { expect(subject).to eq('/a/1') }
19
18
  end
20
19
 
20
+ context 'single token with extra params' do
21
+ let(:params) { { id: 1, something: 'else' } }
22
+ let(:url_template) { '/a/:id' }
23
+ specify { expect(subject).to eq('/a/1') }
24
+ end
25
+
21
26
  context 'multiple token' do
22
27
  let(:params) { { id: 2, a_id: 1 } }
23
28
  let(:url_template) { '/a/:a_id/b/:id' }
@@ -118,7 +123,7 @@ describe RESTinPeace::TemplateSanitizer do
118
123
  specify { expect { subject }.to raise_error(RESTinPeace::TemplateSanitizer::IncompleteParams) }
119
124
  end
120
125
 
121
- context 'param which need to be encoded' do
126
+ context 'param which need to be encoded is white space separated string' do
122
127
  let(:params) { { id: 'this param need to be encoded' } }
123
128
  let(:encoded_param) { 'this+param+need+to+be+encoded' }
124
129
  let(:attributes) { { a_id: 2 } }
@@ -126,6 +131,23 @@ describe RESTinPeace::TemplateSanitizer do
126
131
  specify { expect(subject).to eq("/a/2/b/#{encoded_param}") }
127
132
  end
128
133
 
134
+ context 'path which need to be encoded is white space separated string' do
135
+ let(:params) { { id: 1 } }
136
+ let(:attributes) { { a_name: 'with space' } }
137
+ let(:encoded_path) { 'with%20space' }
138
+ let(:url_template) { '/a/:a_name/b/:id' }
139
+ specify { expect(subject).to eq("/a/#{encoded_path}/b/1") }
140
+ end
141
+
142
+ context 'path and param which need to be encoded are white space separated string' do
143
+ let(:params) { { place: 'dhaini kata' } }
144
+ let(:encoded_param) { 'dhaini+kata' }
145
+ let(:attributes) { { a_name: 'with space' } }
146
+ let(:encoded_path) { 'with%20space' }
147
+ let(:url_template) { '/a/:a_name/b/:place' }
148
+ specify { expect(subject).to eq("/a/#{encoded_path}/b/#{encoded_param}") }
149
+ end
150
+
129
151
  context 'param which need to be encoded is null string' do
130
152
  let(:params) { { id: '' } }
131
153
  let(:encoded_param) { '' }
@@ -134,6 +156,13 @@ describe RESTinPeace::TemplateSanitizer do
134
156
  specify { expect(subject).to eq("/a/2/b/#{encoded_param}") }
135
157
  end
136
158
 
159
+ context 'path which need to be encoded is null string' do
160
+ let(:params) { { id: 1 } }
161
+ let(:attributes) { { a_name: '' } }
162
+ let(:url_template) { '/a/:a_name/b/:id' }
163
+ specify { expect(subject).to eq('/a//b/1') }
164
+ end
165
+
137
166
  context 'param which need to be encoded is nil' do
138
167
  let(:params) { { id: nil } }
139
168
  let(:encoded_param) { '' }
@@ -141,6 +170,13 @@ describe RESTinPeace::TemplateSanitizer do
141
170
  let(:url_template) { '/a/:a_id/b/:id' }
142
171
  specify { expect { subject }.to raise_exception(RESTinPeace::TemplateSanitizer::IncompleteParams) }
143
172
  end
173
+
174
+ context 'path which need to be encoded is nil' do
175
+ let(:params) { { id: 1 } }
176
+ let(:attributes) { { a_name: nil } }
177
+ let(:url_template) { '/a/:a_name/b/:id' }
178
+ specify { expect { subject }.to raise_exception(RESTinPeace::TemplateSanitizer::IncompleteParams) }
179
+ end
144
180
  end
145
181
 
146
182
  describe '#tokens' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-in-peace
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.3
4
+ version: 6.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raffael Schmid
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-09 00:00:00.000000000 Z
11
+ date: 2017-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -30,6 +30,20 @@ dependencies:
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '6'
33
+ - !ruby/object:Gem::Dependency
34
+ name: addressable
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.5'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.5'
33
47
  - !ruby/object:Gem::Dependency
34
48
  name: rake
35
49
  requirement: !ruby/object:Gem::Requirement
@@ -142,6 +156,7 @@ files:
142
156
  - gemfiles/Gemfile.activemodel-4.1.x
143
157
  - gemfiles/Gemfile.activemodel-4.2.x
144
158
  - gemfiles/Gemfile.activemodel-5.0.x
159
+ - gemfiles/Gemfile.activemodel-5.1.x
145
160
  - images/rest_in_peace.gif
146
161
  - lib/rest-in-peace.rb
147
162
  - lib/rest_in_peace.rb
@@ -191,14 +206,4 @@ rubygems_version: 2.5.2
191
206
  signing_key:
192
207
  specification_version: 4
193
208
  summary: REST in peace
194
- test_files:
195
- - spec/rest_in_peace/active_model_api_spec.rb
196
- - spec/rest_in_peace/api_call_spec.rb
197
- - spec/rest_in_peace/definition_proxy/attributes_definitions_spec.rb
198
- - spec/rest_in_peace/definition_proxy/collection_method_definitions_spec.rb
199
- - spec/rest_in_peace/definition_proxy/resource_method_definitions_spec.rb
200
- - spec/rest_in_peace/definition_proxy_spec.rb
201
- - spec/rest_in_peace/response_converter_spec.rb
202
- - spec/rest_in_peace/template_sanitizer_spec.rb
203
- - spec/rest_in_peace_spec.rb
204
- - spec/spec_helper.rb
209
+ test_files: []