party_resource 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,103 @@
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
@@ -0,0 +1,118 @@
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.stub(:Connector => 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
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: party_resource
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Tristan Harris
13
+ - Steve Tooke
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-05-26 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: httparty
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 5
31
+ - 2
32
+ version: 0.5.2
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: activesupport
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 2
44
+ - 3
45
+ - 5
46
+ version: 2.3.5
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 1
58
+ - 2
59
+ - 9
60
+ version: 1.2.9
61
+ type: :development
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: yard
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ type: :development
74
+ version_requirements: *id004
75
+ - !ruby/object:Gem::Dependency
76
+ name: webmock
77
+ prerelease: false
78
+ requirement: &id005 !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ type: :development
86
+ version_requirements: *id005
87
+ description: party_resource is a framework for building ruby objects which interact with a REST api. Built on top of HTTParty.
88
+ email: dev+party_resource@edendevelopment.co.uk
89
+ executables: []
90
+
91
+ extensions: []
92
+
93
+ extra_rdoc_files:
94
+ - LICENSE
95
+ - README.rdoc
96
+ files:
97
+ - lib/party_resource.rb
98
+ - lib/party_resource/buildable.rb
99
+ - lib/party_resource/connector.rb
100
+ - lib/party_resource/connector/base.rb
101
+ - lib/party_resource/exceptions.rb
102
+ - lib/party_resource/method_define.rb
103
+ - lib/party_resource/party_resource.rb
104
+ - lib/party_resource/property.rb
105
+ - lib/party_resource/request.rb
106
+ - lib/party_resource/route.rb
107
+ - LICENSE
108
+ - README.rdoc
109
+ has_rdoc: true
110
+ homepage: http://github.com/edendevelopment/party_resource.git
111
+ licenses: []
112
+
113
+ post_install_message:
114
+ rdoc_options:
115
+ - --charset=UTF-8
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ segments:
123
+ - 0
124
+ version: "0"
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ segments:
130
+ - 0
131
+ version: "0"
132
+ requirements: []
133
+
134
+ rubyforge_project:
135
+ rubygems_version: 1.3.6
136
+ signing_key:
137
+ specification_version: 3
138
+ summary: Simple REST interface for ruby objects.
139
+ test_files:
140
+ - spec/fixtures/other_class.rb
141
+ - spec/fixtures/test_base_class.rb
142
+ - spec/fixtures/test_class.rb
143
+ - spec/integration/integration_spec.rb
144
+ - spec/spec_helper.rb
145
+ - spec/unit/connector/base_spec.rb
146
+ - spec/unit/connector_spec.rb
147
+ - spec/unit/exceptions_spec.rb
148
+ - spec/unit/party_resource_spec.rb
149
+ - spec/unit/request_spec.rb
150
+ - spec/unit/route_spec.rb