angus-remote 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/lib/angus-remote.rb +4 -0
  2. data/lib/angus/remote/builder.rb +204 -0
  3. data/lib/angus/remote/client.rb +79 -0
  4. data/lib/angus/remote/exceptions.rb +50 -0
  5. data/lib/angus/remote/http/multipart.rb +54 -0
  6. data/lib/angus/remote/http/multipart_methods/multipart_base.rb +36 -0
  7. data/lib/angus/remote/http/multipart_methods/multipart_post.rb +11 -0
  8. data/lib/angus/remote/http/multipart_methods/multipart_put.rb +11 -0
  9. data/lib/angus/remote/http/query_params.rb +53 -0
  10. data/lib/angus/remote/message.rb +14 -0
  11. data/lib/angus/remote/proxy_client.rb +58 -0
  12. data/lib/angus/remote/proxy_client_utils.rb +70 -0
  13. data/lib/angus/remote/remote_response.rb +44 -0
  14. data/lib/angus/remote/representation.rb +18 -0
  15. data/lib/angus/remote/response/builder.rb +308 -0
  16. data/lib/angus/remote/response/hash.rb +47 -0
  17. data/lib/angus/remote/response/serializer.rb +43 -0
  18. data/lib/angus/remote/service_directory.rb +217 -0
  19. data/lib/angus/remote/utils.rb +119 -0
  20. data/lib/angus/remote/version.rb +4 -2
  21. data/lib/angus/unmarshalling.rb +33 -0
  22. data/spec/angus/remote/builder_spec.rb +105 -0
  23. data/spec/angus/remote/client_spec.rb +75 -0
  24. data/spec/angus/remote/http/multipart_methods/multipart_base_spec.rb +36 -0
  25. data/spec/angus/remote/http/multipart_spec.rb +120 -0
  26. data/spec/angus/remote/http/query_params_spec.rb +28 -0
  27. data/spec/angus/remote/proxy_client_utils_spec.rb +102 -0
  28. data/spec/angus/remote/response/builder_spec.rb +69 -0
  29. data/spec/angus/remote/service_directory_spec.rb +76 -0
  30. data/spec/angus/remote/utils_spec.rb +204 -0
  31. metadata +192 -32
  32. data/.gitignore +0 -17
  33. data/Gemfile +0 -4
  34. data/LICENSE.txt +0 -22
  35. data/README.md +0 -29
  36. data/Rakefile +0 -1
  37. data/angus-remote.gemspec +0 -23
  38. data/lib/angus/remote.rb +0 -7
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ require 'angus/remote/response/builder'
4
+
5
+ require 'angus/remote/remote_response'
6
+
7
+ describe Angus::Remote::Response::Builder do
8
+
9
+ subject(:builder) { Angus::Remote::Response::Builder }
10
+
11
+ let(:raw_response) { { 'user' => {} } }
12
+ let(:response_class) { builder.build_response_class('Get user') }
13
+ let(:response) { Angus::Remote::RemoteResponse.new() }
14
+ let(:email_field) { double(:email_field, :name => :email, :required => true, :type => 'string',
15
+ :elements_type => nil) }
16
+ let(:representations_hash) { { 'user' => double(:user_rep, :fields => [email_field]) } }
17
+ let(:glossary_terms_hash) { {} }
18
+ let(:element) { double(:element, :name => 'user', :required => true, :type => 'user') }
19
+
20
+ describe '.build_response_method' do
21
+
22
+ it 'adds the method to the response class' do
23
+ builder.build_response_method(raw_response, response_class, response, representations_hash,
24
+ glossary_terms_hash, element)
25
+
26
+ response_class.new.should respond_to(:user)
27
+ end
28
+
29
+ end
30
+
31
+ describe '.build_from_representation' do
32
+
33
+ context 'hash_value is nil' do
34
+ it 'should return nil' do
35
+ Angus::Remote::Response::Builder.build_from_representation(
36
+ nil,
37
+ double(:type),
38
+ double(:representations),
39
+ double(:glossary_terms_hash)
40
+ ).should be_nil
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '.build_response_class' do
46
+ let(:operation_name) { 'foo' }
47
+
48
+ it 'should return a client class' do
49
+ response_class = Angus::Remote::Response::Builder.build_response_class(operation_name)
50
+
51
+ response = response_class.new
52
+
53
+ response.should be_kind_of(Angus::Remote::RemoteResponse)
54
+ end
55
+
56
+ it 'the response_class#name should include the operation\'s name' do
57
+ response_class = Angus::Remote::Response::Builder.build_response_class(operation_name)
58
+
59
+ response_class.name.should include(operation_name)
60
+ end
61
+
62
+ it 'the response_class#to_s should include the operation\'s name' do
63
+ response_class = Angus::Remote::Response::Builder.build_response_class(operation_name)
64
+
65
+ response_class.to_s.should include(operation_name)
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ require 'angus/remote/service_directory'
4
+
5
+ describe Angus::Remote::ServiceDirectory do
6
+
7
+ subject(:service_directory) { Angus::Remote::ServiceDirectory }
8
+
9
+ let(:code_name) { 'vpos' }
10
+ let(:version) { '0.1' }
11
+ let(:doc_url) { 'some_url/doc' }
12
+ let(:api_url) { 'some_url/api' }
13
+
14
+ before do
15
+ Angus::Remote::ServiceDirectory.stub(:service_configuration => { "v#{version}" => {
16
+ 'doc_url' => doc_url, 'api_url' => api_url }
17
+ })
18
+ end
19
+
20
+ describe '.get_service_definition' do
21
+
22
+ let(:service_definition){ double(:service_definition)}
23
+
24
+ context 'when a file url' do
25
+ let(:doc_url) { 'file://path/to/doc' }
26
+
27
+ before do
28
+ Angus::SDoc::DefinitionsReader.stub(:service_definition => service_definition)
29
+ end
30
+
31
+ it 'builds the service definition from the path' do
32
+ Angus::SDoc::DefinitionsReader.should_receive(
33
+ :service_definition
34
+ ).with('path/to/doc')
35
+
36
+ service_directory.get_service_definition(code_name, version)
37
+ end
38
+
39
+ it 'returns the service definition' do
40
+ service_directory.get_service_definition(code_name, version).should eq(service_definition)
41
+ end
42
+ end
43
+
44
+ context 'when a remote url' do
45
+ let(:doc_url) { 'some_url/doc' }
46
+ let(:definition_hash) { {} }
47
+
48
+ before do
49
+ service_directory.stub(:fetch_remote_service_definition => definition_hash)
50
+ Angus::SDoc::DefinitionsReader.stub(:build_service_definition => service_definition)
51
+ end
52
+
53
+ it 'gets the definition hash from the remote service' do
54
+ service_directory.should_receive(:fetch_remote_service_definition).with(
55
+ doc_url
56
+ ).and_return(definition_hash)
57
+
58
+ service_directory.get_service_definition(code_name, version)
59
+ end
60
+
61
+ it 'builds the service definition from the definition hash' do
62
+ Angus::SDoc::DefinitionsReader.should_receive(
63
+ :build_service_definition
64
+ ).with(definition_hash)
65
+
66
+ service_directory.get_service_definition(code_name, version)
67
+ end
68
+
69
+ it 'returns the service definition' do
70
+ service_directory.get_service_definition(code_name, version).should eq(service_definition)
71
+ end
72
+ end
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,204 @@
1
+ require 'json'
2
+
3
+ require 'fakefs/spec_helpers'
4
+
5
+ require 'angus/remote/utils'
6
+
7
+ describe Angus::Remote::Utils do
8
+
9
+ include FakeFS::SpecHelpers
10
+
11
+ subject(:utils) { Angus::Remote::Utils }
12
+
13
+ describe '.build_request' do
14
+
15
+ let(:request) { utils.build_request(:get, '/listing', :q => 'spec') }
16
+
17
+ it 'returns a Net::HTTPRequest object' do
18
+ request.should be_kind_of(Net::HTTPRequest)
19
+ end
20
+
21
+ context 'when not encoding as json' do
22
+ subject { utils.build_request(:get, '/listing', {:q => 'spec'}, false) }
23
+
24
+ describe 'the built request' do
25
+
26
+ its(:method) { should eq('GET') }
27
+ its(:path) { should eq('/listing?q=spec') }
28
+
29
+ end
30
+ end
31
+
32
+ context 'when encoding as json' do
33
+ subject { utils.build_request(:get, '/listing', {:q => 'spec'}, true) }
34
+
35
+ describe 'the built request' do
36
+
37
+ its(:method) { should eq('GET') }
38
+ its(:path) { should eq('/listing') }
39
+ its(:body) { should eq(JSON({:q => 'spec'})) }
40
+
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+
47
+ describe '.build_base_request' do
48
+
49
+ let(:path) { '/' }
50
+
51
+ context 'when invalid http method' do
52
+ it 'raises MethodArgumentError' do
53
+ expect {
54
+ utils.build_base_request(:invalid, path)
55
+ }.to raise_error(Angus::Remote::MethodArgumentError)
56
+ end
57
+ end
58
+
59
+ shared_examples 'a client builder' do |method, multipart, kind_of|
60
+ context "when #{method}, multipart = #{multipart}" do
61
+ it "returns a kind_of #{kind_of}" do
62
+ res = utils.build_base_request(method, path, multipart)
63
+
64
+ res.should be_a(kind_of)
65
+ end
66
+ end
67
+ end
68
+
69
+ it_behaves_like 'a client builder', :get, false, Net::HTTP::Get
70
+ it_behaves_like 'a client builder', :post, false, Net::HTTP::Post
71
+ it_behaves_like 'a client builder', :post, true, Http::MultipartMethods::Post
72
+ it_behaves_like 'a client builder', :put, false, Net::HTTP::Put
73
+ it_behaves_like 'a client builder', :put, true, Http::MultipartMethods::Put
74
+ it_behaves_like 'a client builder', :delete, false, Net::HTTP::Delete
75
+ end
76
+
77
+ describe '.severe_error_response?' do
78
+ shared_examples 'a status checker' do |code|
79
+ it "is true when code = #{code}" do
80
+ response = double(:response, :code => code)
81
+ utils.severe_error_response?(response).should be
82
+ end
83
+ end
84
+
85
+ it_behaves_like 'a status checker', 500
86
+ it_behaves_like 'a status checker', 501
87
+ it_behaves_like 'a status checker', 503
88
+ end
89
+
90
+ describe '.build_path' do
91
+ let(:path) { '/users/:user_id/profile/:profile_id' }
92
+
93
+ it 'buils a path using the given params' do
94
+ path_params = [4201, 2]
95
+
96
+ res = utils.build_path(path, path_params)
97
+
98
+ res.should eq('/users/4201/profile/2')
99
+ end
100
+
101
+ it 'raises a PathArgumentError when received more args than needed' do
102
+ path_params = [:more, :args, :than, :needed]
103
+
104
+ expect {
105
+ utils.build_path(path, path_params)
106
+ }.to raise_error(Angus::Remote::PathArgumentError)
107
+ end
108
+
109
+ it 'raises a PathArgumentError when received less args than needed' do
110
+ path_params = [:less]
111
+
112
+ expect {
113
+ utils.build_path(path, path_params)
114
+ }.to raise_error(Angus::Remote::PathArgumentError)
115
+ end
116
+ end
117
+
118
+ describe '.build_normal_request' do
119
+
120
+ subject { utils.build_normal_request(:get, '/listing', {:q => 'spec'}) }
121
+
122
+ shared_examples 'a method without body' do |method|
123
+ describe 'the built request' do
124
+ subject { utils.build_normal_request(method, '/listing', {:q => 'spec'}) }
125
+
126
+ its(:method) { should eq(method.upcase) }
127
+ its(:path) { should eq('/listing?q=spec') }
128
+ its(:body) { should be_nil }
129
+
130
+ end
131
+ end
132
+
133
+ shared_examples 'a method with body' do |method|
134
+ describe 'the built request' do
135
+ subject { utils.build_normal_request(method, '/listing', {:q => 'spec'}) }
136
+
137
+ its(:method) { should eq(method.upcase) }
138
+ its(:path) { should eq('/listing') }
139
+ its(:body) { should eq('q=spec') }
140
+
141
+ end
142
+ end
143
+
144
+ it_behaves_like 'a method without body', 'get'
145
+ it_behaves_like 'a method without body', 'delete'
146
+ it_behaves_like 'a method with body', 'post'
147
+ it_behaves_like 'a method with body', 'put'
148
+
149
+ context 'a multipart request' do
150
+ let(:file_name) { 'some_file.txt' }
151
+
152
+ let(:file) { File.new(file_name) }
153
+ let(:tempfile) { Tempfile.new('tempfile', temp_dir) }
154
+ let(:temp_dir) { '/tmp' }
155
+ let(:some_upload_io) { UploadIO.new(file, 'application/octet-stream') }
156
+
157
+ let(:request) { utils.build_normal_request('post', '/files', {:file => some_upload_io}) }
158
+
159
+ before do
160
+ Dir.mkdir('/tmp')
161
+
162
+ File.new(file_name, 'w')
163
+ end
164
+
165
+ describe 'the built request' do
166
+ subject { request }
167
+
168
+ it 'is of class Http::MultipartMethods::MultipartBase' do
169
+ should be_kind_of(Http::MultipartMethods::MultipartBase)
170
+ end
171
+
172
+ its(:method) { should eq('POST') }
173
+ its(:path) { should eq('/files') }
174
+ its(:body) { should be_nil }
175
+
176
+ end
177
+ end
178
+ end
179
+
180
+ describe '.build_json_request' do
181
+
182
+ let(:method) { :post }
183
+ let(:path) { '/' }
184
+ let(:params) { [0, 1, 2] }
185
+
186
+ let(:request) do
187
+ utils.build_json_request(method, path, params)
188
+ end
189
+
190
+ describe 'the returned request' do
191
+
192
+ it 'its Content-Type header = application/json' do
193
+ request['Content-Type'].should eq('application/json')
194
+ end
195
+
196
+ it 'its body = JSON encoded params' do
197
+ request.body.should eq(JSON(params))
198
+ end
199
+
200
+ end
201
+
202
+ end
203
+
204
+ end
metadata CHANGED
@@ -1,86 +1,246 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: angus-remote
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.0.1
4
+ version: 0.0.2
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Adrian Gomez
9
- autorequire:
9
+ - Gianfranco Zas
10
+ autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2013-10-10 00:00:00.000000000 Z
13
+ date: 2013-10-31 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
- name: bundler
16
- version_requirements: !ruby/object:Gem::Requirement
16
+ name: angus-sdoc
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
17
19
  requirements:
