shutl_resource 1.6.0 → 1.7.0

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.
@@ -89,22 +89,37 @@ module Shutl::Resource
89
89
  save args, options
90
90
  end
91
91
 
92
+ def split_hash h
93
+ partitions = h.partition { |key, value| yield key, value }
94
+ [Hash[partitions.first], Hash[partitions.last]]
95
+ end
96
+
97
+ def split_url_args args
98
+ split_hash(args) do |key, value|
99
+ !remote_collection_url.index(":#{key}").nil?
100
+ end
101
+ end
102
+
103
+ def split_header_args args
104
+ split_hash(args) do |key, value|
105
+ %(auth headers from).include?(key.to_s)
106
+ end
107
+ end
92
108
 
93
109
  def all(args = {})
94
- partition = args.partition { |key, value| !remote_collection_url.index(":#{key}").nil? }
110
+ header_args, non_header_args = split_header_args args
111
+ url_args, params = split_url_args non_header_args
95
112
 
96
- url_args = partition.first.inject({}) { |h, pair| h[pair.first] = pair.last; h }
97
- params = partition.last.inject({}) { |h, pair| h[pair.first] = pair.last; h }
113
+ url = generate_collection_url url_args, params
98
114
 
99
- url = generate_collection_url url_args, params
100
115
  response = connection.get(url) do |req|
101
- req.headers = generate_request_header(header_options(args))
116
+ req.headers = generate_request_header(header_options(header_args))
102
117
  end
103
118
 
104
119
  check_fail response, "Failed to find all #{name.downcase.pluralize}"
105
120
 
106
121
  response_object = response.body[@resource_name.pluralize].map do |h|
107
- new_object(args.merge(h), response.body)
122
+ new_object(non_header_args.merge(h), response.body)
108
123
  end
109
124
  if order_collection?
110
125
  response_object.sort! do |a,b|
@@ -1,5 +1,5 @@
1
1
  module Shutl
2
2
  module Resource
3
- VERSION = '1.6.0'
3
+ VERSION = '1.7.0'
4
4
  end
5
5
  end
@@ -3,11 +3,11 @@ require 'spec_helper'
3
3
  describe Shutl::Resource::Rest do
4
4
  let(:expected_headers) do
