hyperion_http 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.rspec +4 -0
  4. data/.travis.yml +7 -0
  5. data/CHANGES.md +145 -0
  6. data/Gemfile +5 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +421 -0
  9. data/Rakefile +11 -0
  10. data/hyperion_http.gemspec +34 -0
  11. data/lib/hyperion/aux/bug_error.rb +2 -0
  12. data/lib/hyperion/aux/hash_ext.rb +5 -0
  13. data/lib/hyperion/aux/logger.rb +54 -0
  14. data/lib/hyperion/aux/typho.rb +9 -0
  15. data/lib/hyperion/aux/util.rb +18 -0
  16. data/lib/hyperion/aux/version.rb +3 -0
  17. data/lib/hyperion/formats.rb +69 -0
  18. data/lib/hyperion/headers.rb +43 -0
  19. data/lib/hyperion/hyperion.rb +79 -0
  20. data/lib/hyperion/requestor.rb +88 -0
  21. data/lib/hyperion/result_handling/dispatch_dsl.rb +67 -0
  22. data/lib/hyperion/result_handling/dispatching_hyperion_result.rb +10 -0
  23. data/lib/hyperion/result_handling/result_maker.rb +64 -0
  24. data/lib/hyperion/types/client_error_code.rb +9 -0
  25. data/lib/hyperion/types/client_error_detail.rb +46 -0
  26. data/lib/hyperion/types/client_error_response.rb +50 -0
  27. data/lib/hyperion/types/hyperion_error.rb +6 -0
  28. data/lib/hyperion/types/hyperion_result.rb +24 -0
  29. data/lib/hyperion/types/hyperion_status.rb +10 -0
  30. data/lib/hyperion/types/hyperion_uri.rb +97 -0
  31. data/lib/hyperion/types/payload_descriptor.rb +9 -0
  32. data/lib/hyperion/types/response_descriptor.rb +21 -0
  33. data/lib/hyperion/types/rest_route.rb +20 -0
  34. data/lib/hyperion.rb +15 -0
  35. data/lib/hyperion_test/fake.rb +64 -0
  36. data/lib/hyperion_test/fake_server/config.rb +36 -0
  37. data/lib/hyperion_test/fake_server/dispatcher.rb +74 -0
  38. data/lib/hyperion_test/fake_server/types.rb +7 -0
  39. data/lib/hyperion_test/fake_server.rb +54 -0
  40. data/lib/hyperion_test/spec_helper.rb +19 -0
  41. data/lib/hyperion_test/test_framework_hooks.rb +34 -0
  42. data/lib/hyperion_test.rb +2 -0
  43. data/spec/lib/hyperion/aux/util_spec.rb +29 -0
  44. data/spec/lib/hyperion/formats_spec.rb +84 -0
  45. data/spec/lib/hyperion/headers_spec.rb +61 -0
  46. data/spec/lib/hyperion/logger_spec.rb +60 -0
  47. data/spec/lib/hyperion/test_spec.rb +222 -0
  48. data/spec/lib/hyperion/types/client_error_response_spec.rb +52 -0
  49. data/spec/lib/hyperion/types/hyperion_result_spec.rb +17 -0
  50. data/spec/lib/hyperion/types/hyperion_uri_spec.rb +113 -0
  51. data/spec/lib/hyperion_spec.rb +187 -0
  52. data/spec/lib/superion_spec.rb +151 -0
  53. data/spec/lib/types_spec.rb +46 -0
  54. data/spec/spec_helper.rb +3 -0
  55. data/spec/support/core_helpers.rb +5 -0
  56. metadata +280 -0
@@ -0,0 +1,151 @@
1
+ require 'ostruct'
2
+ require 'hyperion_test'
3
+
4
+ class ClassWithHyperionHandlers
5
+ include Hyperion::Requestor
6
+
7
+ def initialize(predetermined_result)
8
+ @predetermined_result = predetermined_result
9
+ end
10
+
11
+ def hyperion_handler(result)
12
+ result.when(->{true}) { @predetermined_result }
13
+ end
14
+ end
15
+
16
+ describe Hyperion::Requestor do
17
+ include Hyperion::Requestor
18
+
19
+ def arrange(method, response)
20
+ @route = RestRoute.new(method, 'http://indigo.com/things',
21
+ ResponseDescriptor.new('thing', 1, :json),
22
+ PayloadDescriptor.new(:json))
23
+
24
+ fake_route(@route) do |request|
25
+ @request = request
26
+ response
27
+ end
28
+ end
29
+
30
+ def assert_result(expected_result)
31
+ expect(@result).to eql expected_result
32
+ end
33
+
34
+ it 'makes requests' do
35
+ arrange(:get, {'a' => 'b'})
36
+ @result = request(@route)
37
+ assert_result({'a' => 'b'})
38
+ end
39
+
40
+ it 'makes requests with payload bodies' do
41
+ arrange(:put, {'a' => 'b'})
42
+ @result = request(@route, body: {'the' => 'body'})
43
+ assert_result({'a' => 'b'})
44
+ expect(@request.body).to eql({'the' => 'body'})
45
+ end
46
+
47
+ it 'renders the response with the render proc' do
48
+ arrange(:get, {'x' => 1, 'y' => 2})
49
+ @result = request(@route, render: OpenStruct.method(:new))
50
+ assert_result(OpenStruct.new(x: 1, y: 2))
51
+ end
52
+
53
+ context 'when a `hyperion_handlers` method is defined' do
54
+ it 'takes precedence over the core handlers' do
55
+ arrange(:get, [200, {}, nil])
56
+ hyperion_handler_result = double
57
+ route = @route
58
+ @result = ClassWithHyperionHandlers.new(hyperion_handler_result).instance_eval do
59
+ request(route)
60
+ end
61
+ assert_result(hyperion_handler_result)
62
+ end
63
+ end
64
+
65
+ context 'when request handlers are provided' do
66
+ it 'earlier handlers take precedence over later ones' do
67
+ arrange(:get, [333, {}, nil])
68
+ custom_handlers = {
69
+ proc {|x| x.code.odd?} => 'odd',
70
+ 333 => 'got 333'
71
+ }
72
+
73
+ @result = request(@route, also_handle: custom_handlers)
74
+ assert_result('odd')
75
+ end
76
+ end
77
+
78
+ context 'when given a block' do
79
+ before :each do
80
+ arrange(:get, {'x' => 1, 'y' => 2})
81
+ @result = request(@route, render: OpenStruct.method(:new)) do |point|
82
+ @point = point
83
+ :block_result
84
+ end
85
+ end
86
+ it 'passes the rendered response to the block' do
87
+ expect(@point).to be_an OpenStruct
88
+ end
89
+ it "returns the block's return value" do
90
+ expect(@result).to eql :block_result
91
+ end
92
+ end
93
+
94
+ it 'rejects invalid arguments' do
95
+ arrange(:get, {'a' => 'b'})
96
+ expect{request(:foo)}.to raise_error 'You passed me :foo, which is not a RestRoute'
97
+ expect{request(@route, :foo)}.to raise_error 'You passed me :foo, which is not an options hash'
98
+ end
99
+
100
+ context 'by default' do
101
+ context 'on a 400-level response' do
102
+
103
+ def example(opts)
104
+ arrange(:get, [(400..499).to_a.sample, {}, opts[:response]])
105
+ expect{request(@route)}.to raise_error HyperionError, opts[:error]
106
+ end
107
+
108
+
109
+
110
+ context 'when the response is a properly-formed error message' do
111
+ it 'raises an error with the response message' do
112
+ example response: '{"message":"oops"}',
113
+ error: 'oops'
114
+ end
115
+ end
116
+ context 'when the response is not a properly-formed error message' do
117
+ it 'raises an error containing the route and the dumped response' do
118
+ example response: '{"huh":"wut"}',
119
+ error: 'The request failed: GET http://indigo.com/things: {"huh"=>"wut"}'
120
+ end
121
+ end
122
+ context 'when there is no response' do
123
+ it 'raises an generic error message containing the route' do
124
+ example response: nil,
125
+ error: 'The request failed: GET http://indigo.com/things'
126
+ end
127
+ end
128
+ end
129
+
130
+ context 'on a 404 response' do
131
+ it 'raises an error' do
132
+ arrange(:get, [404, {}, nil])
133
+ expect{request(@route)}.to raise_error HyperionError, "Got HTTP 404 for #{@route}. Is the route implemented?"
134
+ end
135
+ end
136
+
137
+ context 'on a 500-level response' do
138
+ it 'raises an error' do
139
+ arrange(:get, [(500..599).to_a.sample, {}, {'a' => 1}])
140
+ expect{request(@route)}.to raise_error HyperionError, "#{@route}\n{\"a\"=>1}"
141
+ end
142
+ end
143
+
144
+ context 'when a result falls through' do
145
+ it 'raises an error' do
146
+ arrange(:get, [(300..399).to_a.sample, {}, nil])
147
+ expect{request(@route)}.to raise_error HyperionError, /response did not match any conditions/
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,46 @@
1
+ require 'rspec'
2
+ require 'hyperion'
3
+
4
+ describe HyperionResult do
5
+ describe '#to_s' do
6
+ let!(:route) { RestRoute.new(:get, 'http://somesite.org/foo/bar') }
7
+
8
+ def make_result(status, code=400)
9
+ HyperionResult.new(route, status, code)
10
+ end
11
+
12
+ it 'pretty prints the result' do
13
+ msg = "Success: #{route.to_s}"
14
+ expect(make_result(HyperionStatus::SUCCESS).to_s).to eql msg
15
+
16
+ msg = "Timed out: #{route.to_s}"
17
+ expect(make_result(HyperionStatus::TIMED_OUT).to_s).to eql msg
18
+
19
+ msg = "No response: #{route.to_s}"
20
+ expect(make_result(HyperionStatus::NO_RESPONSE).to_s).to eql msg
21
+
22
+ msg = "Bad route (404): #{route.to_s}"
23
+ expect(make_result(HyperionStatus::BAD_ROUTE, 404).to_s).to eql msg
24
+
25
+ msg = "Client error: #{route.to_s}"
26
+ expect(make_result(HyperionStatus::CLIENT_ERROR, 400).to_s).to eql msg
27
+
28
+ msg = "Server error: #{route.to_s}"
29
+ expect(make_result(HyperionStatus::SERVER_ERROR, 500).to_s).to eql msg
30
+
31
+ msg = "HTTP 432: #{route.to_s}"
32
+ expect(make_result(HyperionStatus::CHECK_CODE, 432).to_s).to eql msg
33
+ end
34
+ end
35
+ end
36
+
37
+ describe ResponseDescriptor do
38
+ include Hyperion::Headers
39
+
40
+ describe '#to_s' do
41
+ it 'returns the short mimetype' do
42
+ rd = ResponseDescriptor.new('ttt', 999, :json)
43
+ expect(rd.to_s).to eql short_mimetype(rd)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ require 'rspec'
2
+ require 'json_spec'
3
+ require 'hyperion'
@@ -0,0 +1,5 @@
1
+ RSpec.configure do |config|
2
+ config.mock_with :rspec do |mocks|
3
+ mocks.verify_partial_doubles = true
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,280 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hyperion_http
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Indigo BioAutomation, Inc.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: yard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: json_spec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec_junit_formatter
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: abstractivator
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activesupport
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: immutable_struct
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.1'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.1'
139
+ - !ruby/object:Gem::Dependency
140
+ name: oj
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '2.12'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '2.12'
153
+ - !ruby/object:Gem::Dependency
154
+ name: typhoeus
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.7'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '0.7'
167
+ - !ruby/object:Gem::Dependency
168
+ name: mimic
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 0.4.3
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 0.4.3
181
+ description: Ruby REST client for internal service architecture
182
+ email:
183
+ - pwinton@indigobio.com
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - ".gitignore"
189
+ - ".rspec"
190
+ - ".travis.yml"
191
+ - CHANGES.md
192
+ - Gemfile
193
+ - LICENSE.txt
194
+ - README.md
195
+ - Rakefile
196
+ - hyperion_http.gemspec
197
+ - lib/hyperion.rb
198
+ - lib/hyperion/aux/bug_error.rb
199
+ - lib/hyperion/aux/hash_ext.rb
200
+ - lib/hyperion/aux/logger.rb
201
+ - lib/hyperion/aux/typho.rb
202
+ - lib/hyperion/aux/util.rb
203
+ - lib/hyperion/aux/version.rb
204
+ - lib/hyperion/formats.rb
205
+ - lib/hyperion/headers.rb
206
+ - lib/hyperion/hyperion.rb
207
+ - lib/hyperion/requestor.rb
208
+ - lib/hyperion/result_handling/dispatch_dsl.rb
209
+ - lib/hyperion/result_handling/dispatching_hyperion_result.rb
210
+ - lib/hyperion/result_handling/result_maker.rb
211
+ - lib/hyperion/types/client_error_code.rb
212
+ - lib/hyperion/types/client_error_detail.rb
213
+ - lib/hyperion/types/client_error_response.rb
214
+ - lib/hyperion/types/hyperion_error.rb
215
+ - lib/hyperion/types/hyperion_result.rb
216
+ - lib/hyperion/types/hyperion_status.rb
217
+ - lib/hyperion/types/hyperion_uri.rb
218
+ - lib/hyperion/types/payload_descriptor.rb
219
+ - lib/hyperion/types/response_descriptor.rb
220
+ - lib/hyperion/types/rest_route.rb
221
+ - lib/hyperion_test.rb
222
+ - lib/hyperion_test/fake.rb
223
+ - lib/hyperion_test/fake_server.rb
224
+ - lib/hyperion_test/fake_server/config.rb
225
+ - lib/hyperion_test/fake_server/dispatcher.rb
226
+ - lib/hyperion_test/fake_server/types.rb
227
+ - lib/hyperion_test/spec_helper.rb
228
+ - lib/hyperion_test/test_framework_hooks.rb
229
+ - spec/lib/hyperion/aux/util_spec.rb
230
+ - spec/lib/hyperion/formats_spec.rb
231
+ - spec/lib/hyperion/headers_spec.rb
232
+ - spec/lib/hyperion/logger_spec.rb
233
+ - spec/lib/hyperion/test_spec.rb
234
+ - spec/lib/hyperion/types/client_error_response_spec.rb
235
+ - spec/lib/hyperion/types/hyperion_result_spec.rb
236
+ - spec/lib/hyperion/types/hyperion_uri_spec.rb
237
+ - spec/lib/hyperion_spec.rb
238
+ - spec/lib/superion_spec.rb
239
+ - spec/lib/types_spec.rb
240
+ - spec/spec_helper.rb
241
+ - spec/support/core_helpers.rb
242
+ homepage: ''
243
+ licenses:
244
+ - MIT
245
+ metadata: {}
246
+ post_install_message:
247
+ rdoc_options: []
248
+ require_paths:
249
+ - lib
250
+ required_ruby_version: !ruby/object:Gem::Requirement
251
+ requirements:
252
+ - - ">="
253
+ - !ruby/object:Gem::Version
254
+ version: '0'
255
+ required_rubygems_version: !ruby/object:Gem::Requirement
256
+ requirements:
257
+ - - ">="
258
+ - !ruby/object:Gem::Version
259
+ version: '0'
260
+ requirements: []
261
+ rubyforge_project:
262
+ rubygems_version: 2.4.5
263
+ signing_key:
264
+ specification_version: 4
265
+ summary: Ruby REST client
266
+ test_files:
267
+ - spec/lib/hyperion/aux/util_spec.rb
268
+ - spec/lib/hyperion/formats_spec.rb
269
+ - spec/lib/hyperion/headers_spec.rb
270
+ - spec/lib/hyperion/logger_spec.rb
271
+ - spec/lib/hyperion/test_spec.rb
272
+ - spec/lib/hyperion/types/client_error_response_spec.rb
273
+ - spec/lib/hyperion/types/hyperion_result_spec.rb
274
+ - spec/lib/hyperion/types/hyperion_uri_spec.rb
275
+ - spec/lib/hyperion_spec.rb
276
+ - spec/lib/superion_spec.rb
277
+ - spec/lib/types_spec.rb
278
+ - spec/spec_helper.rb
279
+ - spec/support/core_helpers.rb
280
+ has_rdoc: