heracles-wrapper 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/heracles-wrapper/exceptions.rb +10 -2
- data/lib/heracles-wrapper/request/create_job.rb +5 -3
- data/lib/heracles-wrapper/request_success.rb +3 -3
- data/lib/heracles-wrapper/test_helper.rb +3 -3
- data/lib/heracles-wrapper/version.rb +1 -1
- data/spec/heracles-wrapper/exceptions_spec.rb +16 -7
- data/spec/heracles-wrapper/request/create_job_spec.rb +23 -10
- data/spec/heracles-wrapper/request_success_spec.rb +54 -27
- data/spec/heracles-wrapper/test_helper_spec.rb +2 -2
- metadata +4 -4
@@ -1,10 +1,18 @@
|
|
1
|
+
require 'json'
|
1
2
|
module Heracles
|
2
3
|
module Wrapper
|
4
|
+
DefaultErrorObject = Hash
|
3
5
|
class RequestFailure < RuntimeError
|
4
|
-
attr_reader :code, :
|
6
|
+
attr_reader :code, :errors, :response
|
5
7
|
def initialize(response)
|
6
8
|
@code = response.respond_to?(:code) ? response.code : 500
|
7
|
-
|
9
|
+
begin
|
10
|
+
@errors = response.respond_to?(:body) ?
|
11
|
+
JSON.parse(response.body).fetch('errors',{}) :
|
12
|
+
{}
|
13
|
+
rescue JSON::ParserError
|
14
|
+
@errors = {"response" => "Not JSON format; See response.body"}
|
15
|
+
end
|
8
16
|
@response = response
|
9
17
|
super("code: #{@code}")
|
10
18
|
end
|
@@ -31,10 +31,12 @@ class Heracles::Wrapper::Request::CreateJob
|
|
31
31
|
def as_json
|
32
32
|
{
|
33
33
|
:api_key => config.api_key,
|
34
|
-
:
|
35
|
-
|
34
|
+
:job => {
|
35
|
+
:workflow_name => workflow_name,
|
36
|
+
:parameters => parameters
|
37
|
+
}
|
36
38
|
}.tap {|hash|
|
37
|
-
hash[:parent_job_id] = parent_job_id if parent_job_id
|
39
|
+
hash[:job][:parent_job_id] = parent_job_id if parent_job_id
|
38
40
|
}
|
39
41
|
end
|
40
42
|
|
@@ -9,14 +9,14 @@ module Heracles
|
|
9
9
|
attr_reader(
|
10
10
|
:job_id,
|
11
11
|
:location,
|
12
|
-
:
|
12
|
+
:errors,
|
13
13
|
:code
|
14
14
|
)
|
15
15
|
def initialize(http_response)
|
16
16
|
super(http_response)
|
17
17
|
@json = JSON.parse(http_response.body)
|
18
|
-
@job_id = @json.fetch('
|
19
|
-
@
|
18
|
+
@job_id = @json.fetch('job').fetch('id').to_i
|
19
|
+
@errors = @json.fetch('errors',{})
|
20
20
|
@location = http_response.headers.fetch(:location)
|
21
21
|
@code = http_response.code
|
22
22
|
end
|
@@ -6,7 +6,7 @@ module Heracles::Wrapper
|
|
6
6
|
RESPONSE_JOB_ID = 1234.freeze
|
7
7
|
RESPONSE_CODE = 201.freeze
|
8
8
|
|
9
|
-
def with_heracles_service_failure_stub(service_name,
|
9
|
+
def with_heracles_service_failure_stub(service_name, errors = [])
|
10
10
|
wrap_service_with_proxy(service_name) do
|
11
11
|
Heracles::Wrapper.send(
|
12
12
|
"#{service_name}_service=",
|
@@ -18,7 +18,7 @@ module Heracles::Wrapper
|
|
18
18
|
:parameters => options.fetch(:parameters, {})
|
19
19
|
).tap { |obj|
|
20
20
|
def obj.call
|
21
|
-
raise Heracles::Wrapper::RequestFailure.new(
|
21
|
+
raise Heracles::Wrapper::RequestFailure.new(errors)
|
22
22
|
end
|
23
23
|
}
|
24
24
|
}
|
@@ -37,7 +37,7 @@ module Heracles::Wrapper
|
|
37
37
|
"/jobs/#{response[:job_id]}"
|
38
38
|
)
|
39
39
|
response[:code] ||= RESPONSE_CODE
|
40
|
-
response[:
|
40
|
+
response[:errors] ||= []
|
41
41
|
|
42
42
|
Heracles::Wrapper.send(
|
43
43
|
"#{service_name}_service=",
|
@@ -8,11 +8,20 @@ describe Heracles::Wrapper::RequestFailure do
|
|
8
8
|
OpenStruct.new(:code => expected_code, :body => expected_body)
|
9
9
|
}
|
10
10
|
let(:expected_code) { 123 }
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
describe 'without errors' do
|
12
|
+
let(:expected_body) { "{\"hello\":\"world\"}" }
|
13
|
+
it('has #code') { subject.code.must_equal expected_code }
|
14
|
+
it('has #errors') { subject.errors.must_equal({}) }
|
15
|
+
it('has #response') { subject.response.must_equal response }
|
16
|
+
it('has #to_s') { subject.to_s.must_equal "code: #{expected_code}" }
|
17
|
+
end
|
18
|
+
describe 'with errors' do
|
19
|
+
let(:expected_body) { "{\"errors\": {\"world\": 1}}" }
|
20
|
+
it('has #code') { subject.code.must_equal expected_code }
|
21
|
+
it('has #errors') { subject.errors.fetch('world').must_equal 1 }
|
22
|
+
it('has #response') { subject.response.must_equal response }
|
23
|
+
it('has #to_s') { subject.to_s.must_equal "code: #{expected_code}" }
|
24
|
+
end
|
16
25
|
end
|
17
26
|
describe 'poorly formed response as per a timeout' do
|
18
27
|
subject { Heracles::Wrapper::RequestFailure.new(response) }
|
@@ -20,8 +29,8 @@ describe Heracles::Wrapper::RequestFailure do
|
|
20
29
|
let(:expected_code) { 500 }
|
21
30
|
let(:expected_body) { '' }
|
22
31
|
it('has #code') { subject.code.must_equal expected_code }
|
23
|
-
it('has #
|
32
|
+
it('has #errors') { subject.errors.must_equal({}) }
|
24
33
|
it('has #response') { subject.response.must_equal response }
|
25
34
|
it('has #to_s') { subject.to_s.must_equal "code: #{expected_code}" }
|
26
35
|
end
|
27
|
-
end
|
36
|
+
end
|
@@ -41,7 +41,7 @@ describe 'Heracles::Wrapper::Request::CreateJob' do
|
|
41
41
|
stub_request(:post, subject.url.to_s).
|
42
42
|
to_return(
|
43
43
|
{
|
44
|
-
:body => %({"
|
44
|
+
:body => %({"job" : { "id" : "#{expected_job_id}" } }),
|
45
45
|
:status => 201,
|
46
46
|
:headers => {
|
47
47
|
:content_type => 'application/json',
|
@@ -58,14 +58,21 @@ describe 'Heracles::Wrapper::Request::CreateJob' do
|
|
58
58
|
stub_request(:post, subject.url.to_s).to_timeout
|
59
59
|
lambda {
|
60
60
|
subject.call
|
61
|
-
}.must_raise
|
61
|
+
}.must_raise(Heracles::Wrapper::RequestFailure)
|
62
62
|
end
|
63
63
|
|
64
64
|
it 'handles redirection' do
|
65
65
|
stub_request(:post, subject.url.to_s).to_return(:status => 302)
|
66
66
|
lambda {
|
67
67
|
subject.call
|
68
|
-
}.must_raise
|
68
|
+
}.must_raise(Heracles::Wrapper::RequestFailure)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'handles server error' do
|
72
|
+
stub_request(:post, subject.url.to_s).to_return(:status => 500)
|
73
|
+
lambda {
|
74
|
+
subject.call
|
75
|
+
}.must_raise(Heracles::Wrapper::RequestFailure)
|
69
76
|
end
|
70
77
|
|
71
78
|
it 'handles 404' do
|
@@ -116,8 +123,10 @@ describe 'Heracles::Wrapper::Request::CreateJob' do
|
|
116
123
|
subject.as_json.must_equal(
|
117
124
|
{
|
118
125
|
:api_key => expected_api_key,
|
119
|
-
:
|
120
|
-
|
126
|
+
:job => {
|
127
|
+
:workflow_name => expected_workflow_name,
|
128
|
+
:parameters => {}
|
129
|
+
}
|
121
130
|
}
|
122
131
|
)
|
123
132
|
end
|
@@ -129,9 +138,11 @@ describe 'Heracles::Wrapper::Request::CreateJob' do
|
|
129
138
|
subject.as_json.must_equal(
|
130
139
|
{
|
131
140
|
:api_key => expected_api_key,
|
132
|
-
:
|
133
|
-
|
134
|
-
|
141
|
+
:job => {
|
142
|
+
:workflow_name => expected_workflow_name,
|
143
|
+
:parent_job_id => expected_parent_job_id,
|
144
|
+
:parameters => {}
|
145
|
+
}
|
135
146
|
}
|
136
147
|
)
|
137
148
|
end
|
@@ -153,8 +164,10 @@ describe 'Heracles::Wrapper::Request::CreateJob' do
|
|
153
164
|
subject.as_json.must_equal(
|
154
165
|
{
|
155
166
|
:api_key => expected_api_key,
|
156
|
-
:
|
157
|
-
|
167
|
+
:job => {
|
168
|
+
:workflow_name => expected_workflow_name,
|
169
|
+
:parameters => options[:parameters]
|
170
|
+
}
|
158
171
|
}
|
159
172
|
)
|
160
173
|
end
|
@@ -5,45 +5,72 @@ require File.expand_path('../../../lib/heracles-wrapper/config', __FILE__)
|
|
5
5
|
require 'minitest/autorun'
|
6
6
|
require 'webmock/minitest'
|
7
7
|
require 'ostruct'
|
8
|
+
require 'json'
|
8
9
|
|
9
10
|
describe 'Heracles::Wrapper::RequestSuccess' do
|
10
11
|
subject { Heracles::Wrapper::RequestSuccess.new(http_response) }
|
11
12
|
let(:expected_job_id) { 1234 }
|
12
|
-
let(:
|
13
|
+
let(:expected_errors) { %({"name" : "one message"}) }
|
13
14
|
let(:expected_code) { 201 }
|
14
15
|
let(:expected_location) { 'http://somewhere.over/the/rainbown' }
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
16
|
+
describe 'without errors' do
|
17
|
+
let(:http_response) {
|
18
|
+
OpenStruct.new(
|
19
|
+
:body => %(
|
20
|
+
{
|
21
|
+
"job": {
|
22
|
+
"id": #{expected_job_id}
|
23
|
+
}
|
24
|
+
}
|
25
|
+
),
|
26
|
+
:headers => { :location => expected_location },
|
27
|
+
:code => expected_code
|
28
|
+
).tap { |obj|
|
29
|
+
def obj.foo_bar; 'Baz'; end
|
30
|
+
}
|
27
31
|
}
|
28
|
-
|
32
|
+
it 'has #errors' do
|
33
|
+
subject.errors.must_equal({})
|
34
|
+
end
|
29
35
|
|
30
|
-
it 'has #code' do
|
31
|
-
subject.code.must_equal expected_code
|
32
|
-
end
|
33
36
|
|
34
|
-
it 'has #job_id' do
|
35
|
-
subject.job_id.must_equal expected_job_id
|
36
37
|
end
|
38
|
+
describe 'with errors' do
|
39
|
+
let(:http_response) {
|
40
|
+
OpenStruct.new(
|
41
|
+
:body => %(
|
42
|
+
{
|
43
|
+
"job": {
|
44
|
+
"id": #{expected_job_id}
|
45
|
+
},
|
46
|
+
"errors": #{expected_errors}
|
47
|
+
}
|
48
|
+
),
|
49
|
+
:headers => { :location => expected_location },
|
50
|
+
:code => expected_code
|
51
|
+
).tap { |obj|
|
52
|
+
def obj.foo_bar; 'Baz'; end
|
53
|
+
}
|
54
|
+
}
|
37
55
|
|
38
|
-
|
39
|
-
|
40
|
-
|
56
|
+
it 'has #code' do
|
57
|
+
subject.code.must_equal expected_code
|
58
|
+
end
|
41
59
|
|
42
|
-
|
43
|
-
|
44
|
-
|
60
|
+
it 'has #job_id' do
|
61
|
+
subject.job_id.must_equal expected_job_id
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'has #location' do
|
65
|
+
subject.location.must_equal expected_location
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'has #errors' do
|
69
|
+
subject.errors.must_equal JSON.parse(expected_errors)
|
70
|
+
end
|
45
71
|
|
46
|
-
|
47
|
-
|
72
|
+
it 'delegates everything else to the http_response' do
|
73
|
+
subject.foo_bar.must_equal 'Baz'
|
74
|
+
end
|
48
75
|
end
|
49
76
|
end
|
@@ -43,7 +43,7 @@ describe Heracles::Wrapper::TestHelper do
|
|
43
43
|
:create_job, input_parameters
|
44
44
|
).call
|
45
45
|
@response.job_id.must_be_kind_of Fixnum
|
46
|
-
@response.
|
46
|
+
@response.errors.must_be_kind_of Array
|
47
47
|
@response.code.must_be_kind_of Fixnum
|
48
48
|
@response.location.must_be_kind_of String
|
49
49
|
end
|
@@ -73,7 +73,7 @@ describe Heracles::Wrapper::TestHelper do
|
|
73
73
|
).
|
74
74
|
to_return(
|
75
75
|
{
|
76
|
-
:body => %({"
|
76
|
+
:body => %({"job" : {"id" : "#{expected_job_id}"}}),
|
77
77
|
:status => expected_code,
|
78
78
|
:headers => {
|
79
79
|
:content_type => 'application/json',
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: heracles-wrapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rest-client
|
@@ -193,7 +193,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
193
193
|
version: '0'
|
194
194
|
segments:
|
195
195
|
- 0
|
196
|
-
hash:
|
196
|
+
hash: 3196260337063136150
|
197
197
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
198
198
|
none: false
|
199
199
|
requirements:
|
@@ -202,7 +202,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
202
202
|
version: '0'
|
203
203
|
segments:
|
204
204
|
- 0
|
205
|
-
hash:
|
205
|
+
hash: 3196260337063136150
|
206
206
|
requirements: []
|
207
207
|
rubyforge_project:
|
208
208
|
rubygems_version: 1.8.24
|