shutl_resource 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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, :logger
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
@@ -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,6 +1,33 @@
1
1
  require 'open-uri'
2
2
  module Shutl::Resource
3
3
  module RestClassMethods
4
+ def base_uri uri
5
+ @base_uri = uri
6
+ end
7
+
8
+ def connection
9
+ @connection ||= Faraday.new(:url => @base_uri || Shutl::Resource.base_uri) do |faraday|
10
+ faraday.request :url_encoded # form-encode POST params
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
+
17
+ if Shutl::Resource.logger
18
+ faraday.use :extended_logging, logger: Shutl::Resource.logger
19
+ end
20
+ end
21
+ end
22
+
23
+ def headers
24
+ {
25
+ 'Accept' => 'application/json',
26
+ 'Content-Type' => 'application/json',
27
+ 'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}"
28
+ }
29
+ end
30
+
4
31
  def find(args = {}, params = {})
5
32
  params = args if @singular_resource
6
33
  auth_options = { auth: params.delete(:auth), from: params.delete(:from) }
@@ -15,13 +42,13 @@ module Shutl::Resource
15
42
  url = member_url args.dup, params
16
43
  end
17
44
 
18
- response = get url, headers_with_auth(auth_options)
45
+ response = connection.get(url) do |req|
46
+ req.headers = headers_with_auth(auth_options)
47
+ end
19
48
 
20
49
  check_fail response, "Failed to find #{name}! args: #{args}, params: #{params}"
21
50
 
22
- parsed = response.parsed_response
23
-
24
- including_parent_attributes = parsed[@resource_name].merge args
51
+ including_parent_attributes = response.body[@resource_name].merge args
25
52
  new_object including_parent_attributes, response
26
53
  end
27
54
 
@@ -29,40 +56,41 @@ module Shutl::Resource
29
56
  url = generate_collection_url attributes
30
57
  attributes.delete "response"
31
58
 
32
- response = post(
33
- url,
34
- { body: { @resource_name => attributes }.to_json }.merge(headers_with_auth(options))
35
- )
59
+ response = connection.post(url) do |req|
60
+ req.headers = headers_with_auth(options)
61
+ req.body = { @resource_name => attributes }.to_json
62
+ end
36
63
 
37
64
  check_fail response, "Create failed"
38
65
 
39
- parsed = response.parsed_response || {}
66
+ parsed = response.body || {}
40
67
  attributes = parsed[@resource_name] || {}
41
68
 
42
69
  new_object attributes, response
43
70
  end
44
71
 
45
72
  def destroy instance, options = {}
46
- message = "Failed to destroy #{name.downcase.pluralize}"
73
+ failure_message = "Failed to destroy #{name.downcase.pluralize}"
47
74
 
48
75
  perform_action(
49
76
  instance,
50
77
  :delete,
78
+ {},
51
79
  headers_with_auth(options),
52
- message
80
+ failure_message
53
81
  ).success?
54
82
  end
55
83
 
56
84
  def save instance, options = {}
57
- #TODO: this is sometimes a hash and sometimes a Rest - need to rethink this
58
85
  attributes = instance.attributes rescue instance
59
86
 
60
- payload = {
61
- body: { @resource_name => convert_new_id(attributes) }.to_json
62
- }
87
+ body = { @resource_name => convert_new_id(attributes) }.to_json
63
88
 
64
- payload_with_headers = payload.merge(headers_with_auth options)
65
- response = perform_action(instance, :put, payload, "Save failed")
89
+ response = perform_action(instance,
90
+ :put,
91
+ body,
92
+ headers_with_auth(options),
93
+ "Save failed")
66
94
 
67
95
  response.success?
68
96
  end
@@ -80,11 +108,13 @@ module Shutl::Resource
80
108
  params = partition.last.inject({}) { |h, pair| h[pair.first] = pair.last; h }
81
109
 
82
110
  url = generate_collection_url url_args, params
83
- response = get url, headers_with_auth(auth_options)
111
+ response = connection.get(url) do |req|
112
+ req.headers = headers_with_auth(auth_options)
113
+ end
84
114
 
85
115
  check_fail response, "Failed to find all #{name.downcase.pluralize}"
86
116
 
87
- response_object = response.parsed_response[@resource_name.pluralize].map do |h|
117
+ response_object = response.body[@resource_name.pluralize].map do |h|
88
118
  new_object(args.merge(h), response)
89
119
  end
90
120
  if order_collection?
@@ -95,7 +125,7 @@ module Shutl::Resource
95
125
  end
96
126
  end
97
127
 
98
- RestCollection.new(response_object, response.parsed_response['pagination'])
128
+ RestCollection.new(response_object, response.body['pagination'])
99
129
  end
100
130
 
101
131
  class RestCollection
@@ -205,15 +235,17 @@ module Shutl::Resource
205
235
  h['Authorization'] = "Bearer #{options[:auth]}" if options[:auth]
206
236
  h['From'] = "#{options[:from]}" if options[:from]
207
237
  end
208
- { headers: headers }
209
238
  end
210
239
 
211
- def perform_action instance, verb, args, failure_message
240
+ def perform_action instance, verb, body, headers, failure_message
212
241
  attributes = instance.is_a?(Hash) ? instance : instance.attributes
213
242
  attributes.delete "response" #used in debugging requests/responses
214
243
 
215
244
  url = member_url attributes
216
- response = send verb, url, args
245
+ response = connection.send(verb, url) do |req|
246
+ req.headers = headers
247
+ req.body = body
248
+ end
217
249
 
