fizzy-api 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f7c3650bd785f1da29b76e7eb3601ea4c6b20529
4
- data.tar.gz: bd84ab4f4c8f6afc2fc00a0827b40d9309156e70
3
+ metadata.gz: 7fc3193e43fa75dbb7385d9a42288b292028eb12
4
+ data.tar.gz: e1ef0ba2c2829a753fa69b8ee9ee5c9e612c9d71
5
5
  SHA512:
6
- metadata.gz: 64e609e8180df1f6447a1bd605dbfea91b319e8bf9395193da81d9de660d18ac83d36f6a9647f6a8ff71b66bdcea1f2f6e7e76060890214aa55a244a145b61d1
7
- data.tar.gz: 78dd0fccbe96ffe69035ca590b774aabd017312eef24b056940ad98d962b8c209dd265614066f5ae8b468104e6fe902a2965455adc1856b8df848efa2f8f3dae
6
+ metadata.gz: 375e483780dfbc2574db63c3a66f4112cd3f10256514450c591f02696ae27c89305804b520fb6a721691165c9c318cb4cb70976b02e168910216382eb20f66a2
7
+ data.tar.gz: 06d7b0af3f48be07aab2b4203c25a4c274feba63d442c60006be9af97fe57b04662baf522275dcf070dcdf79436825c4a9ea6b53a6bea205616222d6246164be
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fizzy-api (0.0.6)
4
+ fizzy-api (0.1.0)
5
5
  active_interaction (~> 2.0)
6
6
  httparty (~> 0.12)
7
7
  virtus (~> 1.0)