5
5
  {
6
- headers:
6
+ headers:
7
7
  {
8
- 'Accept' => 'application/json',
9
- 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
10
- 'Authorization' => 'some auth',
8
+ 'Accept' => 'application/json',
9
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
10
+ 'Authorization' => 'some auth',
11
11
  'Content-Type' => 'application/json+consumer',
12
12
  'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}",
13
13
  'Consumer-Access-Token' => 'ConsumerAccessToken'
@@ -35,8 +35,8 @@ describe Shutl::Resource::Rest do
35
35
  let(:resource) { TestSingularResource.new }
36
36
  let(:expected_headers) {
37
37
  {
38
- 'Accept' => 'application/json',
39
- 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
38
+ 'Accept' => 'application/json',
39
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
40
40
  'Authorization' => 'Bearer some_auth',
41
41
  'Content-Type' => 'application/json',
42
42
  'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}",
@@ -87,21 +87,21 @@ describe Shutl::Resource::Rest do
87
87
  post_headers = expected_headers[:headers].except("Accept-Encoding")
88
88
  @request = stub_request(:post, 'http://host/test_singular_resources')
89
89
  .with(post_headers.merge(body: body))
90
- .to_return(:status => 200,
90
+ .to_return(:status => 200,
91
91
  :headers => response_headers)
92
92
  end
93
-
93
+
94
94
  it 'queries the endpoint' do
95
95
  TestSingularResource.create(attributes, headers: headers)
96
96
  @request.should have_been_requested
97
- end
98
- end
97
+ end
98
+ end
99
99
  end
100
100
 
101
101
  describe "#destroy" do
102
102
  let(:destroy_headers) { expected_headers[:headers].except("Accept-Encoding") }
103
-
104
- before do
103
+
104
+ before do
105
105
  @request = stub_request(:delete, 'http://host/test_singular_resources/a')
106
106
  .with(destroy_headers)
107
107
  end
@@ -109,12 +109,12 @@ describe Shutl::Resource::Rest do
109
109
  it 'queries the endpoint' do
110
110
  TestSingularResource.destroy(id: 'a')
111
111
  @request.should have_been_requested
112
- end
113
- end
112
+ end
113
+ end
114
114
 
115
115
  describe "#save" do
116
116
  let(:save_headers) { expected_headers[:headers].except("Accept-Encoding") }
117
- before do
117
+ before do
118
118
  @request = stub_request(:put, 'http://host/test_rests/a')
119
119
  .with(save_headers)
120
120
  end
@@ -122,7 +122,7 @@ describe Shutl::Resource::Rest do
122
122
  it 'queries the endpoint' do
123
123
  resource.save
124
124
  @request.should have_been_requested
125
- end
125
+ end
126
126
  end
127
127
 
128
128
  describe '#update!' do
@@ -145,7 +145,7 @@ describe Shutl::Resource::Rest do
145
145
 
146
146
  describe '#all' do
147
147
  let(:body) { { test_rests: [{ a: "a", b: 2 }]}.to_json }
148
-
148
+
149
149
  before do
150
150
  @request = stub_request(:get, 'http://host/test_rests')
151
151
  .with(expected_headers)
@@ -156,6 +156,5 @@ describe Shutl::Resource::Rest do
156
156
  TestRest.all headers: headers
157
157
  @request.should have_been_requested
158
158
  end
159
-
160
159
  end
161
160
  end
@@ -294,7 +294,6 @@ describe Shutl::Resource::Rest do
294
294
  to_return(:status => 403)
295
295
 
296
296
  lambda { TestRest.all }.should raise_error(Shutl::ForbiddenAccess)
297
-
298
297
  end
299
298
 
300
299
  context 'ordering the collection' do
@@ -331,6 +330,41 @@ describe Shutl::Resource::Rest do
331
330
  @request.should have_been_requested
332
331
  end
333
332
  end
333
+
334
+ describe 'response attributes' do
335
+ before do
336
+ stub_request(:get, "http://host/test_rests").
337
+ with(:headers => {
338
+ 'Authorization'=>'Bearer TOKEN',
339
+ 'Accept'=>'application/json',
340
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
341
+ 'Content-Type'=>'application/json',
342
+ 'User-Agent'=>"Shutl Resource Gem v#{Shutl::Resource::VERSION}"}).
343
+ to_return(:status => 200, :body => '{"test_rests": [{ "a": "a", "b": 2 }]}', :headers => {})
344
+ end
345
+
346
+ let(:result) do
347
+ TestRest.all(
348
+ auth: 'TOKEN', 'auth' => 'TOKEN',
349
+ headers: {some: 'headers'}, 'headers' => {some: 'headers'},
350
+ from: 'bob@example.com', 'from' => 'bob@example.com')
351
+ end
352
+
353
+ it 'should not have auth token' do
354
+ result.first.attributes.should_not have_key('auth')
355
+ result.first.attributes.should_not have_key(:auth)
356
+ end
357
+
358
+ it 'should not have headers' do
359
+ result.first.attributes.should_not have_key('headers')
360
+ result.first.attributes.should_not have_key(:headers)
361
+ end
362
+
363
+ it 'should not have from email' do
364
+ result.first.attributes.should_not have_key('from')
365
+ result.first.attributes.should_not have_key(:from)
366
+ end
367
+ end
334
368
  end
335
369
 
336
370
  describe '.create' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shutl_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 1.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2014-08-01 00:00:00.000000000 Z
17
+ date: 2014-09-10 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: shutl_auth
@@ -217,18 +217,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
217
217
  - - ! '>='
218
218
  - !ruby/object:Gem::Version
219
219
  version: '0'
220
- segments:
221
- - 0
222
- hash: -3278622488598740442
223
220
  required_rubygems_version: !ruby/object:Gem::Requirement
224
221
  none: false
225
222
  requirements:
226
223
  - - ! '>='
227
224
  - !ruby/object:Gem::Version
228
225
  version: '0'
229
- segments:
230
- - 0
231
- hash: -3278622488598740442
232
226
  requirements: []
233
227
  rubyforge_project:
234
228
  rubygems_version: 1.8.23