218
250
  check_fail response, failure_message
219
251
 
@@ -224,7 +256,7 @@ module Shutl::Resource
224
256
  instance = new add_resource_id_to(args), response
225
257
 
226
258
  instance.tap do |i|
227
- parsed_response = response.parsed_response
259
+ parsed_response = response.body
228
260
 
229
261
  if errors = (parsed_response and parsed_response["errors"])
230
262
  i.errors = errors
@@ -233,8 +265,7 @@ module Shutl::Resource
233
265
  end
234
266
 
235
267
  def check_fail response, message
236
- c = response.code
237
- failure_klass = case c
268
+ failure_klass = case response.status
238
269
  when 299
239
270
  if Shutl::Resource.raise_exceptions_on_no_quotes_generated
240
271
  Shutl::NoQuotesGenerated
@@ -265,7 +296,7 @@ module Shutl::Resource
265
296
 
266
297
 
267
298
  output = begin
268
- response.parsed_response["errors"]["base"]
299
+ response.body["errors"]["base"]
269
300
  rescue
270
301
  message
271
302
  end
@@ -278,8 +309,6 @@ module Shutl::Resource
278
309
  def generate_url!(url_pattern, args, params = {})
279
310
  url = url_pattern.dup
280
311
 
281
- url = "#{Shutl::Resource.base_uri}#{url}" unless self.base_uri
282
-
283
312
  args, url = replace_args_from_pattern! args, url
284
313
 
285
314
  url = URI.escape url
@@ -1,5 +1,5 @@
1
1
  module Shutl
2
2
  module Resource
3
- VERSION = '1.0.0'
3
+ VERSION = '1.1.0'
4
4
  end
5
5
  end
@@ -1,6 +1,10 @@
1
1
  module Shutl
2
2
  end
3
3
 
4
+ require 'faraday'
5
+ require 'faraday_middleware'
6
+ require "faraday/conductivity"
7
+
4
8
  require 'shutl_auth'
5
9
  require 'shutl/resource/version'
6
10
  require 'shutl/resource/configuration'
@@ -21,6 +21,9 @@ 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'
26
+ gem.add_dependency 'faraday-conductivity'
24
27
 
25
28
  gem.add_development_dependency 'rake'
26
29
  gem.add_development_dependency 'rspec', '~> 2.11.0'
@@ -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'
@@ -81,7 +81,7 @@ describe Shutl::Resource::Rest do
81
81
  end
82
82
 
83
83
  it 'should support the params with invalid uri name' do
84
- request = stub_request(:get, 'http://host/nested/10/resources?arg1=val1&arg%202=val2').
84
+ request = stub_request(:get, 'http://host/nested/10/resources?arg1=val1&arg%2B2=val2').
85
85
  to_return(body: '{"nested_resources": []}', headers: headers)
86
86
 
87
87
  NestedResource.all(parent_id: 10, arg1: 'val1', :'arg 2' => 'val2')
@@ -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', 'Content-Type' => 'application/json' }
6
- end
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,3 @@ 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
- require 'support/double_logger'
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.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-08-02 00:00:00.000000000 Z
14
+ date: 2013-08-19 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: httparty
@@ -61,6 +61,54 @@ dependencies:
61
61
  - - ! '>='
62
62
  - !ruby/object:Gem::Version
63
63
  version: '0'
64
+ - !ruby/object:Gem::Dependency
65
+ name: faraday
66
+ requirement: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ type: :runtime
73
+ prerelease: false
74
+ version_requirements: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ - !ruby/object:Gem::Dependency
81
+ name: faraday_middleware
82
+ requirement: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ - !ruby/object:Gem::Dependency
97
+ name: faraday-conductivity
98
+ requirement: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
64
112
  - !ruby/object:Gem::Dependency
65
113
  name: rake
66
114
  requirement: !ruby/object:Gem::Requirement
@@ -158,7 +206,6 @@ files:
158
206
  - spec/rest_resource_spec.rb
159
207
  - spec/spec_helper.rb
160
208
  - spec/support/configured_base_uri_resource.rb
161
- - spec/support/double_logger.rb
162
209
  - spec/support/test_resource.rb
163
210
  - spec/support/test_singular_resource.rb
164
211
  homepage: ''
@@ -173,21 +220,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
173
220
  - - ! '>='
174
221
  - !ruby/object:Gem::Version
175
222
  version: '0'
176
- segments:
177
- - 0
178
- hash: 2862234294544768530
179
223
  required_rubygems_version: !ruby/object:Gem::Requirement
180
224
  none: false
181
225
  requirements:
182
226
  - - ! '>='
183
227
  - !ruby/object:Gem::Version
184
228
  version: '0'
185
- segments:
186
- - 0
187
- hash: 2862234294544768530
188
229
  requirements: []
189
230
  rubyforge_project:
190
- rubygems_version: 1.8.25
231
+ rubygems_version: 1.8.23
191
232
  signing_key:
192
233
  specification_version: 3
193
234
  summary: Manage Shutl Rest resource. Parse/Serialize JSON
@@ -199,6 +240,5 @@ test_files:
199
240
  - spec/rest_resource_spec.rb
200
241
  - spec/spec_helper.rb
201
242
  - spec/support/configured_base_uri_resource.rb
202
- - spec/support/double_logger.rb
203
243
  - spec/support/test_resource.rb
204
244
  - 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