@@ -0,0 +1,32 @@
1
+ module Fizzy
2
+ module Api
3
+ module Endpoints
4
+ class CalculateOutcome < Endpoint
5
+ string :dossier_id
6
+ string :protocol_subscription_id
7
+
8
+ def execute
9
+ response = Api.basic_auth_session.post("/dossier/#{dossier_id}"\
10
+ "/protocol_subscriptions/#{protocol_subscription_id}"\
11
+ '/calculate')
12
+ process_response(response)
13
+ end
14
+
15
+ private
16
+
17
+ def invalid_response(response)
18
+ case response.code
19
+ when 202
20
+ raise(Errors::OutcomeNotAvailableError,
21
+ select_measurement_text(response.body, 'The results are currently being calculated.'))
22
+ when 404
23
+ raise Errors::GraphNotFoundError, select_measurement_text(response.body,
24
+ 'Participant not found, or graph not supported.')
25
+ else
26
+ raise Errors::UnexpectedStatusError, "Status code #{response.code} not expected."
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,26 @@
1
+ module Fizzy
2
+ module Api
3
+ module Endpoints
4
+ class Endpoint < ActiveInteraction::Base
5
+ protected
6
+
7
+ def process_response(response)
8
+ case response.code
9
+ when 200
10
+ response
11
+ else
12
+ invalid_response(response)
13
+ end
14
+ end
15
+
16
+ def invalid_response(response)
17
+ raise Errors::UnexpectedStatusError, "Status code #{response.code} not expected."
18
+ end
19
+
20
+ def select_measurement_text(response, default_message)
21
+ response.body || default_message
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,37 @@
1
+ module Fizzy
2
+ module Api
3
+ module Endpoints
4
+ class RenderGraph < Endpoint
5
+ string :dossier_id
6
+ string :protocol_subscription_id
7
+ string :path
8
+ hash :options, default: {}
9
+
10
+ def execute
11
+ response = Api.basic_auth_session.get("/dossier/#{dossier_id}"\
12
+ "/protocol_subscriptions/#{protocol_subscription_id}" \
13
+ "/render/#{path}", options)
14
+ process_response(response)
15
+ end
16
+
17
+ private
18
+
19
+ def invalid_response(response)
20
+ case response.code
21
+ when 202
22
+ raise Errors::OutcomeNotAvailableError, select_measurement_text(response,
23
+ 'The results have not yet been calculated.')
24
+ when 204
25
+ raise Errors::TooFewMeasurementsError, select_measurement_text(response,
26
+ 'Not enough measurements available.')
27
+ when 404
28
+ raise Errors::GraphNotFoundError, select_measurement_text(response,
29
+ 'Graph not found.')
30
+ else
31
+ raise Errors::UnexpectedStatusError, "Status code #{response.code} not expected."
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ module Fizzy
2
+ module Api
3
+ module Endpoints
4
+ class RenderGraphSynchronous < Endpoint
5
+ string :dossier_id
6
+ string :protocol_subscription_id
7
+ string :path
8
+ hash :options, default: {}
9
+
10
+ def execute
11
+ response = Api.basic_auth_session.get("/dossier/#{dossier_id}"\
12
+ "/protocol_subscriptions/#{protocol_subscription_id}" \
13
+ "/render_sync/#{path}", options)
14
+ process_response(response)
15
+ end
16
+
17
+ private
18
+
19
+ def invalid_response(response)
20
+ case response.code
21
+ when 204
22
+ raise Errors::TooFewMeasurementsError, select_measurement_text(response,
23
+ 'Not enough measurements available.')
24
+ when 404
25
+ raise Errors::GraphNotFoundError, select_measurement_text(response,
26
+ 'Graph not found.')
27
+ when 405
28
+ raise Errors::MethodNotAllowedError, select_measurement_text(response,
29
+ 'Current image path not eligible for'\
30
+ ' synchronous rendering')
31
+ else
32
+ raise Errors::UnexpectedStatusError, "Status code #{response.code} not expected."
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,17 @@
1
+ module Fizzy
2
+ module Api
3
+ module Endpoints
4
+ class RenderOverview < Endpoint
5
+ string :dossier_id
6
+ string :protocol_subscription_id
7
+
8
+ def execute
9
+ response = Api.basic_auth_session.get("/dossier/#{dossier_id}"\
10
+ "/protocol_subscriptions/#{protocol_subscription_id}" \
11
+ '/render.json')
12
+ process_response(response)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ require 'fizzy/api/endpoints/endpoint'
2
+ require 'fizzy/api/endpoints/calculate_outcome'
3
+ require 'fizzy/api/endpoints/render_graph'
4
+ require 'fizzy/api/endpoints/render_graph_synchronous'
5
+ require 'fizzy/api/endpoints/render_overview'
6
+
7
+ module Fizzy
8
+ module Api
9
+ module Endpoints
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ module Fizzy
2
+ module Api
3
+ module Errors
4
+ class MethodNotAllowedError < StandardError
5
+ end
6
+ end
7
+ end
8
+ end
@@ -2,3 +2,4 @@ require 'fizzy/api/errors/graph_not_found_error'
2
2
  require 'fizzy/api/errors/outcome_not_available_error'
3
3
  require 'fizzy/api/errors/too_few_measurements_error'
4
4
  require 'fizzy/api/errors/unexpected_status_error'
5
+ require 'fizzy/api/errors/method_not_allowed_error'
@@ -1,5 +1,5 @@
1
1
  module Fizzy
2
2
  module Api
3
- VERSION = '0.0.6'.freeze
3
+ VERSION = '0.1.0'.freeze
4
4
  end
5
5
  end
data/lib/fizzy/api.rb CHANGED
@@ -3,10 +3,7 @@ require 'fizzy/api/version'
3
3
  require 'fizzy/api/sessions'
4
4
  require 'fizzy/api/models'
5
5
  require 'fizzy/api/errors'
6
-
7
- require 'fizzy/api/calculate_outcome'
8
- require 'fizzy/api/render_graph'
9
- require 'fizzy/api/render_overview'
6
+ require 'fizzy/api/endpoints'
10
7
 
11
8
  module Fizzy
