active_rest_client 1.0.5 → 1.0.6

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: dd23c05a32d0575c6e4ff7d2334b7264ca1ee1db
4
- data.tar.gz: 4abca247cd1cc050a34f9f50edc60c82408fde93
3
+ metadata.gz: 3477c6e4b20c5133c874a8ef329c9e71f2d50be0
4
+ data.tar.gz: 7cdfcda9c6f33f0262228c80a5b1cdade5aee5ff
5
5
  SHA512:
6
- metadata.gz: e98ae059859804960a1a7030ef3662536787a4dda830318f4c35f5470198ae85f3d3f17857dd0a4d9d4291e71923f1f8f4a9ef25ef7ad613216d8a7b672cfc09
7
- data.tar.gz: cb70cc5f0ce3a0138f8d6767f1aa0a15b46eecaf75a473e9acd48de5f175c25388e8f7e9802d8a6e697fc3cc017437342c00e8381b34af7e1f08819cf63bb022
6
+ metadata.gz: 29e829f81a617090b82e56cb68a03f46ca92238bcc6c9f5d056ad524daa5822724ad65dc9a79f1fdebf60e642d270200bd379748257ba84771d9454167000743
7
+ data.tar.gz: 9b2b9b9de8634f62e9ff1a78879dd0d5fd1ec7272550ab4dfa7a6e2aee74e41cbe927f6e784f0abffb8f1ebab250cbd106351927642b86d7b7e4ba560a7ecbc4
data/README.md CHANGED
@@ -315,9 +315,9 @@ ActiveRestClient::Base.cache_store = Redis::Store.new("redis://localhost:6379/0/
315
315
 
316
316
  ### Using filters
317
317
 
318
- You can use filters to alter get/post parameters, the URL or set the post body (doing so overrides normal parameter insertion in to the body) before a request. This can either be a block or a named method (like ActionController's `before_filter`/`before_action` methods).
318
+ You can use filters to alter get/post parameters, the URL or set the post body (doing so overrides normal parameter insertion in to the body) before a request or to adjust the response after a request. This can either be a block or a named method (like ActionController's `before_filter`/`before_action` methods).
319
319
 
320
- The filter is passed the name of the method (e.g. `:save`) and a request object. The request object has four public attributes `post_params` (a Hash of the POST parameters), `get_params` (a Hash of the GET parameters), headers and `url` (a String containing the full URL without GET parameters appended)
320
+ The filter is passed the name of the method (e.g. `:save`) and an object (a request object for `before_filter` and a response object for `after_filter`). The request object has four public attributes `post_params` (a Hash of the POST parameters), `get_params` (a Hash of the GET parameters), headers and `url` (a String containing the full URL without GET parameters appended)
321
321
 
322
322
  ```ruby
323
323
  require 'secure_random'
@@ -368,6 +368,22 @@ class Person < MyProject::Base
368
368
  end
369
369
  ```
370
370
 
371
+ After filters work in exactly the same way:
372
+
373
+ ```ruby
374
+ class Person < ActiveRestClient::Base
375
+ after_filter :fix_empty_content
376
+
377
+ private
378
+
379
+ def fix_empty_content
380
+ if response.status == 204 && response.body.blank?
381
+ response.body = '{"empty": true}'
382
+ end
383
+ end
384
+ end
385
+ ```
386
+
371
387
  ### Lazy Loading
372
388
 
373
389
  ActiveRestClient supports lazy loading (delaying the actual API call until the response is actually used, so that views can be cached without still causing API calls).
@@ -69,8 +69,9 @@ module ActiveRestClient
69
69
  def translate(result, options = {})
70
70
  result.headers["content-type"] = "application/hal+json"
71
71
  result = OpenStruct.new(status:result.status, headers:result.headers, body:result.body)
72
- obj = MultiJson.load(result.body)
73
- result.body = yield obj
72
+ if result.body.present?
73
+ result.body = yield MultiJson.load(result.body)
74
+ end
74
75
  result
75
76
  end
76
77
 
@@ -99,9 +99,9 @@ module ActiveRestClient
99
99
  return handle_response(OpenStruct.new(status:200, body:fake, headers:{"X-ARC-Faked-Response" => "true"}))
100
100
  end
101
101
  if object_is_class?
102
- @object.send(:_filter_request, @method[:name], self)
102
+ @object.send(:_filter_request, :before, @method[:name], self)
103
103
  else
104
- @object.class.send(:_filter_request, @method[:name], self)
104
+ @object.class.send(:_filter_request, :before, @method[:name], self)
105
105
  end
106
106
  append_get_parameters
107
107
  prepare_request_body
@@ -126,6 +126,11 @@ module ActiveRestClient
126
126
  if object_is_class? && @object.record_response?
127
127
  @object.record_response(self.url, response)
128
128
  end
129
+ if object_is_class?
130
+ @object.send(:_filter_request, :after, @method[:name], response)
131
+ else
132
+ @object.class.send(:_filter_request, :after, @method[:name], response)
133
+ end
129
134
  result = handle_response(response, cached)
130
135
  original_object_class.write_cached_response(self, response, result)
131
136
  result
@@ -417,7 +422,7 @@ module ActiveRestClient
417
422
  if @response.body.is_a?(Array) || @response.body.is_a?(Hash)
418
423
  body = @response.body
419
424
  else
420
- body = MultiJson.load(@response.body) || {}
425
+ body = @response.body.blank? ? {} : MultiJson.load(@response.body)
421
426
  end
422
427
  body = begin
423
428
  @method[:name].nil? ? body : translator.send(@method[:name], body)
@@ -2,35 +2,45 @@ module ActiveRestClient
2
2
  module RequestFiltering
3
3
  module ClassMethods
4
4
  def before_request(method_name = nil, &block)
5
- @filters ||= []
5
+ @before_filters ||= []
6
6
  if block
7
- @filters << block
7
+ @before_filters << block
8
8
  elsif method_name
9
- @filters << method_name
9
+ @before_filters << method_name
10
10
  end
11
11
  end
12
12
 
13
- def _filter_request(name, request)
14
- _handle_super_class_filters(name, request)
15
- @filters ||= []
16
- @filters.each do |filter|
13
+ def after_request(method_name = nil, &block)
14
+ @after_filters ||= []
15
+ if block
16
+ @after_filters << block
17
+ elsif method_name
18
+ @after_filters << method_name
19
+ end
20
+ end
21
+
22
+ def _filter_request(type, name, param)
23
+ _handle_super_class_filters(type, name, param)
24
+ filters = (type == :before ? @before_filters : @after_filters)
25
+ filters ||= []
26
+ filters.each do |filter|
17
27
  if filter.is_a? Symbol
18
28
  if self.respond_to?(filter)
19
- self.send(filter, name, request)
29
+ self.send(filter, name, param)
20
30
  else
21
31
  instance = self.new
22
- instance.send(filter, name, request)
32
+ instance.send(filter, name, param)
23
33
  end
24
34
  else
25
- filter.call(name, request)
35
+ filter.call(name, param)
26
36
  end
27
37
  end
28
38
  end
29
39
 
30
- def _handle_super_class_filters(name, request)
40
+ def _handle_super_class_filters(type, name, request)
31
41
  @parents ||= []
32
42
  @parents.each do |parent|
33
- parent._filter_request(name, request)
43
+ parent._filter_request(type, name, request)
34
44
  end
35
45
  end
36
46
 
@@ -1,3 +1,3 @@
1
1
  module ActiveRestClient
2
- VERSION = "1.0.5"
2
+ VERSION = "1.0.6"
3
3
  end
@@ -204,7 +204,7 @@ describe ActiveRestClient::Base do
204
204
 
205
205
  it "runs filters as usual" do
206
206
  ActiveRestClient::Request.any_instance.should_receive(:do_request).with(any_args).and_return(OpenStruct.new(status:200, headers:{}, body:"{\"first_name\":\"Billy\"}"))
207
- EmptyExample.should_receive(:_filter_request).with(any_args)
207
+ EmptyExample.should_receive(:_filter_request).with(any_args).exactly(2).times
208
208
  EmptyExample._request("http://api.example.com/")
209
209
  end
210
210
 
@@ -120,6 +120,13 @@ describe ActiveRestClient::Base do
120
120
  expect(ret.first_name).to eq("Billy")
121
121
  end
122
122
 
123
+ it 'skips the altering of the response body when there is none' do
124
+ allow_any_instance_of(ActiveRestClient::Connection).to receive(:get).with('/change-format', instance_of(Hash))
125
+ .and_return(double(body: '', status: 200, headers: {}))
126
+ result = ProxyClientExample.change_format
127
+ expect(result._attributes).to be_empty
128
+ end
129
+
123
130
  it "can continue with the request in the normal way, passing it on to the server" do
124
131
  ActiveRestClient::Connection.any_instance.should_receive(:get).with("/not_proxied?id=1", instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", status:200, headers:{}))
125
132
  ProxyClientExample.not_proxied(id:1)
@@ -18,6 +18,8 @@ class RequestFilteringExample
18
18
  before_request :set_to_ssl
19
19
  before_request :set_via_instance
20
20
 
21
+ after_request :change_body
22
+
21
23
  private
22
24
 
23
25
  def self.set_to_ssl(name, request)
@@ -27,6 +29,10 @@ class RequestFilteringExample
27
29
  def set_via_instance(name, request)
28
30
  request.url.gsub!("//www", "//new")
29
31
  end
32
+
33
+ def change_body(name, response)
34
+ response.body = "{test: 1}"
35
+ end
30
36
  end
31
37
 
32
38
  class SubClassedRequestFilteringExample < RequestFilteringExample
@@ -37,36 +43,42 @@ end
37
43
 
38
44
  describe ActiveRestClient::RequestFiltering do
39
45
  let(:request) { OpenStruct.new(get_params:{}, post_params:{}, url:"http://www.example.com", headers:ActiveRestClient::HeadersList.new) }
46
+ let(:response) { OpenStruct.new(body:"") }
40
47
 
41
48
  it "should call through to adjust the parameters" do
42
- RequestFilteringExample._filter_request(:test, request)
49
+ RequestFilteringExample._filter_request(:before, :test, request)
43
50
  expect(request.get_params).to have_key(:filter1)
44
51
  end
45
52
 
46
53
  it "should call through for more than one filter" do
47
- RequestFilteringExample._filter_request(:test, request)
54
+ RequestFilteringExample._filter_request(:before, :test, request)
48
55
  expect(request.get_params).to have_key(:filter1)
49
56
  expect(request.post_params).to have_key(:post_filter1)
50
57
  end
51
58
 
52
59
  it "should allow adjusting the URL via a named filter" do
53
- RequestFilteringExample._filter_request(:test, request)
60
+ RequestFilteringExample._filter_request(:before, :test, request)
54
61
  expect(request.url).to match(/https:\/\//)
55
62
  end
56
63
 
57
64
  it "should allow adjusting the URL via a named filter as an instance method" do
58
- RequestFilteringExample._filter_request(:test, request)
65
+ RequestFilteringExample._filter_request(:before, :test, request)
59
66
  expect(request.url).to match(/\/\/new\./)
60
67
  end
61
68
 
62
69
  it "should allow filters to be set on the parent or on the child" do
63
- SubClassedRequestFilteringExample._filter_request(:test, request)
70
+ SubClassedRequestFilteringExample._filter_request(:before, :test, request)
64
71
  expect(request.url).to match(/\/\/new\./)
65
72
  expect(request.get_params[:api_key]).to eq(1234)
66
73
  end
67
74
 
68
75
  it "should allow filters to add custom headers" do
69
- RequestFilteringExample._filter_request(:test, request)
76
+ RequestFilteringExample._filter_request(:before, :test, request)
70
77
  expect(request.headers["X-My-Header"]).to eq("myvalue")
71
78
  end
79
+
80
+ it "should be able to alter the response body" do
81
+ RequestFilteringExample._filter_request(:after, :test, response)
82
+ expect(response.body).to eq("{test: 1}")
83
+ end
72
84
  end
@@ -13,10 +13,17 @@ describe ActiveRestClient::Request do
13
13
  end
14
14
  end
15
15
 
16
+ after_request do |name, response|
17
+ if name == :change
18
+ response.body = "{\"test\": 1}"
19
+ end
20
+ end
21
+
16
22
  get :all, "/", :has_many => {:expenses => ExampleOtherClient}
17
23
  get :babies, "/babies", :has_many => {:children => ExampleOtherClient}
18
24
  get :headers, "/headers"
19
25
  get :find, "/:id"
26
+ get :change, "/change"
20
27
  post :create, "/create"
21
28
  put :update, "/put/:id"
22
29
  delete :remove, "/remove/:id"
@@ -440,17 +447,23 @@ describe ActiveRestClient::Request do
440
447
 
441
448
  it "should send all class mapped methods through _filter_request" do
442
449
  ActiveRestClient::Connection.any_instance.should_receive(:get).with("/", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"first_name\":\"Johnny\", \"expenses\":[{\"amount\":1}, {\"amount\":2}]}", status:200, headers:{}))
443
- ExampleClient.should_receive(:_filter_request).with(any_args)
450
+ ExampleClient.should_receive(:_filter_request).with(any_args).exactly(2).times
444
451
  ExampleClient.all
445
452
  end
446
453
 
447
454
  it "should send all instance mapped methods through _filter_request" do
448
455
  ActiveRestClient::Connection.any_instance.should_receive(:get).with("/", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"first_name\":\"Johnny\", \"expenses\":[{\"amount\":1}, {\"amount\":2}]}", status:200, headers:{}))
449
- ExampleClient.should_receive(:_filter_request).with(any_args)
456
+ ExampleClient.should_receive(:_filter_request).with(any_args).exactly(2).times
450
457
  e = ExampleClient.new
451
458
  e.all
452
459
  end
453
460
 
461
+ it "should change the generated object if an after_filter changes it" do
462
+ ActiveRestClient::Connection.any_instance.should_receive(:get).with("/change", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"first_name\":\"Johnny\", \"expenses\":[{\"amount\":1}, {\"amount\":2}]}", status:200, headers:{}))
463
+ obj = ExampleClient.change
464
+ expect(obj.test).to eq(1)
465
+ end
466
+
454
467
  context "Direct URL requests" do
455
468
  class SameServerExampleClient < ActiveRestClient::Base
456
469
  URL = "http://www.example.com/some/url"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_rest_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Which Ltd
@@ -9,202 +9,202 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-01-06 00:00:00.000000000 Z
12
+ date: 2015-01-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ~>
18
+ - - "~>"
19
19
  - !ruby/object:Gem::Version
20
20
  version: '1.3'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ~>
25
+ - - "~>"
26
26
  - !ruby/object:Gem::Version
27
27
  version: '1.3'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - '>='
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
34
  version: '0'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - '>='
39
+ - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: rspec
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ~>
46
+ - - "~>"
47
47
  - !ruby/object:Gem::Version
48
48
  version: '2'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ~>
53
+ - - "~>"
54
54
  - !ruby/object:Gem::Version
55
55
  version: '2'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: webmock
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - '>='
60
+ - - ">="
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - '>='
67
+ - - ">="
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rspec_junit_formatter
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - '>='
74
+ - - ">="
75
75
  - !ruby/object:Gem::Version
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - '>='
81
+ - - ">="
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: simplecov
86
86
  requirement: !ruby/object:Gem::Requirement
87
87
  requirements:
88
- - - '>='
88
+ - - ">="
89
89
  - !ruby/object:Gem::Version
90
90
  version: '0'
91
91
  type: :development
92
92
  prerelease: false
93
93
  version_requirements: !ruby/object:Gem::Requirement
94
94
  requirements:
95
- - - '>='
95
+ - - ">="
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0'
98
98
  - !ruby/object:Gem::Dependency
99
99
  name: simplecov-rcov
100
100
  requirement: !ruby/object:Gem::Requirement
101
101
  requirements:
102
- - - '>='
102
+ - - ">="
103
103
  - !ruby/object:Gem::Version
104
104
  version: '0'
105
105
  type: :development
106
106
  prerelease: false
107
107
  version_requirements: !ruby/object:Gem::Requirement
108
108
  requirements:
109
- - - '>='
109
+ - - ">="
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
112
  - !ruby/object:Gem::Dependency
113
113
  name: guard-rspec
114
114
  requirement: !ruby/object:Gem::Requirement
115
115
  requirements:
116
- - - '>='
116
+ - - ">="
117
117
  - !ruby/object:Gem::Version
118
118
  version: '0'
119
119
  type: :development
120
120
  prerelease: false
121
121
  version_requirements: !ruby/object:Gem::Requirement
122
122
  requirements:
123
- - - '>='
123
+ - - ">="
124
124
  - !ruby/object:Gem::Version
125
125
  version: '0'
126
126
  - !ruby/object:Gem::Dependency
127
127
  name: terminal-notifier-guard
128
128
  requirement: !ruby/object:Gem::Requirement
129
129
  requirements:
130
- - - '>='
130
+ - - ">="
131
131
  - !ruby/object:Gem::Version
132
132
  version: '0'
133
133
  type: :development
134
134
  prerelease: false
135
135
  version_requirements: !ruby/object:Gem::Requirement
136
136
  requirements:
137
- - - '>='
137
+ - - ">="
138
138
  - !ruby/object:Gem::Version
139
139
  version: '0'
140
140
  - !ruby/object:Gem::Dependency
141
141
  name: coveralls
142
142
  requirement: !ruby/object:Gem::Requirement
143
143
  requirements:
144
- - - '>='
144
+ - - ">="
145
145
  - !ruby/object:Gem::Version
146
146
  version: '0'
147
147
  type: :development
148
148
  prerelease: false
149
149
  version_requirements: !ruby/object:Gem::Requirement
150
150
  requirements:
151
- - - '>='
151
+ - - ">="
152
152
  - !ruby/object:Gem::Version
153
153
  version: '0'
154
154
  - !ruby/object:Gem::Dependency
155
155
  name: multi_json
156
156
  requirement: !ruby/object:Gem::Requirement
157
157
  requirements:
158
- - - '>='
158
+ - - ">="
159
159
  - !ruby/object:Gem::Version
160
160
  version: '0'
161
161
  type: :runtime
162
162
  prerelease: false
163
163
  version_requirements: !ruby/object:Gem::Requirement
164
164
  requirements:
165
- - - '>='
165
+ - - ">="
166
166
  - !ruby/object:Gem::Version
167
167
  version: '0'
168
168
  - !ruby/object:Gem::Dependency
169
169
  name: activesupport
170
170
  requirement: !ruby/object:Gem::Requirement
171
171
  requirements:
172
- - - '>='
172
+ - - ">="
173
173
  - !ruby/object:Gem::Version
174
174
  version: '0'
175
175
  type: :runtime
176
176
  prerelease: false
177
177
  version_requirements: !ruby/object:Gem::Requirement
178
178
  requirements:
179
- - - '>='
179
+ - - ">="
180
180
  - !ruby/object:Gem::Version
181
181
  version: '0'
182
182
  - !ruby/object:Gem::Dependency
183
183
  name: faraday
184
184
  requirement: !ruby/object:Gem::Requirement
185
185
  requirements:
186
- - - '>='
186
+ - - ">="
187
187
  - !ruby/object:Gem::Version
188
188
  version: '0'
189
189
  type: :runtime
190
190
  prerelease: false
191
191
  version_requirements: !ruby/object:Gem::Requirement
192
192
  requirements:
193
- - - '>='
193
+ - - ">="
194
194
  - !ruby/object:Gem::Version
195
195
  version: '0'
196
196
  - !ruby/object:Gem::Dependency
197
197
  name: patron
198
198
  requirement: !ruby/object:Gem::Requirement
199
199
  requirements:
200
- - - '>='
200
+ - - ">="
201
201
  - !ruby/object:Gem::Version
202
202
  version: 0.4.9
203
203
  type: :development
204
204
  prerelease: false
205
205
  version_requirements: !ruby/object:Gem::Requirement
206
206
  requirements:
207
- - - '>='
207
+ - - ">="
208
208
  - !ruby/object:Gem::Version
209
209
  version: 0.4.9
210
210
  description: Accessing REST services in an ActiveRecord style
@@ -215,10 +215,10 @@ executables: []
215
215
  extensions: []
216
216
  extra_rdoc_files: []
217
217
  files:
218
- - .gitignore
219
- - .rspec
220
- - .simplecov
221
- - .travis.yml
218
+ - ".gitignore"
219
+ - ".rspec"
220
+ - ".simplecov"
221
+ - ".travis.yml"
222
222
  - CONTRIBUTING.md
223
223
  - Gemfile
224
224
  - Guardfile
@@ -275,17 +275,17 @@ require_paths:
275
275
  - lib
276
276
  required_ruby_version: !ruby/object:Gem::Requirement
277
277
  requirements:
278
- - - '>='
278
+ - - ">="
279
279
  - !ruby/object:Gem::Version
280
280
  version: '0'
281
281
  required_rubygems_version: !ruby/object:Gem::Requirement
282
282
  requirements:
283
- - - '>='
283
+ - - ">="
284
284
  - !ruby/object:Gem::Version
285
285
  version: '0'
286
286
  requirements: []
287
287
  rubyforge_project:
288
- rubygems_version: 2.0.14
288
+ rubygems_version: 2.2.2
289
289
  signing_key:
290
290
  specification_version: 4
291
291
  summary: This gem is for accessing REST services in an ActiveRecord style. ActiveResource
@@ -311,4 +311,3 @@ test_files:
311
311
  - spec/lib/result_iterator_spec.rb
312
312
  - spec/lib/validation_spec.rb
313
313
  - spec/spec_helper.rb
314
- has_rdoc: