party_resource 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -0
- data/lib/party_resource/connector.rb +3 -3
- data/lib/party_resource/connector/base.rb +41 -14
- data/lib/party_resource/exceptions.rb +4 -4
- data/lib/party_resource/route.rb +5 -1
- metadata +259 -106
- data/spec/fixtures/other_class.rb +0 -19
- data/spec/fixtures/test_base_class.rb +0 -17
- data/spec/fixtures/test_class.rb +0 -44
- data/spec/integration/integration_spec.rb +0 -213
- data/spec/spec_helper.rb +0 -20
- data/spec/unit/connector/base_spec.rb +0 -83
- data/spec/unit/connector_spec.rb +0 -67
- data/spec/unit/exceptions_spec.rb +0 -34
- data/spec/unit/party_resource_spec.rb +0 -208
- data/spec/unit/request_spec.rb +0 -103
- data/spec/unit/route_spec.rb +0 -118
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe 'Exceptions' do
|
4
|
-
|
5
|
-
describe PartyResource::Exceptions::ConnectionError do
|
6
|
-
subject { PartyResource::Exceptions::ConnectionError }
|
7
|
-
|
8
|
-
describe 'build' do
|
9
|
-
let(:built) { subject.build(data) }
|
10
|
-
let_mock(:data)
|
11
|
-
|
12
|
-
it 'builds ResourceNotFound' do
|
13
|
-
data.stub(:code => 404)
|
14
|
-
built.should be_a(PartyResource::Exceptions::ResourceNotFound)
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'builds ResourceInvalid' do
|
18
|
-
data.stub(:code => 422)
|
19
|
-
built.should be_a(PartyResource::Exceptions::ResourceInvalid)
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'builds ClientError' do
|
23
|
-
data.stub(:code => 400)
|
24
|
-
built.should be_a(PartyResource::Exceptions::ClientError)
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'builds ServerError' do
|
28
|
-
data.stub(:code => 500)
|
29
|
-
built.should be_a(PartyResource::Exceptions::ServerError)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
end
|
@@ -1,208 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(__FILE__, '..', '..', 'spec_helper'))
|
2
|
-
|
3
|
-
describe "PartyResource" do
|
4
|
-
|
5
|
-
subject do
|
6
|
-
Class.new().send(:include, PartyResource)
|
7
|
-
end
|
8
|
-
let(:object) { subject.new }
|
9
|
-
|
10
|
-
describe '.connect' do
|
11
|
-
|
12
|
-
let(:route) { mock(:route) }
|
13
|
-
let_mock(:other_options)
|
14
|
-
|
15
|
-
before do
|
16
|
-
PartyResource::Route.stub(:new => route)
|
17
|
-
end
|
18
|
-
|
19
|
-
context 'for a class level route' do
|
20
|
-
let_mock(:klass)
|
21
|
-
|
22
|
-
it "creates a class method matching the name" do
|
23
|
-
subject.connect :new_resource_method
|
24
|
-
should respond_to(:new_resource_method)
|
25
|
-
end
|
26
|
-
|
27
|
-
it "creates a new route" do
|
28
|
-
options = {:values => other_options, :as => klass}
|
29
|
-
PartyResource::Route.should_receive(:new).with({:values => other_options, :connector => nil, :as => klass})
|
30
|
-
subject.connect :new_resource_method, options
|
31
|
-
end
|
32
|
-
|
33
|
-
context 'the created method' do
|
34
|
-
|
35
|
-
before { subject.connect :new_resource_method }
|
36
|
-
|
37
|
-
it 'calls the route with the class and the arguments' do
|
38
|
-
args = [mock(:arg), mock(:arg)]
|
39
|
-
route.should_receive(:call).with(subject, *args)
|
40
|
-
|
41
|
-
subject.new_resource_method args[0], args[1]
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|
45
|
-
|
46
|
-
context 'with a connector set' do
|
47
|
-
before do
|
48
|
-
subject.party_connector :foo
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'passes the connector name to the route' do
|
52
|
-
PartyResource::Route.should_receive(:new).with(hash_including(:connector => :foo))
|
53
|
-
subject.connect :new_resource_method, {}
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
context 'for an instance level route' do
|
59
|
-
|
60
|
-
it "creates an instance method matching the name" do
|
61
|
-
subject.connect :new_resource_method, :on => :instance
|
62
|
-
subject.new.should respond_to(:new_resource_method)
|
63
|
-
end
|
64
|
-
|
65
|
-
it "creates a new route" do
|
66
|
-
options = {:on => :instance, :others => other_options}
|
67
|
-
PartyResource::Route.should_receive(:new).with({:others => other_options, :connector => nil, :as => :self})
|
68
|
-
subject.connect :new_resource_method, options
|
69
|
-
end
|
70
|
-
|
71
|
-
context 'the created method' do
|
72
|
-
|
73
|
-
before { subject.connect :new_resource_method, :on => :instance }
|
74
|
-
|
75
|
-
it 'calls the route with the class and the arguments' do
|
76
|
-
args = [mock(:arg), mock(:arg)]
|
77
|
-
object = subject.new
|
78
|
-
route.should_receive(:call).with(object, *args)
|
79
|
-
|
80
|
-
object.new_resource_method args[0], args[1]
|
81
|
-
end
|
82
|
-
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
describe '.parameter_values' do
|
88
|
-
let_mock(:v1)
|
89
|
-
let_mock(:v2)
|
90
|
-
let_mock(:v3)
|
91
|
-
it 'returns internal values for requested variables' do
|
92
|
-
subject.stub(:v1 => v1, :v2 => v2, :v3 => v3)
|
93
|
-
subject.parameter_values([:v1, :v3]).should == {:v1 => v1, :v3 => v3}
|
94
|
-
end
|
95
|
-
|
96
|
-
it 'raises a MissingParameter error' do
|
97
|
-
subject.stub(:v1 => v1)
|
98
|
-
lambda { subject.parameter_values([:v1, :vx]) }.should raise_error(PartyResource::Exceptions::MissingParameter)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
describe 'properties' do
|
103
|
-
let_mock(:v1)
|
104
|
-
let_mock(:v2)
|
105
|
-
it 'populates property values from hash' do
|
106
|
-
subject.property :name, :name2
|
107
|
-
object.send(:populate_properties, :name => v1, :name2 => v2)
|
108
|
-
object.name.should == v1
|
109
|
-
object.name2.should == v2
|
110
|
-
end
|
111
|
-
|
112
|
-
it 'renames properties from passed data' do
|
113
|
-
subject.property :name, :from => :input_name
|
114
|
-
object.send(:populate_properties, :input_name => v1, :name => v2)
|
115
|
-
object.name.should == v1
|
116
|
-
end
|
117
|
-
|
118
|
-
it 'reaches inside nested values' do
|
119
|
-
subject.property :name, :from => [:parent, :input_name]
|
120
|
-
object.send(:populate_properties, :parent => { :input_name => v1 }, :input_name => v2)
|
121
|
-
object.name.should == v1
|
122
|
-
end
|
123
|
-
|
124
|
-
it 'falls back to populating based on the property name if from is not found' do
|
125
|
-
subject.property :name, :from => :input_name
|
126
|
-
object.send(:populate_properties, :name => v2)
|
127
|
-
object.name.should == v2
|
128
|
-
end
|
129
|
-
|
130
|
-
it 'does not try to build nil values' do
|
131
|
-
klass = Class.new
|
132
|
-
subject.property :name, :as => klass
|
133
|
-
klass.should_not_receive(:new)
|
134
|
-
object.send(:populate_properties, :name => nil)
|
135
|
-
object.name.should == nil
|
136
|
-
end
|
137
|
-
|
138
|
-
it "doesn't set properties to nil if they aren't in the data hash" do
|
139
|
-
subject.property :name, :name2
|
140
|
-
object.send(:populate_properties, :name => v1)
|
141
|
-
object.name.should == v1
|
142
|
-
object.name2.should == nil
|
143
|
-
object.send(:populate_properties,:name2 => v2)
|
144
|
-
object.name.should == v1
|
145
|
-
object.name2.should == v2
|
146
|
-
end
|
147
|
-
|
148
|
-
it 'returns a hash representation of properties' do
|
149
|
-
subject.property :name, :name2
|
150
|
-
object.send(:populate_properties, :name => v1, :name2 => v2)
|
151
|
-
object.to_properties_hash.should == {:name => v1, :name2 => v2}
|
152
|
-
end
|
153
|
-
|
154
|
-
it 'does not include nil values in the properties hash' do
|
155
|
-
subject.property :name, :name2
|
156
|
-
object.send(:populate_properties, :name => v1)
|
157
|
-
object.to_properties_hash.should == {:name => v1}
|
158
|
-
end
|
159
|
-
|
160
|
-
it 'translates propery names to input names' do
|
161
|
-
subject.property :name, :from => :input_name
|
162
|
-
object.send(:populate_properties, :name => v1)
|
163
|
-
object.to_properties_hash.should == {:input_name => v1}
|
164
|
-
end
|
165
|
-
|
166
|
-
it 'translates propery names to nested input names' do
|
167
|
-
subject.property :name, :from => [:block, :input_name]
|
168
|
-
object.send(:populate_properties, :name => v1)
|
169
|
-
object.to_properties_hash.should == {:block => {:input_name => v1}}
|
170
|
-
end
|
171
|
-
|
172
|
-
it 'translates property names to their required output names' do
|
173
|
-
subject.property :name, :to => :output_name
|
174
|
-
subject.property :name2, :to => [:block, :output_name]
|
175
|
-
object.send(:populate_properties, :name => v1, :name2 => v2)
|
176
|
-
object.to_properties_hash.should == {:output_name => v1, :block => {:output_name => v2}}
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
describe '#properties_equal?' do
|
181
|
-
let_mock(:v1)
|
182
|
-
let(:object2) { subject.new }
|
183
|
-
|
184
|
-
before do
|
185
|
-
subject.property :name, :name2
|
186
|
-
object.send(:populate_properties, :name => v1, :name2 => nil)
|
187
|
-
end
|
188
|
-
|
189
|
-
it 'returns true if all properties are equal' do
|
190
|
-
subject.property :name, :name2
|
191
|
-
object.send(:populate_properties, :name => v1, :name2 => nil)
|
192
|
-
object2.send(:populate_properties, :name => v1)
|
193
|
-
object.should be_properties_equal(object2)
|
194
|
-
end
|
195
|
-
|
196
|
-
it 'returns false if all properties are not equal' do
|
197
|
-
object2.send(:populate_properties, :name => v1, :name2 => v1)
|
198
|
-
object.should_not be_properties_equal(object2)
|
199
|
-
end
|
200
|
-
|
201
|
-
it 'returns false if the other object does not respond to all required properties' do
|
202
|
-
object.should_not be_properties_equal(Object.new)
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
|
207
|
-
end
|
208
|
-
|
data/spec/unit/request_spec.rb
DELETED
@@ -1,103 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(__FILE__, '..', '..', 'spec_helper'))
|
2
|
-
|
3
|
-
describe PartyResource::Request do
|
4
|
-
|
5
|
-
subject { PartyResource::Request.new(verb, path, context, args, params) }
|
6
|
-
let_mock(:context, :parameter_values => {})
|
7
|
-
let_mock(:verb)
|
8
|
-
let_mock(:value)
|
9
|
-
let(:params) { {} }
|
10
|
-
let(:path) { '/path' }
|
11
|
-
|
12
|
-
context 'with static path' do
|
13
|
-
let(:path) { 'mypath/file.json' }
|
14
|
-
let(:args) { {} }
|
15
|
-
|
16
|
-
its(:verb) { should == verb }
|
17
|
-
its(:path) { should == path }
|
18
|
-
its(:data) { should == {} }
|
19
|
-
|
20
|
-
it "should merge http_data with passed options" do
|
21
|
-
subject.http_data(:foo => :bar).should == { :foo => :bar }
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
context 'with a parameter' do
|
26
|
-
let(:path) { 'mypath/file.json' }
|
27
|
-
let(:args) { {:param => value} }
|
28
|
-
|
29
|
-
its(:verb) { should == verb }
|
30
|
-
its(:path) { should == path }
|
31
|
-
its(:data) { should == args }
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'with a parameter referenced in the path' do
|
35
|
-
let(:path) { 'mypath/:param.json' }
|
36
|
-
let(:args) { {:param => 'THE_VALUE', :second => value} }
|
37
|
-
|
38
|
-
its(:verb) { should == verb }
|
39
|
-
its(:path) { should == 'mypath/THE_VALUE.json' }
|
40
|
-
its(:data) { should == {:second => value } }
|
41
|
-
end
|
42
|
-
|
43
|
-
context 'with an object variable referenced in the path' do
|
44
|
-
let_mock(:context, :parameter_values => {:var => 'THE_VALUE'})
|
45
|
-
let(:path) { 'mypath/:param/:var.json' }
|
46
|
-
let(:args) { {:param => 'PARAM_VALUE', :second => value} }
|
47
|
-
|
48
|
-
its(:verb) { should == verb }
|
49
|
-
its(:path) { should == 'mypath/PARAM_VALUE/THE_VALUE.json' }
|
50
|
-
its(:data) { should == { :second => value } }
|
51
|
-
it 'asks the context object for required data values' do
|
52
|
-
context.should_receive(:parameter_values).with([:var])
|
53
|
-
subject.path
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
context 'with extra parameters' do
|
58
|
-
let(:path) { '/path/:id' }
|
59
|
-
let_mock(:p1)
|
60
|
-
let_mock(:p2)
|
61
|
-
let_mock(:a1)
|
62
|
-
let_mock(:a2)
|
63
|
-
let(:params) { {:id => mock(:id2), :param => p1, :param2 => p2} }
|
64
|
-
let(:args) { {:id => 'THE_ID', :param => a1, :arg2 => a2} }
|
65
|
-
it 'merges them into the request data' do
|
66
|
-
subject.path.should == '/path/THE_ID'
|
67
|
-
subject.data.should == {:param => a1, :param2 => p2, :arg2 => a2}
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
context 'for a GET request' do
|
72
|
-
let(:verb) { :get }
|
73
|
-
let_mock(:args)
|
74
|
-
its(:http_data) { { :query => args } }
|
75
|
-
|
76
|
-
context 'with no data' do
|
77
|
-
let(:args) { {} }
|
78
|
-
its(:http_data) { should == {} }
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
[:post, :put, :delete].each do |http_verb|
|
83
|
-
context "for a #{http_verb} request" do
|
84
|
-
let(:verb) { http_verb }
|
85
|
-
let(:args) { {:param => value} }
|
86
|
-
its(:http_data) { should == { :body => args } }
|
87
|
-
end
|
88
|
-
|
89
|
-
context 'with no data' do
|
90
|
-
let(:args) { {} }
|
91
|
-
its(:http_data) { should == {} }
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
context 'encoding the path' do
|
96
|
-
let(:args) { {} }
|
97
|
-
let(:path) { '/path needing encoding' }
|
98
|
-
it 'encodes the path' do
|
99
|
-
subject.path.should == '/path%20needing%20encoding'
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
end
|
data/spec/unit/route_spec.rb
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(__FILE__, '..', '..', 'spec_helper'))
|
2
|
-
|
3
|
-
describe PartyResource::Route do
|
4
|
-
|
5
|
-
subject { PartyResource::Route.new options }
|
6
|
-
|
7
|
-
let_mock(:path)
|
8
|
-
let_mock(:object)
|
9
|
-
let_mock(:raw_result)
|
10
|
-
let(:connector) { mock(:connector, :fetch => raw_result) }
|
11
|
-
let(:klass) { Class.new {def initialize(data); end} }
|
12
|
-
let(:options) { { :get => path, :as => klass } }
|
13
|
-
|
14
|
-
before do
|
15
|
-
PartyResource::Connector.stub(:lookup => connector)
|
16
|
-
end
|
17
|
-
|
18
|
-
describe ".call" do
|
19
|
-
|
20
|
-
[:get, :put, :post, :delete].each do |verb|
|
21
|
-
|
22
|
-
context "for a #{verb} request" do
|
23
|
-
let(:options) { { verb => path, :as => klass } }
|
24
|
-
|
25
|
-
it "performs a #{verb} request to the path with the connector" do
|
26
|
-
request = mock(:request)
|
27
|
-
PartyResource::Request.should_receive(:new).with(verb, path, object, anything, anything).and_return(request)
|
28
|
-
connector.should_receive(:fetch).with(request)
|
29
|
-
subject.call(object)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'with more than one verb passed' do
|
35
|
-
let(:options) { { :post => path, :get => path } }
|
36
|
-
|
37
|
-
it "raises an argument error" do
|
38
|
-
lambda{ subject.call(object) }.should raise_error(ArgumentError)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context "with no variables" do
|
43
|
-
let(:options) { { :get => path, :as => klass } }
|
44
|
-
|
45
|
-
it "builds the request path" do
|
46
|
-
PartyResource::Request.should_receive(:new).with(:get, path, object, {}, anything)
|
47
|
-
subject.call(object)
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
context "with some variables" do
|
53
|
-
let(:options) { { :get => path, :with => [:a, :b, :c], :as => klass } }
|
54
|
-
|
55
|
-
it "builds the request path" do
|
56
|
-
PartyResource::Request.should_receive(:new).with(:get, path, object, {:a => 1, :b => 2, :c => 3}, anything)
|
57
|
-
subject.call(object, 1, 2, 3)
|
58
|
-
end
|
59
|
-
|
60
|
-
it "raises an ArgumentError for the wrong number of arguments" do
|
61
|
-
lambda { subject.call(object, 1, 2, 3, 4) }.should raise_error(ArgumentError)
|
62
|
-
lambda { subject.call(object) }.should raise_error(ArgumentError)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
context 'when returning as class' do
|
67
|
-
let_mock(:result_object)
|
68
|
-
|
69
|
-
it 'builds an object from the data returned' do
|
70
|
-
klass.should_receive(:new).with(raw_result).and_return(result_object)
|
71
|
-
subject.call(object).should == result_object
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
context 'when returning as :raw' do
|
76
|
-
let(:options) { { :get => path, :as => :raw } }
|
77
|
-
it 'builds an object from the data returned' do
|
78
|
-
klass.should_not_receive(:new)
|
79
|
-
subject.call(object).should == raw_result
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
context 'when returning as class with builder method' do
|
84
|
-
let_mock(:result_object)
|
85
|
-
let(:options) { { :get => path, :as => [klass, :builder] } }
|
86
|
-
|
87
|
-
it 'builds an object from the data returned' do
|
88
|
-
klass.should_receive(:builder).with(raw_result).and_return(result_object)
|
89
|
-
subject.call(object).should == result_object
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
context 'when returning as proc' do
|
94
|
-
let_mock(:result_object)
|
95
|
-
let(:options) { { :get => path, :as => lambda {|data| data.morph } } }
|
96
|
-
|
97
|
-
it 'builds an object from the data returned' do
|
98
|
-
raw_result.should_receive(:morph).and_return(result_object)
|
99
|
-
subject.call(object).should == result_object
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
context 'when returning an array' do
|
104
|
-
let(:raw_result) { [1, 2] }
|
105
|
-
let(:options) { { :get => path, :as => lambda {|data| "X#{data}" } } }
|
106
|
-
it 'builds each value individually' do
|
107
|
-
subject.call(object).should == %w{ X1 X2 }
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
context 'with an extra params hash' do
|
112
|
-
it '' do
|
113
|
-
lambda { subject.call(object, {:params => :data}) }.should_not raise_error
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
end
|
118
|
-
end
|