12
9
  module Api
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+ require 'shared_examples_for_endpoints'
3
+ module Fizzy
4
+ module Api
5
+ module Endpoints
6
+ describe CalculateOutcome do
7
+ let(:dossier_id) { '123' }
8
+ let(:protocol_subscription_id) { 'abc' }
9
+ let(:response) { httparty_response('') }
10
+ let(:session) { FactoryGirl.build :basic_auth_session }
11
+
12
+ it_behaves_like 'an endpoint'
13
+
14
+ before do
15
+ allow(Sessions::BasicAuthSession).to receive(:new).and_return session
16
+ allow(session).to receive(:post).and_return nil
17
+ end
18
+
19
+ it 'should call the correct url' do
20
+ expect(session).to receive(:post)
21
+ .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/calculate")
22
+ .and_return(response)
23
+
24
+ described_class.run! dossier_id: dossier_id,
25
+ protocol_subscription_id: protocol_subscription_id
26
+ end
27
+
28
+ describe 'error handling' do
29
+ it 'should notice a 404' do
30
+ allow(response).to receive(:code).and_return 404
31
+ expect(session).to receive(:post)
32
+ .with('/dossier/fake/protocol_subscriptions/fake/calculate')
33
+ .and_return(response)
34
+ outcome = lambda do
35
+ described_class.run! dossier_id: 'fake',
36
+ protocol_subscription_id: 'fake'
37
+ end
38
+ expect { outcome.call }.to raise_error(Errors::GraphNotFoundError)
39
+ end
40
+
41
+ it 'should fail a 500 with an UnexpectedStatusError' do
42
+ allow(response).to receive(:code).and_return 500
43
+ expect(session).to receive(:post)
44
+ .with('/dossier/fake/protocol_subscriptions/fake/calculate')
45
+ .and_return(response)
46
+ outcome = lambda do
47
+ described_class.run! dossier_id: 'fake',
48
+ protocol_subscription_id: 'fake'
49
+ end
50
+ expect { outcome.call }.to raise_error(Errors::UnexpectedStatusError)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+ require 'shared_examples_for_endpoints'
3
+ module Fizzy
4
+ module Api
5
+ module Endpoints
6
+ describe RenderGraph do
7
+ let(:dossier_id) { '123' }
8
+ let(:protocol_subscription_id) { 'abc' }
9
+ let(:path) { 'welbevinden.svg' }
10
+ let(:options) { {} }
11
+ let(:response) { httparty_response('<svg>') }
12
+ let(:session) { FactoryGirl.build :basic_auth_session }
13
+
14
+ it_behaves_like 'an endpoint'
15
+
16
+ before do
17
+ allow(Sessions::BasicAuthSession).to receive(:new).and_return session
18
+ end
19
+
20
+ it 'should call the correct url' do
21
+ expect(session).to receive(:get)
22
+ .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render/#{path}", options)
23
+ .and_return(response)
24
+ outcome = described_class.run! dossier_id: dossier_id,
25
+ protocol_subscription_id: protocol_subscription_id,
26
+ path: path,
27
+ options: options
28
+ expect(outcome).to eq '<svg>'
29
+ end
30
+
31
+ describe 'error handling' do
32
+ it 'should notice a 202' do
33
+ allow(response).to receive(:code).and_return 202
34
+ expect(session).to receive(:get)
35
+ .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render/#{path}", options)
36
+ .and_return(response)
37
+ outcome = lambda do
38
+ described_class.run! dossier_id: dossier_id,
39
+ protocol_subscription_id: protocol_subscription_id,
40
+ path: path,
41
+ options: options
42
+ end
43
+ expect { outcome.call }.to raise_error(Errors::OutcomeNotAvailableError)
44
+ end
45
+
46
+ it 'should notice a 204' do
47
+ allow(response).to receive(:code).and_return 204
48
+ expect(session).to receive(:get)
49
+ .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render/#{path}", options)
50
+ .and_return(response)
51
+ outcome = lambda do
52
+ described_class.run! dossier_id: dossier_id,
53
+ protocol_subscription_id: protocol_subscription_id,
54
+ path: path,
55
+ options: options
56
+ end
57
+ expect { outcome.call }.to raise_error(Errors::TooFewMeasurementsError)
58
+ end
59
+
60
+ it 'should notice a 404' do
61
+ allow(response).to receive(:code).and_return 404
62
+ expect(session).to receive(:get)
63
+ .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render/#{path}", options)
64
+ .and_return(response)
65
+ outcome = lambda do
66
+ described_class.run! dossier_id: dossier_id,
67
+ protocol_subscription_id: protocol_subscription_id,
68
+ path: path,
69
+ options: options
70
+ end
71
+ expect { outcome.call }.to raise_error(Errors::GraphNotFoundError)
72
+ end
73
+
74
+ it 'should fail a 500 with an UnexpectedStatusError' do
75
+ allow(response).to receive(:code).and_return 500
76
+ expect(session).to receive(:get)
77
+ .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render/#{path}", options)
78
+ .and_return(response)
79
+ outcome = lambda do
80
+ described_class.run! dossier_id: dossier_id,
81
+ protocol_subscription_id: protocol_subscription_id,
82
+ path: path,
83
+ options: options
84
+ end
85
+ expect { outcome.call }.to raise_error(Errors::UnexpectedStatusError)
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+ require 'shared_examples_for_endpoints'
3
+ module Fizzy
4
+ module Api
5
+ module Endpoints
6
+ describe RenderGraphSynchronous do
7
+ let(:dossier_id) { '123' }
8
+ let(:protocol_subscription_id) { 'abc' }
9
+ let(:path) { 'welbevinden.svg' }
10
+ let(:options) { {} }
11
+ let(:response) { httparty_response('<svg>') }
12
+ let(:session) { FactoryGirl.build :basic_auth_session }
13
+
14
+ it_behaves_like 'an endpoint'
15
+
16
+ before do
17
+ allow(Sessions::BasicAuthSession).to receive(:new).and_return session
18
+ end
19
+
20
+ it 'should call the correct url' do
21
+ expect(session).to receive(:get)
22
+ .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render_sync/#{path}",
23
+ options)
24
+ .and_return(response)
25
+ outcome = described_class.run! dossier_id: dossier_id,
26
+ protocol_subscription_id: protocol_subscription_id,
27
+ path: path,
28
+ options: options
29
+ expect(outcome).to eq '<svg>'
30
+ end
31
+
32
+ describe 'error handling' do
33
+ it 'should notice a 204' do
34
+ allow(response).to receive(:code).and_return 204
35
+ expect(session).to receive(:get)
36
+ .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render_sync/#{path}",
37
+ options)
38
+ .and_return(response)
39
+ outcome = lambda do
40
+ described_class.run! dossier_id: dossier_id,
41
+ protocol_subscription_id: protocol_subscription_id,
42
+ path: path,
43
+ options: options
44
+ end
45
+ expect { outcome.call }.to raise_error(Errors::TooFewMeasurementsError)
46
+ end
47
+
48
+ it 'should notice a 404' do
49
+ allow(response).to receive(:code).and_return 404
50
+ expect(session).to receive(:get)
51
+ .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render_sync/#{path}",
52
+ options)
53
+ .and_return(response)
54
+ outcome = lambda do
55
+ described_class.run! dossier_id: dossier_id,
56
+ protocol_subscription_id: protocol_subscription_id,
57
+ path: path,
58
+ options: options
59
+ end
60
+ expect { outcome.call }.to raise_error(Errors::GraphNotFoundError)
61
+ end
62
+
63
+ it 'should notice a 405' do
64
+ allow(response).to receive(:code).and_return 405
65
+ expect(session).to receive(:get)
66
+ .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render_sync/#{path}",
67
+ options)
68
+ .and_return(response)
69
+ outcome = lambda do
70
+ described_class.run! dossier_id: dossier_id,
71
+ protocol_subscription_id: protocol_subscription_id,
72
+ path: path,
73
+ options: options
74
+ end
75
+ expect { outcome.call }.to raise_error(Errors::MethodNotAllowedError)
76
+ end
77
+
78
+ it 'should fail a 500 with an UnexpectedStatusError' do
79
+ allow(response).to receive(:code).and_return 500
80
+ expect(session).to receive(:get)
81
+ .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render_sync/#{path}",
82
+ options)
83
+ .and_return(response)
84
+ outcome = lambda do
85
+ described_class.run! dossier_id: dossier_id,
86
+ protocol_subscription_id: protocol_subscription_id,
87
+ path: path,
88
+ options: options
89
+ end
90
+ expect { outcome.call }.to raise_error(Errors::UnexpectedStatusError)
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+ require 'shared_examples_for_endpoints'
3
+ module Fizzy
4
+ module Api
5
+ module Endpoints
6
+ describe RenderOverview do
7
+ let(:dossier_id) { '123' }
8
+ let(:protocol_subscription_id) { 'abc' }
9
+ let(:response) { httparty_response('{}') }
10
+ let(:session) { FactoryGirl.build :basic_auth_session }
11
+
12
+ it_behaves_like 'an endpoint'
13
+
14
+ before do
15
+ allow(Sessions::BasicAuthSession).to receive(:new).and_return session
16
+ end
17
+
18
+ it 'should call the correct url' do
19
+ expect(session).to receive(:get)
20
+ .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render.json")
21
+ .and_return(response)
22
+ outcome = described_class.run! dossier_id: dossier_id,
23
+ protocol_subscription_id: protocol_subscription_id
24
+ expect(outcome).to eq '{}'
25
+ end
26
+
27
+ describe 'error handling' do
28
+ it 'should fail a 500 with an UnexpectedStatusError' do
29
+ allow(response).to receive(:code).and_return 500
30
+ expect(session).to receive(:get)
31
+ .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render.json")
32
+ .and_return(response)
33
+ outcome = lambda do
34
+ described_class.run! dossier_id: dossier_id,
35
+ protocol_subscription_id: protocol_subscription_id
36
+ end
37
+ expect { outcome.call }.to raise_error(Errors::UnexpectedStatusError)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,18 @@
1
+ module Fizzy
2
+ module Api
3
+ module Endpoints
4
+ shared_examples_for 'an endpoint' do
5
+ it 'should define a process response method' do
6
+ expect(subject.methods).to include(:process_response)
7
+ end
8
+ it 'should define an invalid response method' do
9
+ methods = subject.private_methods + subject.methods
10
+ expect(methods).to include(:invalid_response)
11
+ end
12
+ it 'should define a select_measurement_text method' do
13
+ expect(subject.methods).to include(:select_measurement_text)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fizzy-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frank Blaauw
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-05-04 00:00:00.000000000 Z
12
+ date: 2016-05-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -127,25 +127,31 @@ files:
127
127
  - fizzy_api.gemspec
128
128
  - lib/fizzy-api.rb
129
129
  - lib/fizzy/api.rb
130
- - lib/fizzy/api/calculate_outcome.rb
130
+ - lib/fizzy/api/endpoints.rb
131
+ - lib/fizzy/api/endpoints/calculate_outcome.rb
132
+ - lib/fizzy/api/endpoints/endpoint.rb
133
+ - lib/fizzy/api/endpoints/render_graph.rb
134
+ - lib/fizzy/api/endpoints/render_graph_synchronous.rb
135
+ - lib/fizzy/api/endpoints/render_overview.rb
131
136
  - lib/fizzy/api/errors.rb
132
137
  - lib/fizzy/api/errors/graph_not_found_error.rb
138
+ - lib/fizzy/api/errors/method_not_allowed_error.rb
133
139
  - lib/fizzy/api/errors/outcome_not_available_error.rb
134
140
  - lib/fizzy/api/errors/too_few_measurements_error.rb
135
141
  - lib/fizzy/api/errors/unexpected_status_error.rb
136
142
  - lib/fizzy/api/models.rb
137
- - lib/fizzy/api/render_graph.rb
138
- - lib/fizzy/api/render_overview.rb
139
143
  - lib/fizzy/api/sessions.rb
140
144
  - lib/fizzy/api/sessions/basic_auth_session.rb
