shutl_resource 0.10.1 → 0.10.3

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.
@@ -1,21 +1,25 @@
1
1
  module Shutl::Resource
2
2
  module RestClassMethods
3
- def find(args, params = {})
4
- unless args.kind_of?(Hash)
3
+ def find(args = {}, params = {})
4
+ if @singular_resource
5
+ params = args
6
+ url = singular_member_url params
7
+ elsif !args.kind_of?(Hash)
5
8
  id = args
6
9
  args = { resource_id_name => id }
10
+ url = member_url args.dup, params
11
+ else
12
+ url = member_url args.dup, params
7
13
  end
14
+
8
15
  auth_options = { auth: params.delete(:auth), from: params.delete(:from) }
9
- url = member_url args.dup, params
10
16
  response = get url, headers_with_auth(auth_options)
11
17
 
12
-
13
18
  check_fail response, "Failed to find #{name} with the id #{id}"
14
19
 
15
20
  parsed = response.parsed_response
16
21
 
17
22
  including_parent_attributes = parsed[@resource_name].merge args
18
-
19
23
  new_object including_parent_attributes, response
20
24
  end
21
25
 
@@ -119,6 +123,10 @@ module Shutl::Resource
119
123
  end
120
124
  end
121
125
 
126
+ def singular_resource
127
+ @singular_resource = true
128
+ end
129
+
122
130
  def resource_name(name)
123
131
  instance_variable_set :@resource_name, name
124
132
  end
@@ -167,8 +175,13 @@ module Shutl::Resource
167
175
  args
168
176
  end
169
177
 
178
+ def singular_member_url params={}
179
+ generate_url! "/#{@resource_name}", params
180
+ end
181
+
170
182
  def member_url *args
171
183
  attributes = args.first.with_indifferent_access
184
+
172
185
  unless attributes[resource_id_name] ||= attributes[:id]
173
186
  raise ArgumentError, "Missing resource id with name: `#{resource_id_name}' for #{self}"
174
187
  end
@@ -1,5 +1,5 @@
1
1
  module Shutl
2
2
  module Resource
3
- VERSION = '0.10.1'
3
+ VERSION = '0.10.3'
4
4
  end
5
5
  end
@@ -15,6 +15,40 @@ describe Shutl::Resource::Rest do
15
15
  let(:resource) { TestRest.new(a: 'a', b: 2) }
16
16
 
17
17
  describe '#find' do
18
+ context "with a singular resource" do
19
+ let(:resource) { TestSingularResource.new }
20
+ let(:headers_with_auth) do
21
+ headers.merge("Authorization" => "Bearer some auth")
22
+ end
23
+
24
+ before do
25
+ @request = stub_request(:get, 'http://host/test_singular_resource').
26
+ to_return(:status => 200,
27
+ :body => '{"test_singular_resource": { "a": "a", "b": 2 }}',
28
+ :headers => headers)
29
+ end
30
+
31
+ it 'queries the endpoint' do
32
+ TestSingularResource.find(auth: "some auth")
33
+
34
+ @request.should have_been_requested
35
+ end
36
+
37
+ it 'should parse the result of the body to create an object' do
38
+ resource = TestSingularResource.find(auth: "some auth")
39
+
40
+ resource.should_not be_nil
41
+ resource.should be_kind_of TestSingularResource
42
+ end
43
+
44
+ it 'should assign the attributes based on the json returned' do
45
+ resource = TestSingularResource.find(auth: "some auth")
46
+
47
+ resource.a.should == 'a'
48
+ resource.b.should == 2
49
+ end
50
+ end
51
+
18
52
  context 'with no arguments' do
19
53
  before do
20
54
  @request = stub_request(:get, 'http://host/test_rests/a').
data/spec/spec_helper.rb CHANGED
@@ -2,4 +2,5 @@ require 'shutl_resource'
2
2
  require 'webmock/rspec'
3
3
 
4
4
  require 'support/test_resource'
5
+ require 'support/test_singular_resource'
5
6
  require 'support/double_logger'
@@ -0,0 +1,5 @@
1
+ class TestSingularResource
2
+ include Shutl::Resource::Rest
3
+ base_uri "http://host"
4
+ singular_resource
5
+ 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.10.1
4
+ version: 0.10.3
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-07-04 00:00:00.000000000 Z
14
+ date: 2013-07-05 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: httparty
@@ -158,6 +158,7 @@ files:
158
158
  - spec/spec_helper.rb
159
159
  - spec/support/double_logger.rb
160
160
  - spec/support/test_resource.rb
161
+ - spec/support/test_singular_resource.rb
161
162
  homepage: ''
162
163
  licenses: []
163
164
  post_install_message:
@@ -172,7 +173,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
172
173
  version: '0'
173
174
  segments:
174
175
  - 0
175
- hash: -1307169526788725825
176
+ hash: 1105111113104510387
176
177
  required_rubygems_version: !ruby/object:Gem::Requirement
177
178
  none: false
178
179
  requirements:
@@ -181,7 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
181
182
  version: '0'
182
183
  segments:
183
184
  - 0
184
- hash: -1307169526788725825
185
+ hash: 1105111113104510387
185
186
  requirements: []
186
187
  rubyforge_project:
187
188
  rubygems_version: 1.8.23
@@ -196,3 +197,4 @@ test_files:
196
197
  - spec/spec_helper.rb
197
198
  - spec/support/double_logger.rb
198
199
  - spec/support/test_resource.rb
200
+ - spec/support/test_singular_resource.rb