18
20
  - - ~>
19
21
  - !ruby/object:Gem::Version
20
- version: '1.3'
22
+ version: '0.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
21
26
  none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '0.0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: persistent_http
22
33
  requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '1.0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
23
43
  requirements:
24
44
  - - ~>
25
45
  - !ruby/object:Gem::Version
26
- version: '1.3'
46
+ version: '1.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: multipart-post
49
+ requirement: !ruby/object:Gem::Requirement
27
50
  none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.2'
55
+ type: :runtime
28
56
  prerelease: false
29
- type: :development
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '1.2'
30
63
  - !ruby/object:Gem::Dependency
31
64
  name: rake
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
32
73
  version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
33
75
  requirements:
34
- - - '>='
76
+ - - ! '>='
35
77
  - !ruby/object:Gem::Version
36
78
  version: '0'
79
+ - !ruby/object:Gem::Dependency
80
+ name: fakefs
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ version: '0.4'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
37
90
  none: false
91
+ requirements:
92
+ - - ~>
93
+ - !ruby/object:Gem::Version
94
+ version: '0.4'
95
+ - !ruby/object:Gem::Dependency
96
+ name: rspec
38
97
  requirement: !ruby/object:Gem::Requirement
98
+ none: false
39
99
  requirements:
40
- - - '>='
100
+ - - ~>
41
101
  - !ruby/object:Gem::Version
42
- version: '0'
102
+ version: '2.14'
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
43
106
  none: false
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '2.14'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ~>
117
+ - !ruby/object:Gem::Version
118
+ version: '0.7'
119
+ type: :development
44
120
  prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ~>
125
+ - !ruby/object:Gem::Version
126
+ version: '0.7'
127
+ - !ruby/object:Gem::Dependency
128
+ name: simplecov-rcov
129
+ requirement: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ~>
133
+ - !ruby/object:Gem::Version
134
+ version: '0.2'
45
135
  type: :development
46
- description: angus-remote
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ~>
141
+ - !ruby/object:Gem::Version
142
+ version: '0.2'
143
+ - !ruby/object:Gem::Dependency
144
+ name: simplecov-rcov-text
145
+ requirement: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ~>
149
+ - !ruby/object:Gem::Version
150
+ version: '0.0'
151
+ type: :development
152
+ prerelease: false
153
+ version_requirements: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ~>
157
+ - !ruby/object:Gem::Version
158
+ version: '0.0'
159
+ - !ruby/object:Gem::Dependency
160
+ name: ci_reporter
161
+ requirement: !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ~>
165
+ - !ruby/object:Gem::Version
166
+ version: '1.9'
167
+ type: :development
168
+ prerelease: false
169
+ version_requirements: !ruby/object:Gem::Requirement
170
+ none: false
171
+ requirements:
172
+ - - ~>
173
+ - !ruby/object:Gem::Version
174
+ version: '1.9'
175
+ description: Provides support code for making requests and building responses
47
176
  email:
48
- - adrian.gomez@moove-it.com
177
+ - angus@moove-it.com
49
178
  executables: []
50
179
  extensions: []
51
180
  extra_rdoc_files: []
52
181
  files:
53
- - .gitignore
54
- - Gemfile
55
- - LICENSE.txt
56
- - README.md
57
- - Rakefile
58
- - angus-remote.gemspec
59
- - lib/angus/remote.rb
182
+ - lib/angus-remote.rb
183
+ - lib/angus/unmarshalling.rb
184
+ - lib/angus/remote/representation.rb
60
185
  - lib/angus/remote/version.rb
61
- homepage: ''
186
+ - lib/angus/remote/http/query_params.rb
187
+ - lib/angus/remote/http/multipart_methods/multipart_put.rb
188
+ - lib/angus/remote/http/multipart_methods/multipart_base.rb
189
+ - lib/angus/remote/http/multipart_methods/multipart_post.rb
190
+ - lib/angus/remote/http/multipart.rb
191
+ - lib/angus/remote/message.rb
192
+ - lib/angus/remote/builder.rb
193
+ - lib/angus/remote/proxy_client_utils.rb
194
+ - lib/angus/remote/response/builder.rb
195
+ - lib/angus/remote/response/serializer.rb
196
+ - lib/angus/remote/response/hash.rb
197
+ - lib/angus/remote/service_directory.rb
198
+ - lib/angus/remote/remote_response.rb
199
+ - lib/angus/remote/exceptions.rb
200
+ - lib/angus/remote/client.rb
201
+ - lib/angus/remote/proxy_client.rb
202
+ - lib/angus/remote/utils.rb
203
+ - spec/angus/remote/service_directory_spec.rb
204
+ - spec/angus/remote/proxy_client_utils_spec.rb
205
+ - spec/angus/remote/http/query_params_spec.rb
206
+ - spec/angus/remote/http/multipart_spec.rb
207
+ - spec/angus/remote/http/multipart_methods/multipart_base_spec.rb
208
+ - spec/angus/remote/utils_spec.rb
209
+ - spec/angus/remote/response/builder_spec.rb
210
+ - spec/angus/remote/client_spec.rb
211
+ - spec/angus/remote/builder_spec.rb
212
+ homepage: http://mooveit.github.io/angus
62
213
  licenses:
63
214
  - MIT
64
- post_install_message:
215
+ post_install_message:
65
216
  rdoc_options: []
66
217
  require_paths:
67
218
  - lib
68
219
  required_ruby_version: !ruby/object:Gem::Requirement
220
+ none: false
69
221
  requirements:
70
- - - '>='
222
+ - - ! '>='
71
223
  - !ruby/object:Gem::Version
72
224
  version: '0'
73
- none: false
74
225
  required_rubygems_version: !ruby/object:Gem::Requirement
226
+ none: false
75
227
  requirements:
76
- - - '>='
228
+ - - ! '>='
77
229
  - !ruby/object:Gem::Version
78
230
  version: '0'
79
- none: false
80
231
  requirements: []
81
- rubyforge_project:
82
- rubygems_version: 1.8.24
83
- signing_key:
232
+ rubyforge_project:
233
+ rubygems_version: 1.8.25
234
+ signing_key:
84
235
  specification_version: 3
85
- summary: angust-remote
86
- test_files: []
236
+ summary: Client for building service objects.
237
+ test_files:
238
+ - spec/angus/remote/service_directory_spec.rb
239
+ - spec/angus/remote/proxy_client_utils_spec.rb
240
+ - spec/angus/remote/http/query_params_spec.rb
241
+ - spec/angus/remote/http/multipart_spec.rb
242
+ - spec/angus/remote/http/multipart_methods/multipart_base_spec.rb
243
+ - spec/angus/remote/utils_spec.rb
244
+ - spec/angus/remote/response/builder_spec.rb
245
+ - spec/angus/remote/client_spec.rb
246
+ - spec/angus/remote/builder_spec.rb