shutl_resource 0.10.6 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/shutl/resource/configuration.rb +1 -9
- data/lib/shutl/resource/rest.rb +0 -6
- data/lib/shutl/resource/rest_class_methods.rb +55 -29
- data/lib/shutl/resource/version.rb +1 -1
- data/lib/shutl_resource.rb +4 -1
- data/shutl_resource.gemspec +2 -0
- data/spec/configuration_spec.rb +0 -12
- data/spec/rest_resource_spec.rb +3 -8
- data/spec/spec_helper.rb +4 -1
- metadata +39 -19
- data/spec/support/double_logger.rb +0 -14
@@ -28,19 +28,11 @@ module Shutl::Resource
|
|
28
28
|
|
29
29
|
module Configuration
|
30
30
|
class << self
|
31
|
-
attr_accessor :base_uri
|
31
|
+
attr_accessor :base_uri, :logging_enabled
|
32
32
|
|
33
33
|
def configure
|
34
34
|
yield self
|
35
35
|
end
|
36
|
-
|
37
|
-
def logger
|
38
|
-
@logger ||= Logger.new($stdout)
|
39
|
-
end
|
40
|
-
|
41
|
-
def logger=(logger)
|
42
|
-
@logger = logger
|
43
|
-
end
|
44
36
|
end
|
45
37
|
end
|
46
38
|
end
|
data/lib/shutl/resource/rest.rb
CHANGED
@@ -14,14 +14,8 @@ module Shutl::Resource
|
|
14
14
|
attr_reader :response
|
15
15
|
|
16
16
|
def self.included(base)
|
17
|
-
base.send :include, HTTParty
|
18
17
|
base.send :extend, Shutl::Resource::RestClassMethods
|
19
18
|
|
20
|
-
base.send :headers, {
|
21
|
-
'Accept' => 'application/json',
|
22
|
-
'Content-Type' => 'application/json'
|
23
|
-
}
|
24
|
-
|
25
19
|
base.send :resource_name, base.name.split('::').last.underscore
|
26
20
|
base.send :resource_id, :id
|
27
21
|
end
|
@@ -1,5 +1,29 @@
|
|
1
1
|
module Shutl::Resource
|
2
2
|
module RestClassMethods
|
3
|
+
def base_uri uri
|
4
|
+
@base_uri = uri
|
5
|
+
end
|
6
|
+
|
7
|
+
def connection
|
8
|
+
@connection ||= Faraday.new(:url => @base_uri || Shutl::Resource.base_uri) do |faraday|
|
9
|
+
faraday.request :url_encoded # form-encode POST params
|
10
|
+
faraday.response :logger if Shutl::Resource.logging_enabled
|
11
|
+
faraday.response :json
|
12
|
+
|
13
|
+
# faraday.ssl[:ca_file] = ENV["SSL_CERT_FILE"]
|
14
|
+
|
15
|
+
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def headers
|
20
|
+
{
|
21
|
+
'Accept' => 'application/json',
|
22
|
+
'Content-Type' => 'application/json',
|
23
|
+
'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}"
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
3
27
|
def find(args = {}, params = {})
|
4
28
|
params = args if @singular_resource
|
5
29
|
auth_options = { auth: params.delete(:auth), from: params.delete(:from) }
|
@@ -14,13 +38,13 @@ module Shutl::Resource
|
|
14
38
|
url = member_url args.dup, params
|
15
39
|
end
|
16
40
|
|
17
|
-
response = get
|
41
|
+
response = connection.get(url) do |req|
|
42
|
+
req.headers = headers_with_auth(auth_options)
|
43
|
+
end
|
18
44
|
|
19
45
|
check_fail response, "Failed to find #{name}! args: #{args}, params: #{params}"
|
20
46
|
|
21
|
-
|
22
|
-
|
23
|
-
including_parent_attributes = parsed[@resource_name].merge args
|
47
|
+
including_parent_attributes = response.body[@resource_name].merge args
|
24
48
|
new_object including_parent_attributes, response
|
25
49
|
end
|
26
50
|
|
@@ -28,40 +52,41 @@ module Shutl::Resource
|
|
28
52
|
url = generate_collection_url attributes
|
29
53
|
attributes.delete "response"
|
30
54
|
|
31
|
-
response = post(
|
32
|
-
|
33
|
-
|
34
|
-
|
55
|
+
response = connection.post(url) do |req|
|
56
|
+
req.headers = headers_with_auth(options)
|
57
|
+
req.body = { @resource_name => attributes }.to_json
|
58
|
+
end
|
35
59
|
|
36
60
|
check_fail response, "Create failed"
|
37
61
|
|
38
|
-
parsed = response.
|
62
|
+
parsed = response.body || {}
|
39
63
|
attributes = parsed[@resource_name] || {}
|
40
64
|
|
41
65
|
new_object attributes, response
|
42
66
|
end
|
43
67
|
|
44
68
|
def destroy instance, options = {}
|
45
|
-
|
69
|
+
failure_message = "Failed to destroy #{name.downcase.pluralize}"
|
46
70
|
|
47
71
|
perform_action(
|
48
72
|
instance,
|
49
73
|
:delete,
|
74
|
+
{},
|
50
75
|
headers_with_auth(options),
|
51
|
-
|
76
|
+
failure_message
|
52
77
|
).success?
|
53
78
|
end
|
54
79
|
|
55
80
|
def save instance, options = {}
|
56
|
-
#TODO: this is sometimes a hash and sometimes a Rest - need to rethink this
|
57
81
|
attributes = instance.attributes rescue instance
|
58
82
|
|
59
|
-
|
60
|
-
body: { @resource_name => convert_new_id(attributes) }.to_json
|
61
|
-
}
|
83
|
+
body = { @resource_name => convert_new_id(attributes) }.to_json
|
62
84
|
|
63
|
-
|
64
|
-
|
85
|
+
response = perform_action(instance,
|
86
|
+
:put,
|
87
|
+
body,
|
88
|
+
headers_with_auth(options),
|
89
|
+
"Save failed")
|
65
90
|
|
66
91
|
response.success?
|
67
92
|
end
|
@@ -79,11 +104,13 @@ module Shutl::Resource
|
|
79
104
|
params = partition.last.inject({}) { |h, pair| h[pair.first] = pair.last; h }
|
80
105
|
|
81
106
|
url = generate_collection_url url_args, params
|
82
|
-
response = get
|
107
|
+
response = connection.get(url) do |req|
|
108
|
+
req.headers = headers_with_auth(auth_options)
|
109
|
+
end
|
83
110
|
|
84
111
|
check_fail response, "Failed to find all #{name.downcase.pluralize}"
|
85
112
|
|
86
|
-
response_object = response.
|
113
|
+
response_object = response.body[@resource_name.pluralize].map do |h|
|
87
114
|
new_object(args.merge(h), response)
|
88
115
|
end
|
89
116
|
if order_collection?
|
@@ -94,7 +121,7 @@ module Shutl::Resource
|
|
94
121
|
end
|
95
122
|
end
|
96
123
|
|
97
|
-
RestCollection.new(response_object, response.
|
124
|
+
RestCollection.new(response_object, response.body['pagination'])
|
98
125
|
end
|
99
126
|
|
100
127
|
class RestCollection
|
@@ -204,15 +231,17 @@ module Shutl::Resource
|
|
204
231
|
h['Authorization'] = "Bearer #{options[:auth]}" if options[:auth]
|
205
232
|
h['From'] = "#{options[:from]}" if options[:from]
|
206
233
|
end
|
207
|
-
{ headers: headers }
|
208
234
|
end
|
209
235
|
|
210
|
-
def perform_action instance, verb,
|
236
|
+
def perform_action instance, verb, body, headers, failure_message
|
211
237
|
attributes = instance.is_a?(Hash) ? instance : instance.attributes
|
212
238
|
attributes.delete "response" #used in debugging requests/responses
|
213
239
|
|
214
240
|
url = member_url attributes
|
215
|
-
response = send
|
241
|
+
response = connection.send(verb, url) do |req|
|
242
|
+
req.headers = headers
|
243
|
+
req.body = body
|
244
|
+
end
|
216
245
|
|
217
246
|
check_fail response, failure_message
|
218
247
|
|
@@ -223,7 +252,7 @@ module Shutl::Resource
|
|
223
252
|
instance = new add_resource_id_to(args), response
|
224
253
|
|
225
254
|
instance.tap do |i|
|
226
|
-
parsed_response = response.
|
255
|
+
parsed_response = response.body
|
227
256
|
|
228
257
|
if errors = (parsed_response and parsed_response["errors"])
|
229
258
|
i.errors = errors
|
@@ -232,8 +261,7 @@ module Shutl::Resource
|
|
232
261
|
end
|
233
262
|
|
234
263
|
def check_fail response, message
|
235
|
-
|
236
|
-
failure_klass = case c
|
264
|
+
failure_klass = case response.status
|
237
265
|
when 299
|
238
266
|
if Shutl::Resource.raise_exceptions_on_no_quotes_generated
|
239
267
|
Shutl::NoQuotesGenerated
|
@@ -264,7 +292,7 @@ module Shutl::Resource
|
|
264
292
|
|
265
293
|
|
266
294
|
output = begin
|
267
|
-
response.
|
295
|
+
response.body["errors"]["base"]
|
268
296
|
rescue
|
269
297
|
message
|
270
298
|
end
|
@@ -277,8 +305,6 @@ module Shutl::Resource
|
|
277
305
|
def generate_url!(url_pattern, args, params = {})
|
278
306
|
url = url_pattern.dup
|
279
307
|
|
280
|
-
url = "#{Shutl::Resource.base_uri}#{url}" unless self.base_uri
|
281
|
-
|
282
308
|
args, url = replace_args_from_pattern! args, url
|
283
309
|
|
284
310
|
url = URI.escape url
|
data/lib/shutl_resource.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
module Shutl
|
2
2
|
end
|
3
3
|
|
4
|
+
require 'faraday'
|
5
|
+
require 'faraday_middleware'
|
6
|
+
|
4
7
|
require 'shutl_auth'
|
5
8
|
require 'shutl/resource/configuration'
|
6
9
|
require 'shutl/resource/rest'
|
@@ -10,7 +13,7 @@ require 'shutl/resource/errors'
|
|
10
13
|
module Shutl::Resource
|
11
14
|
extend self
|
12
15
|
|
13
|
-
delegate :
|
16
|
+
delegate :logging_enabled, :logging_enabled=, :base_uri, :base_uri=, to: Configuration
|
14
17
|
|
15
18
|
def configure(*args, &block)
|
16
19
|
Configuration.configure(*args, &block)
|
data/shutl_resource.gemspec
CHANGED
@@ -21,6 +21,8 @@ Gem::Specification.new do |gem|
|
|
21
21
|
gem.add_dependency 'httparty', '~> 0.10.0'
|
22
22
|
gem.add_dependency 'shutl_auth', '0.8.0'
|
23
23
|
gem.add_dependency 'activemodel'
|
24
|
+
gem.add_dependency 'faraday'
|
25
|
+
gem.add_dependency 'faraday_middleware'
|
24
26
|
|
25
27
|
gem.add_development_dependency 'rake'
|
26
28
|
gem.add_development_dependency 'rspec', '~> 2.11.0'
|
data/spec/configuration_spec.rb
CHANGED
@@ -2,18 +2,6 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Shutl::Resource do
|
4
4
|
describe '#configure' do
|
5
|
-
let(:logger) { Shutl::Resource.logger }
|
6
|
-
|
7
|
-
it 'should configure the logger' do
|
8
|
-
logger = stub('logger')
|
9
|
-
|
10
|
-
Shutl::Resource.configure do |config|
|
11
|
-
config.logger = logger
|
12
|
-
end
|
13
|
-
|
14
|
-
Shutl::Resource.logger.should == logger
|
15
|
-
end
|
16
|
-
|
17
5
|
it "allows for configuration of the base uri" do
|
18
6
|
Shutl::Resource.configure do |config|
|
19
7
|
config.base_uri = 'base uri'
|
data/spec/rest_resource_spec.rb
CHANGED
@@ -2,14 +2,9 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Shutl::Resource::Rest do
|
4
4
|
let(:headers) do
|
5
|
-
{ 'Accept' => 'application/json',
|
6
|
-
|
7
|
-
|
8
|
-
it 'should include the REST verb' do
|
9
|
-
TestRest.should respond_to :get
|
10
|
-
TestRest.should respond_to :post
|
11
|
-
TestRest.should respond_to :post
|
12
|
-
TestRest.should respond_to :delete
|
5
|
+
{ 'Accept' => 'application/json',
|
6
|
+
'Content-Type' => 'application/json',
|
7
|
+
'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}" }
|
13
8
|
end
|
14
9
|
|
15
10
|
let(:resource) { TestRest.new(a: 'a', b: 2) }
|
data/spec/spec_helper.rb
CHANGED
@@ -5,4 +5,7 @@ require 'support/test_resource'
|
|
5
5
|
require 'support/test_resource'
|
6
6
|
require 'support/configured_base_uri_resource'
|
7
7
|
require 'support/test_singular_resource'
|
8
|
-
|
8
|
+
|
9
|
+
Shutl::Resource.configure do |config|
|
10
|
+
config.logging_enabled = false
|
11
|
+
end
|
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: 0.
|
4
|
+
version: 0.11.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -15,7 +15,7 @@ date: 2013-07-19 00:00:00.000000000 Z
|
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: httparty
|
18
|
-
requirement: &
|
18
|
+
requirement: &70223946241460 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|
20
20
|
requirements:
|
21
21
|
- - ~>
|
@@ -23,10 +23,10 @@ dependencies:
|
|
23
23
|
version: 0.10.0
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
|
-
version_requirements: *
|
26
|
+
version_requirements: *70223946241460
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: shutl_auth
|
29
|
-
requirement: &
|
29
|
+
requirement: &70223946238100 !ruby/object:Gem::Requirement
|
30
30
|
none: false
|
31
31
|
requirements:
|
32
32
|
- - =
|
@@ -34,10 +34,10 @@ dependencies:
|
|
34
34
|
version: 0.8.0
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
|
-
version_requirements: *
|
37
|
+
version_requirements: *70223946238100
|
38
38
|
- !ruby/object:Gem::Dependency
|
39
39
|
name: activemodel
|
40
|
-
requirement: &
|
40
|
+
requirement: &70223946237240 !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
43
|
- - ! '>='
|
@@ -45,10 +45,32 @@ dependencies:
|
|
45
45
|
version: '0'
|
46
46
|
type: :runtime
|
47
47
|
prerelease: false
|
48
|
-
version_requirements: *
|
48
|
+
version_requirements: *70223946237240
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: faraday
|
51
|
+
requirement: &70223946267260 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
type: :runtime
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *70223946267260
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: faraday_middleware
|
62
|
+
requirement: &70223946264740 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :runtime
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: *70223946264740
|
49
71
|
- !ruby/object:Gem::Dependency
|
50
72
|
name: rake
|
51
|
-
requirement: &
|
73
|
+
requirement: &70223946262780 !ruby/object:Gem::Requirement
|
52
74
|
none: false
|
53
75
|
requirements:
|
54
76
|
- - ! '>='
|
@@ -56,10 +78,10 @@ dependencies:
|
|
56
78
|
version: '0'
|
57
79
|
type: :development
|
58
80
|
prerelease: false
|
59
|
-
version_requirements: *
|
81
|
+
version_requirements: *70223946262780
|
60
82
|
- !ruby/object:Gem::Dependency
|
61
83
|
name: rspec
|
62
|
-
requirement: &
|
84
|
+
requirement: &70223946289820 !ruby/object:Gem::Requirement
|
63
85
|
none: false
|
64
86
|
requirements:
|
65
87
|
- - ~>
|
@@ -67,10 +89,10 @@ dependencies:
|
|
67
89
|
version: 2.11.0
|
68
90
|
type: :development
|
69
91
|
prerelease: false
|
70
|
-
version_requirements: *
|
92
|
+
version_requirements: *70223946289820
|
71
93
|
- !ruby/object:Gem::Dependency
|
72
94
|
name: debugger
|
73
|
-
requirement: &
|
95
|
+
requirement: &70223946284400 !ruby/object:Gem::Requirement
|
74
96
|
none: false
|
75
97
|
requirements:
|
76
98
|
- - ! '>='
|
@@ -78,10 +100,10 @@ dependencies:
|
|
78
100
|
version: '0'
|
79
101
|
type: :development
|
80
102
|
prerelease: false
|
81
|
-
version_requirements: *
|
103
|
+
version_requirements: *70223946284400
|
82
104
|
- !ruby/object:Gem::Dependency
|
83
105
|
name: webmock
|
84
|
-
requirement: &
|
106
|
+
requirement: &70223946301380 !ruby/object:Gem::Requirement
|
85
107
|
none: false
|
86
108
|
requirements:
|
87
109
|
- - ~>
|
@@ -89,7 +111,7 @@ dependencies:
|
|
89
111
|
version: 1.8.7
|
90
112
|
type: :development
|
91
113
|
prerelease: false
|
92
|
-
version_requirements: *
|
114
|
+
version_requirements: *70223946301380
|
93
115
|
description: Shutl Rest resource
|
94
116
|
email:
|
95
117
|
- davidr@shutl.co.uk
|
@@ -123,7 +145,6 @@ files:
|
|
123
145
|
- spec/rest_resource_spec.rb
|
124
146
|
- spec/spec_helper.rb
|
125
147
|
- spec/support/configured_base_uri_resource.rb
|
126
|
-
- spec/support/double_logger.rb
|
127
148
|
- spec/support/test_resource.rb
|
128
149
|
- spec/support/test_singular_resource.rb
|
129
150
|
homepage: ''
|
@@ -140,7 +161,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
140
161
|
version: '0'
|
141
162
|
segments:
|
142
163
|
- 0
|
143
|
-
hash: -
|
164
|
+
hash: -4070568380889602956
|
144
165
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
166
|
none: false
|
146
167
|
requirements:
|
@@ -149,7 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
170
|
version: '0'
|
150
171
|
segments:
|
151
172
|
- 0
|
152
|
-
hash: -
|
173
|
+
hash: -4070568380889602956
|
153
174
|
requirements: []
|
154
175
|
rubyforge_project:
|
155
176
|
rubygems_version: 1.8.11
|
@@ -164,6 +185,5 @@ test_files:
|
|
164
185
|
- spec/rest_resource_spec.rb
|
165
186
|
- spec/spec_helper.rb
|
166
187
|
- spec/support/configured_base_uri_resource.rb
|
167
|
-
- spec/support/double_logger.rb
|
168
188
|
- spec/support/test_resource.rb
|
169
189
|
- spec/support/test_singular_resource.rb
|
@@ -1,14 +0,0 @@
|
|
1
|
-
|
2
|
-
module Shutl::Resource
|
3
|
-
class NoLogger
|
4
|
-
def debug(message) ; end
|
5
|
-
def info(message) ; end
|
6
|
-
def warn(message) ; end
|
7
|
-
def error(message) ; end
|
8
|
-
def fatal(message) ; end
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
Shutl::Resource.configure do |config|
|
13
|
-
config.logger = Shutl::Resource::NoLogger.new
|
14
|
-
end
|