141
145
  - lib/fizzy/api/version.rb
142
146
  - rubocop-todo.yml
143
147
  - spec/factories/basic_auth_session.rb
144
- - spec/fizzy/api/calculate_outcome_spec.rb
145
- - spec/fizzy/api/render_graph_spec.rb
146
- - spec/fizzy/api/render_overview_spec.rb
148
+ - spec/fizzy/api/endpoints/calculate_outcome_spec.rb
149
+ - spec/fizzy/api/endpoints/render_graph_spec.rb
150
+ - spec/fizzy/api/endpoints/render_graph_synchronous_spec.rb
151
+ - spec/fizzy/api/endpoints/render_overview_spec.rb
147
152
  - spec/fizzy/api/sessions/basic_auth_session_spec.rb
148
153
  - spec/fizzy/api/sessions_spec.rb
154
+ - spec/shared_examples_for_endpoints.rb
149
155
  - spec/spec_helper.rb
150
156
  - spec/support/httparty_helpers.rb
151
157
  homepage: http://github.com/roqua/fizzy_api
@@ -174,10 +180,12 @@ specification_version: 4
174
180
  summary: API wrapper gem around Fizzy's Graph API
175
181
  test_files:
176
182
  - spec/factories/basic_auth_session.rb
177
- - spec/fizzy/api/calculate_outcome_spec.rb
178
- - spec/fizzy/api/render_graph_spec.rb
179
- - spec/fizzy/api/render_overview_spec.rb
183
+ - spec/fizzy/api/endpoints/calculate_outcome_spec.rb
184
+ - spec/fizzy/api/endpoints/render_graph_spec.rb
185
+ - spec/fizzy/api/endpoints/render_graph_synchronous_spec.rb
186
+ - spec/fizzy/api/endpoints/render_overview_spec.rb
180
187
  - spec/fizzy/api/sessions/basic_auth_session_spec.rb
181
188
  - spec/fizzy/api/sessions_spec.rb
189
+ - spec/shared_examples_for_endpoints.rb
182
190
  - spec/spec_helper.rb
183
191
  - spec/support/httparty_helpers.rb
@@ -1,37 +0,0 @@
1
- module Fizzy
2
- module Api
3
- class CalculateOutcome < ActiveInteraction::Base
4
- string :dossier_id
5
- string :protocol_subscription_id
6
-
7
- def execute
8
- response = Api.basic_auth_session.post("/dossier/#{dossier_id}"\
9
- "/protocol_subscriptions/#{protocol_subscription_id}"\
10
- '/calculate')
11
- process_response(response)
12
- end
13
-
14
- private
15
-
16
- def process_response(response)
17
- case response.code
18
- when 200
19
- response
20
- else
21
- invalid_response(response)
22
- end
23
- end
24
-
25
- def invalid_response(response)
26
- case response.code
27
- when 202
28
- raise Errors::OutcomeNotAvailableError, response.body || 'The results are currently being calculated.'
29
- when 404
30
- raise Errors::GraphNotFoundError, response.body || 'Participant not found, or graph not supported.'
31
- else
32
- raise Errors::UnexpectedStatusError, "Status code #{response.code} not expected."
33
- end
34
- end
35
- end
36
- end
37
- end
@@ -1,48 +0,0 @@
1
- module Fizzy
2
- module Api
3
- class RenderGraph < ActiveInteraction::Base
4
- string :dossier_id
5
- string :protocol_subscription_id
6
- string :path
7
- hash :options, default: {}
8
-
9
- def execute
10
- response = Api.basic_auth_session.get("/dossier/#{dossier_id}"\
11
- "/protocol_subscriptions/#{protocol_subscription_id}" \
12
- "/render/#{path}", options)
13
- process_response(response)
14
- end
15
-
16
- private
17
-
18
- def process_response(response)
19
- case response.code
20
- when 200
21
- response
22
- else
23
- invalid_response(response)
24
- end
25
- end
26
-
27
- def invalid_response(response)
28
- case response.code
29
- when 202
30
- raise Errors::OutcomeNotAvailableError, select_measurement_text(response,
31
- 'The results have not yet been calculated.')
32
- when 204
33
- raise Errors::TooFewMeasurementsError, select_measurement_text(response,
34
- 'Not enough measurements available.')
35
- when 404
36
- raise Errors::GraphNotFoundError, select_measurement_text(response,
37
- 'Graph not found.')
38
- else
39
- raise Errors::UnexpectedStatusError, "Status code #{response.code} not expected."
40
- end
41
- end
42
-
43
- def select_measurement_text(response, default_message)
44
- response.body || default_message
45
- end
46
- end
47
- end
48
- end
@@ -1,26 +0,0 @@
1
- module Fizzy
2
- module Api
3
- class RenderOverview < ActiveInteraction::Base
4
- string :dossier_id
5
- string :protocol_subscription_id
6
-
7
- def execute
8
- response = Api.basic_auth_session.get("/dossier/#{dossier_id}"\
9
- "/protocol_subscriptions/#{protocol_subscription_id}" \
10
- '/render.json')
11
- process_response(response)
12
- end
13
-
14
- private
15
-
16
- def process_response(response)
17
- case response.code
18
- when 200
19
- response
20
- else
21
- raise Errors::UnexpectedStatusError, "Status code #{response.code} not expected."
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,51 +0,0 @@
1
- require 'spec_helper'
2
- module Fizzy
3
- module Api
4
- describe CalculateOutcome do
5
- let(:dossier_id) { '123' }
6
- let(:protocol_subscription_id) { 'abc' }
7
- let(:response) { httparty_response('') }
8
- let(:session) { FactoryGirl.build :basic_auth_session }
9
-
10
- before do
11
- allow(Sessions::BasicAuthSession).to receive(:new).and_return session
12
- allow(session).to receive(:post).and_return nil
13
- end
14
-
15
- it 'should call the correct url' do
16
- expect(session).to receive(:post)
17
- .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/calculate")
18
- .and_return(response)
19
-
20
- described_class.run! dossier_id: dossier_id,
21
- protocol_subscription_id: protocol_subscription_id
22
- end
23
-
24
- describe 'error handling' do
25
- it 'should notice a 404' do
26
- allow(response).to receive(:code).and_return 404
27
- expect(session).to receive(:post)
28
- .with('/dossier/fake/protocol_subscriptions/fake/calculate')
29
- .and_return(response)
30
- outcome = lambda do
31
- described_class.run! dossier_id: 'fake',
32
- protocol_subscription_id: 'fake'
33
- end
34
- expect { outcome.call }.to raise_error(Errors::GraphNotFoundError)
35
- end
36
-
37
- it 'should fail a 500 with an UnexpectedStatusError' do
38
- allow(response).to receive(:code).and_return 500
39
- expect(session).to receive(:post)
40
- .with('/dossier/fake/protocol_subscriptions/fake/calculate')
41
- .and_return(response)
42
- outcome = lambda do
43
- described_class.run! dossier_id: 'fake',
44
- protocol_subscription_id: 'fake'
45
- end
46
- expect { outcome.call }.to raise_error(Errors::UnexpectedStatusError)
47
- end
48
- end
49
- end
50
- end
51
- end
@@ -1,86 +0,0 @@
1
- require 'spec_helper'
2
- module Fizzy
3
- module Api
4
- describe RenderGraph do
5
- let(:dossier_id) { '123' }
6
- let(:protocol_subscription_id) { 'abc' }
7
- let(:path) { 'welbevinden.svg' }
8
- let(:options) { {} }
9
- let(:response) { httparty_response('<svg>') }
10
- let(:session) { FactoryGirl.build :basic_auth_session }
11
-
12
- before do
13
- allow(Sessions::BasicAuthSession).to receive(:new).and_return session
14
- end
15
-
16
- it 'should call the correct url' do
17
- expect(session).to receive(:get)
18
- .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render/#{path}", options)
19
- .and_return(response)
20
- outcome = described_class.run! dossier_id: dossier_id,
21
- protocol_subscription_id: protocol_subscription_id,
22
- path: path,
23
- options: options
24
- expect(outcome).to eq '<svg>'
25
- end
26
-
27
- describe 'error handling' do
28
- it 'should notice a 202' do
29
- allow(response).to receive(:code).and_return 202
30
- expect(session).to receive(:get)
31
- .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render/#{path}", options)
32
- .and_return(response)
33
- outcome = lambda do
34
- described_class.run! dossier_id: dossier_id,
35
- protocol_subscription_id: protocol_subscription_id,
36
- path: path,
37
- options: options
38
- end
39
- expect { outcome.call }.to raise_error(Errors::OutcomeNotAvailableError)
40
- end
41
-
42
- it 'should notice a 204' do
43
- allow(response).to receive(:code).and_return 204
44
- expect(session).to receive(:get)
45
- .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render/#{path}", options)
46
- .and_return(response)
47
- outcome = lambda do
48
- described_class.run! dossier_id: dossier_id,
49
- protocol_subscription_id: protocol_subscription_id,
50
- path: path,
51
- options: options
52
- end
53
- expect { outcome.call }.to raise_error(Errors::TooFewMeasurementsError)
54
- end
55
-
56
- it 'should notice a 404' do
57
- allow(response).to receive(:code).and_return 404
58
- expect(session).to receive(:get)
59
- .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render/#{path}", options)
60
- .and_return(response)
61
- outcome = lambda do
62
- described_class.run! dossier_id: dossier_id,
63
- protocol_subscription_id: protocol_subscription_id,
64
- path: path,
65
- options: options
66
- end
67
- expect { outcome.call }.to raise_error(Errors::GraphNotFoundError)
68
- end
69
-
70
- it 'should fail a 500 with an UnexpectedStatusError' do
71
- allow(response).to receive(:code).and_return 500
72
- expect(session).to receive(:get)
73
- .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render/#{path}", options)
74
- .and_return(response)
75
- outcome = lambda do
76
- described_class.run! dossier_id: dossier_id,
77
- protocol_subscription_id: protocol_subscription_id,
78
- path: path,
79
- options: options
80
- end
81
- expect { outcome.call }.to raise_error(Errors::UnexpectedStatusError)
82
- end
83
- end
84
- end
85
- end
86
- end
@@ -1,38 +0,0 @@
1
- require 'spec_helper'
2
- module Fizzy
3
- module Api
4
- describe RenderOverview do
5
- let(:dossier_id) { '123' }
6
- let(:protocol_subscription_id) { 'abc' }
7
- let(:response) { httparty_response('{}') }
8
- let(:session) { FactoryGirl.build :basic_auth_session }
9
-
10
- before do
11
- allow(Sessions::BasicAuthSession).to receive(:new).and_return session
12
- end
13
-
14
- it 'should call the correct url' do
15
- expect(session).to receive(:get)
16
- .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render.json")
17
- .and_return(response)
18
- outcome = described_class.run! dossier_id: dossier_id,
19
- protocol_subscription_id: protocol_subscription_id
20
- expect(outcome).to eq '{}'
21
- end
22
-
23
- describe 'error handling' do
24
- it 'should fail a 500 with an UnexpectedStatusError' do
25
- allow(response).to receive(:code).and_return 500
26
- expect(session).to receive(:get)
27
- .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render.json")
28
- .and_return(response)
29
- outcome = lambda do
30
- described_class.run! dossier_id: dossier_id,
31
- protocol_subscription_id: protocol_subscription_id
32
- end
33
- expect { outcome.call }.to raise_error(Errors::UnexpectedStatusError)
34
- end
35
- end
36
- end
37
- end
38
- end