shutl_resource 1.4.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OTc0YmQyNmIzNmU0NmE2YTcwOTUxZGM5ZjJhOTNiYTFkNzU3ODVmMQ==
5
+ data.tar.gz: !binary |-
6
+ ZDJmNTdlNjFlYzE1YTNhZTk3MGE0Yzg5YjEzMTliNmUzNTAwZjFhNQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YWMzNGQ5MmVjODEzMWU5NzM3ODc2MTQzYmZjOTJkMmUxOWU3MGYyZjBkOWIw
10
+ ZTZjZTkyOGQwMWZhMTNmMGJiNTZiZWU5YTJlMjYzZjE0MmYyNTgyZWJkZmQ2
11
+ Njg4ZTA0YjkwOGYxZDQ3MzYzMTRjZTY5YTE0N2QyNmIyNzE5OTk=
12
+ data.tar.gz: !binary |-
13
+ NGJkYzk2NTU0OTRjZWZjYmFjYWYwNDdjN2ZlN2I1ZDdmZDY5YTE2N2EwZDEx
14
+ MWEzMjE0YTUxMTNhYTYyNjUyOTg5ZTAyODYzZDQyMTFlZjczMjA1ZTg2ZWJh
15
+ MmNlOGIxMjlmNTg3MGVmMDUxYWRiMzQ2NTMzYTJlZjUzOGZmMjk=
data/.travis.yml CHANGED
@@ -1,15 +1,7 @@
1
1
  language: ruby
2
-
3
2
  rvm:
4
- - 1.9.2
5
3
  - 1.9.3
6
- - jruby-19mode
7
-
4
+ - jruby-19mode # JRuby in 1.9 mode
8
5
  jdk:
9
6
  - openjdk7
10
- - oraclejdk7
11
-
12
- matrix:
13
- allow_failures:
14
- - rvm: jruby-19mode
15
-
7
+ - oraclejdk7
@@ -36,10 +36,10 @@ module Shutl::Resource
36
36
  attrs.each { |a, v| instance_variable_set(:"@#{a}", v) }
37
37
  end
38
38
 
39
- def update!(attrs)
39
+ def update!(attrs, headers = {})
40
40
  new_attributes = attributes.merge attrs
41
41
  update_attributes(self.class.add_resource_id_to new_attributes)
42
- save
42
+ save(headers)
43
43
  end
44
44
 
45
45
  def save options={}
@@ -19,17 +19,8 @@ module Shutl::Resource
19
19
  end
20
20
  end
21
21
 
22
- def headers
23
- {
24
- 'Accept' => 'application/json',
25
- 'Content-Type' => 'application/json',
26
- 'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}"
27
- }
28
- end
29
-
30
22
  def find(args = {}, params = {})
31
23
  params = args if @singular_resource
32
- auth_options = { auth: params.delete(:auth), from: params.delete(:from) }
33
24
 
34
25
  if @singular_resource
35
26
  url = singular_member_url params
@@ -42,7 +33,7 @@ module Shutl::Resource
42
33
  end
43
34
 
44
35
  response = connection.get(url) do |req|
45
- req.headers = headers_with_auth(auth_options)
36
+ req.headers = generate_request_header(header_options(params))
46
37
  end
47
38
 
48
39
  check_fail response, "Failed to find #{name}! args: #{args}, params: #{params}"
@@ -56,7 +47,7 @@ module Shutl::Resource
56
47
  attributes.delete "response"
57
48
 
58
49
  response = connection.post(url) do |req|
59
- req.headers = headers_with_auth(options)
50
+ req.headers = generate_request_header(header_options(options))
60
51
  req.body = { @resource_name => attributes }.to_json
61
52
  end
62
53
 
@@ -75,7 +66,7 @@ module Shutl::Resource
75
66
  instance,
76
67
  :delete,
77
68
  {}.to_json,
78
- headers_with_auth(options),
69
+ generate_request_header(header_options(options)),
79
70
  failure_message
80
71
  ).success?
81
72
  end
@@ -88,7 +79,7 @@ module Shutl::Resource
88
79
  response = perform_action(instance,
89
80
  :put,
90
81
  body,
91
- headers_with_auth(options),
82
+ generate_request_header(header_options(options)),
92
83
  "Save failed")
93
84
 
94
85
  response.success?
@@ -100,7 +91,6 @@ module Shutl::Resource
100
91
 
101
92
 
102
93
  def all(args = {})
103
- auth_options = { auth: args.delete(:auth), from: args.delete(:from) }
104
94
  partition = args.partition { |key, value| !remote_collection_url.index(":#{key}").nil? }
105
95
 
106
96
  url_args = partition.first.inject({}) { |h, pair| h[pair.first] = pair.last; h }
@@ -108,7 +98,7 @@ module Shutl::Resource
108
98
 
109
99
  url = generate_collection_url url_args, params
110
100
  response = connection.get(url) do |req|
111
- req.headers = headers_with_auth(auth_options)
101
+ req.headers = generate_request_header(header_options(args))
112
102
  end
113
103
 
114
104
  check_fail response, "Failed to find all #{name.downcase.pluralize}"
@@ -229,10 +219,38 @@ module Shutl::Resource
229
219
 
230
220
  private
231
221
 
222
+
223
+ def headers
224
+ {
225
+ 'Accept' => 'application/json',
226
+ 'Content-Type' => 'application/json',
227
+ 'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}"
228
+ }
229
+ end
230
+
231
+ def header_options params
232
+ header_opts = params[:headers] || {}
233
+ header_opts.merge!(authorization: params[:auth]) if params[:auth]
234
+ header_opts.merge!(from: params[:from]) if params[:from]
235
+ header_opts
236
+ end
237
+
238
+
239
+ def generate_request_header header_options = {}
240
+ header_options.inject(headers) do |h, (k,v)|
241
+ h[header_name(k.to_s)] = v if v
242
+ h
243
+ end
244
+ end
245
+
246
+ def header_name header_key
247
+ header_key.split(%r{\_|\-}).map {|e| e.capitalize }.join("-")
248
+ end
249
+
232
250
  def headers_with_auth options = {}
233
251
  headers.tap do |h|
234
252
  h['Authorization'] = "Bearer #{options[:auth]}" if options[:auth]
235
- h['From'] = "#{options[:from]}" if options[:from]
253
+ h['From'] = "#{options[:from]}" if options[:from]
236
254
  end
237
255
  end
238
256
 
@@ -313,6 +331,7 @@ module Shutl::Resource
313
331
  args, url = replace_args_from_pattern! args, url
314
332
 
315
333
  url = URI.escape url
334
+ params = params.except(:headers, :auth, :from)
316
335
  unless params.empty?
317
336
  url += '?' + params.entries.map do |key, value|
318
337
  URI::encode "#{key}=#{value}"
@@ -1,5 +1,5 @@
1
1
  module Shutl
2
2
  module Resource
3
- VERSION = '1.4.0'
3
+ VERSION = '1.4.1'
4
4
  end
5
5
  end
@@ -4,8 +4,8 @@ require File.expand_path('../lib/shutl/resource/version', __FILE__)
4
4
  $platform ||= RUBY_PLATFORM[/java/] || 'ruby'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
- gem.authors = ["David Rouchy", "Volker Pacher", "Mark Burns"]
8
- gem.email = ["davidr@shutl.co.uk", "volker@shutl.com", "mark@shutl.com"]
7
+ gem.authors = ["David Rouchy", "Volker Pacher", "Mark Burns", "Cosimo Ranieri"]
8
+ gem.email = ["davidr@shutl.co.uk", "volker@shutl.com", "mark@shutl.com", "cosimo@shutl.com"]
9
9
  gem.description = %q{Shutl Rest resource}
10
10
  gem.summary = %q{Manage Shutl Rest resource. Parse/Serialize JSON}
11
11
  gem.homepage = ""
@@ -0,0 +1,135 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shutl::Resource::Rest do
4
+ let(:expected_headers) do
5
+ {
6
+ headers:
7
+ {
8
+ 'Accept' => 'application/json',
9
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
10
+ 'Authorization' => 'some auth',
11
+ 'Content-Type' => 'application/json+consumer',
12
+ 'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}",
13
+ 'Consumer-Access-Token' => 'ConsumerAccessToken'
14
+ }
15
+ }
16
+ end
17
+
18
+ let(:response_headers) do
19
+ { 'Accept' => 'application/json',
20
+ 'Content-Type' => 'application/json',
21
+ 'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}" }
22
+ end
23
+
24
+ let(:resource) { TestRest.new(a: 'a', b: 2) }
25
+
26
+ let(:headers) {
27
+ {
28
+ authorization: "some auth",
29
+ content_type: "application/json+consumer",
30
+ consumer_access_token: "ConsumerAccessToken"
31
+ }
32
+ }
33
+
34
+ describe '#find' do
35
+ context "with a singular resource" do
36
+ let(:resource) { TestSingularResource.new }
37
+
38
+ before do
39
+ @request = stub_request(:get, 'http://host/test_singular_resource')
40
+ .with(expected_headers)
41
+ .to_return(:status => 200,
42
+ :body => '{"test_singular_resource": { "a": "a", "b": 2 }}',
43
+ :headers => response_headers)
44
+
45
+ end
46
+
47
+ it 'queries the endpoint' do
48
+ TestSingularResource.find(headers: headers)
49
+ @request.should have_been_requested
50
+ end
51
+ end
52
+ end
53
+
54
+ describe ".create" do
55
+
56
+ context "With the setting to not raise exceptions" do
57
+ let(:resource) { TestSingularResource.new }
58
+ let(:attributes) { { :a => 1 } }
59
+ let(:body) { { test_singular_resource: { a: 1 } }.to_json }
60
+ before do
61
+ post_headers = expected_headers[:headers].except("Accept-Encoding")
62
+ @request = stub_request(:post, 'http://host/test_singular_resources')
63
+ .with(post_headers.merge(body: body))
64
+ .to_return(:status => 200,
65
+ :headers => response_headers)
66
+ end
67
+
68
+ it 'queries the endpoint' do
69
+ TestSingularResource.create(attributes, headers: headers)
70
+ @request.should have_been_requested
71
+ end
72
+ end
73
+ end
74
+
75
+ describe "#destroy" do
76
+ let(:destroy_headers) { expected_headers[:headers].except("Accept-Encoding") }
77
+
78
+ before do
79
+ @request = stub_request(:delete, 'http://host/test_singular_resources/a')
80
+ .with(destroy_headers)
81
+ end
82
+
83
+ it 'queries the endpoint' do
84
+ TestSingularResource.destroy(id: 'a')
85
+ @request.should have_been_requested
86
+ end
87
+ end
88
+
89
+ describe "#save" do
90
+ let(:save_headers) { expected_headers[:headers].except("Accept-Encoding") }
91
+ before do
92
+ @request = stub_request(:put, 'http://host/test_rests/a')
93
+ .with(save_headers)
94
+ end
95
+
96
+ it 'queries the endpoint' do
97
+ resource.save
98
+ @request.should have_been_requested
99
+ end
100
+ end
101
+
102
+ describe '#update!' do
103
+ let(:update_headers) { expected_headers[:headers].except("Accept-Encoding") }
104
+ let(:attributes) { { a: 'a', b: 'b' } }
105
+ let(:body) { { test_rest: { a: "a", b: "b", id: "a" } }.to_json }
106
+
107
+ before do
108
+ @request = stub_request(:put, "http://host/test_rests/a").
109
+ with(:body => body,
110
+ :headers => update_headers).
111
+ to_return(:status => 200, :body => "", :headers => {})
112
+ end
113
+
114
+ it 'queries the endpoint' do
115
+ resource.update!(attributes, headers: headers)
116
+ @request.should have_been_requested
117
+ end
118
+ end
119
+
120
+ describe '#all' do
121
+ let(:body) { { test_rests: [{ a: "a", b: 2 }]}.to_json }
122
+
123
+ before do
124
+ @request = stub_request(:get, 'http://host/test_rests')
125
+ .with(expected_headers)
126
+ .to_return(:status => 200, body: body,:headers => headers)
127
+ end
128
+
129
+ it 'queries the endpoint' do
130
+ TestRest.all headers: headers
131
+ @request.should have_been_requested
132
+ end
133
+
134
+ end
135
+ end
@@ -2,9 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  describe Shutl::Resource::Rest do
4
4
  let(:headers) do
5
- { 'Accept' => 'application/json',
5
+ { 'Accept' => 'application/json',
6
6
  'Content-Type' => 'application/json',
7
- 'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}" }
7
+ 'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}" }
8
8
  end
9
9
 
10
10
  let(:resource) { TestRest.new(a: 'a', b: 2) }
@@ -12,9 +12,6 @@ describe Shutl::Resource::Rest do
12
12
  describe '#find' do
13
13
  context "with a singular resource" do
14
14
  let(:resource) { TestSingularResource.new }
15
- let(:headers_with_auth) do
16
- headers.merge("Authorization" => "Bearer some auth")
17
- end
18
15
 
19
16
  before do
20
17
  @request = stub_request(:get, 'http://host/test_singular_resource').
@@ -25,7 +22,6 @@ describe Shutl::Resource::Rest do
25
22
 
26
23
  it 'queries the endpoint' do
27
24
  TestSingularResource.find(auth: "some auth")
28
-
29
25
  @request.should have_been_requested
30
26
  end
31
27
 
metadata CHANGED
@@ -1,22 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shutl_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
5
- prerelease:
4
+ version: 1.4.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - David Rouchy
9
8
  - Volker Pacher
10
9
  - Mark Burns
10
+ - Cosimo Ranieri
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-10-09 00:00:00.000000000 Z
14
+ date: 2013-11-05 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: shutl_auth
18
18
  requirement: !ruby/object:Gem::Requirement
19
- none: false
20
19
  requirements:
21
20
  - - ~>
22
21
  - !ruby/object:Gem::Version
@@ -24,7 +23,6 @@ dependencies:
24
23
  type: :runtime
25
24
  prerelease: false
26
25
  version_requirements: !ruby/object:Gem::Requirement
27
- none: false
28
26
  requirements:
29
27
  - - ~>
30
28
  - !ruby/object:Gem::Version
@@ -32,7 +30,6 @@ dependencies:
32
30
  - !ruby/object:Gem::Dependency
33
31
  name: activemodel
34
32
  requirement: !ruby/object:Gem::Requirement
35
- none: false
36
33
  requirements:
37
34
  - - ! '>='
38
35
  - !ruby/object:Gem::Version
@@ -40,7 +37,6 @@ dependencies:
40
37
  type: :runtime
41
38
  prerelease: false
42
39
  version_requirements: !ruby/object:Gem::Requirement
43
- none: false
44
40
  requirements:
45
41
  - - ! '>='
46
42
  - !ruby/object:Gem::Version
@@ -48,7 +44,6 @@ dependencies:
48
44
  - !ruby/object:Gem::Dependency
49
45
  name: faraday
50
46
  requirement: !ruby/object:Gem::Requirement
51
- none: false
52
47
  requirements:
53
48
  - - ! '>='
54
49
  - !ruby/object:Gem::Version
@@ -56,7 +51,6 @@ dependencies:
56
51
  type: :runtime
57
52
  prerelease: false
58
53
  version_requirements: !ruby/object:Gem::Requirement
59
- none: false
60
54
  requirements:
61
55
  - - ! '>='
62
56
  - !ruby/object:Gem::Version
@@ -64,7 +58,6 @@ dependencies:
64
58
  - !ruby/object:Gem::Dependency
65
59
  name: faraday_middleware
66
60
  requirement: !ruby/object:Gem::Requirement
67
- none: false
68
61
  requirements:
69
62
  - - ! '>='
70
63
  - !ruby/object:Gem::Version
@@ -72,7 +65,6 @@ dependencies:
72
65
  type: :runtime
73
66
  prerelease: false
74
67
  version_requirements: !ruby/object:Gem::Requirement
75
- none: false
76
68
  requirements:
77
69
  - - ! '>='
78
70
  - !ruby/object:Gem::Version
@@ -80,7 +72,6 @@ dependencies:
80
72
  - !ruby/object:Gem::Dependency
81
73
  name: faraday-conductivity
82
74
  requirement: !ruby/object:Gem::Requirement
83
- none: false
84
75
  requirements:
85
76
  - - ! '>='
86
77
  - !ruby/object:Gem::Version
@@ -88,7 +79,6 @@ dependencies:
88
79
  type: :runtime
89
80
  prerelease: false
90
81
  version_requirements: !ruby/object:Gem::Requirement
91
- none: false
92
82
  requirements:
93
83
  - - ! '>='
94
84
  - !ruby/object:Gem::Version
@@ -96,7 +86,6 @@ dependencies:
96
86
  - !ruby/object:Gem::Dependency
97
87
  name: rake
98
88
  requirement: !ruby/object:Gem::Requirement
99
- none: false
100
89
  requirements:
101
90
  - - ! '>='
102
91
  - !ruby/object:Gem::Version
@@ -104,7 +93,6 @@ dependencies:
104
93
  type: :development
105
94
  prerelease: false
106
95
  version_requirements: !ruby/object:Gem::Requirement
107
- none: false
108
96
  requirements:
109
97
  - - ! '>='
110
98
  - !ruby/object:Gem::Version
@@ -112,7 +100,6 @@ dependencies:
112
100
  - !ruby/object:Gem::Dependency
113
101
  name: rspec
114
102
  requirement: !ruby/object:Gem::Requirement
115
- none: false
116
103
  requirements:
117
104
  - - ~>
118
105
  - !ruby/object:Gem::Version
@@ -120,7 +107,6 @@ dependencies:
120
107
  type: :development
121
108
  prerelease: false
122
109
  version_requirements: !ruby/object:Gem::Requirement
123
- none: false
124
110
  requirements:
125
111
  - - ~>
126
112
  - !ruby/object:Gem::Version
@@ -128,7 +114,6 @@ dependencies:
128
114
  - !ruby/object:Gem::Dependency
129
115
  name: debugger
130
116
  requirement: !ruby/object:Gem::Requirement
131
- none: false
132
117
  requirements:
133
118
  - - ! '>='
134
119
  - !ruby/object:Gem::Version
@@ -136,7 +121,6 @@ dependencies:
136
121
  type: :development
137
122
  prerelease: false
138
123
  version_requirements: !ruby/object:Gem::Requirement
139
- none: false
140
124
  requirements:
141
125
  - - ! '>='
142
126
  - !ruby/object:Gem::Version
@@ -144,7 +128,6 @@ dependencies:
144
128
  - !ruby/object:Gem::Dependency
145
129
  name: webmock
146
130
  requirement: !ruby/object:Gem::Requirement
147
- none: false
148
131
  requirements:
149
132
  - - ~>
150
133
  - !ruby/object:Gem::Version
@@ -152,7 +135,6 @@ dependencies:
152
135
  type: :development
153
136
  prerelease: false
154
137
  version_requirements: !ruby/object:Gem::Requirement
155
- none: false
156
138
  requirements:
157
139
  - - ~>
158
140
  - !ruby/object:Gem::Version
@@ -162,6 +144,7 @@ email:
162
144
  - davidr@shutl.co.uk
163
145
  - volker@shutl.com
164
146
  - mark@shutl.com
147
+ - cosimo@shutl.com
165
148
  executables: []
166
149
  extensions: []
167
150
  extra_rdoc_files: []
@@ -193,6 +176,7 @@ files:
193
176
  - spec/dynamic_resource_spec.rb
194
177
  - spec/error_spec.rb
195
178
  - spec/remote_url_spec.rb
179
+ - spec/request_headers_spec.rb
196
180
  - spec/rest_resource_spec.rb
197
181
  - spec/spec_helper.rb
198
182
  - spec/support/configured_base_uri_resource.rb
@@ -200,33 +184,26 @@ files:
200
184
  - spec/support/test_singular_resource.rb
201
185
  homepage: ''
202
186
  licenses: []
187
+ metadata: {}
203
188
  post_install_message:
204
189
  rdoc_options: []
205
190
  require_paths:
206
191
  - lib
207
192
  required_ruby_version: !ruby/object:Gem::Requirement
208
- none: false
209
193
  requirements:
210
194
  - - ! '>='
211
195
  - !ruby/object:Gem::Version
212
196
  version: '0'
213
- segments:
214
- - 0
215
- hash: -4184138435452899409
216
197
  required_rubygems_version: !ruby/object:Gem::Requirement
217
- none: false
218
198
  requirements:
219
199
  - - ! '>='
220
200
  - !ruby/object:Gem::Version
221
201
  version: '0'
222
- segments:
223
- - 0
224
- hash: -4184138435452899409
225
202
  requirements: []
226
203
  rubyforge_project:
227
- rubygems_version: 1.8.23
204
+ rubygems_version: 2.1.4
228
205
  signing_key:
229
- specification_version: 3
206
+ specification_version: 4
230
207
  summary: Manage Shutl Rest resource. Parse/Serialize JSON
231
208
  test_files:
232
209
  - spec/configuration_spec.rb
@@ -234,6 +211,7 @@ test_files:
234
211
  - spec/dynamic_resource_spec.rb
235
212
  - spec/error_spec.rb
236
213
  - spec/remote_url_spec.rb
214
+ - spec/request_headers_spec.rb
237
215
  - spec/rest_resource_spec.rb
238
216
  - spec/spec_helper.rb
239
217
  - spec/support/configured_base_uri_resource.rb