clarify 1.1.1 → 2.0.0.alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -16
  3. data/.rspec +2 -0
  4. data/.simplecov +3 -0
  5. data/.travis.yml +7 -0
  6. data/CHANGELOG.md +4 -0
  7. data/Dockerfile +11 -0
  8. data/Gemfile +2 -2
  9. data/LICENSE +1 -1
  10. data/README.md +202 -47
  11. data/Rakefile +62 -4
  12. data/clarify.gemspec +10 -7
  13. data/cucumber.yml +1 -0
  14. data/features/create-bundles.feature +10 -0
  15. data/features/delete-bundle.feature +7 -0
  16. data/features/identity-steps.feature +6 -0
  17. data/features/list-bundles.feature +13 -0
  18. data/features/search-bundles.feature +10 -0
  19. data/features/step_definitions/curied_url_steps.rb +6 -0
  20. data/features/step_definitions/error_steps.rb +4 -0
  21. data/features/step_definitions/http_verification_step.rb +4 -0
  22. data/features/step_definitions/identity_steps.rb +12 -0
  23. data/features/step_definitions/list_steps.rb +76 -0
  24. data/features/support/env.rb +36 -0
  25. data/features/support/lib/curies.rb +19 -0
  26. data/features/support/lib/customer.rb +41 -0
  27. data/features/support/lib/exceptions.rb +27 -0
  28. data/features/support/lib/names.rb +16 -0
  29. data/lib/clarify.rb +16 -14
  30. data/lib/clarify/bundle_repository.rb +26 -0
  31. data/lib/clarify/client.rb +51 -0
  32. data/lib/clarify/collection_iterator.rb +27 -0
  33. data/lib/clarify/configuration.rb +29 -7
  34. data/lib/clarify/errors.rb +22 -0
  35. data/lib/clarify/response.rb +29 -0
  36. data/lib/clarify/response_factory.rb +34 -0
  37. data/lib/clarify/responses/bundle.rb +11 -0
  38. data/lib/clarify/responses/collection.rb +31 -0
  39. data/lib/clarify/responses/no_body.rb +13 -0
  40. data/lib/clarify/responses/search_collection.rb +18 -0
  41. data/lib/clarify/responses/tracks.rb +15 -0
  42. data/lib/clarify/rest_client.rb +129 -0
  43. data/lib/clarify/version.rb +3 -1
  44. data/spec/clarify/bundle_repository_spec.rb +37 -0
  45. data/spec/clarify/client_spec.rb +93 -0
  46. data/spec/clarify/collection_iterator_spec.rb +86 -0
  47. data/spec/clarify/configuration_spec.rb +77 -0
  48. data/spec/clarify/errors_spec.rb +15 -0
  49. data/spec/clarify/response_factory_spec.rb +51 -0
  50. data/spec/clarify/response_spec.rb +69 -0
  51. data/spec/clarify/responses/bundle_spec.rb +8 -0
  52. data/spec/clarify/responses/collection_spec.rb +58 -0
  53. data/spec/clarify/responses/search_collection_spec.rb +40 -0
  54. data/spec/clarify/responses/tracks_spec.rb +18 -0
  55. data/spec/clarify/rest_client_spec.rb +222 -0
  56. data/spec/spec_helper.rb +4 -9
  57. data/src_readme/README_no_output.md +186 -0
  58. data/src_readme/examples/bundle_create.rb +11 -0
  59. data/src_readme/examples/bundle_fetch.rb +9 -0
  60. data/src_readme/examples/bundles_list_fetch.rb +11 -0
  61. data/src_readme/examples/bundles_paged_over.rb +8 -0
  62. data/src_readme/examples/bundles_search.rb +20 -0
  63. data/src_readme/examples/list_bundles.rb +6 -0
  64. data/src_readme/examples/searches_paged_over.rb +10 -0
  65. data/src_readme/examples/setup.rb +5 -0
  66. data/src_readme/make.rb +56 -0
  67. data/src_readme/readme.md.erb +55 -0
  68. metadata +127 -62
  69. data/LICENSE.txt +0 -24
  70. data/examples/create.rb +0 -14
  71. data/examples/delete.rb +0 -12
  72. data/examples/list.rb +0 -14
  73. data/examples/search.rb +0 -26
  74. data/examples/test.rb +0 -15
  75. data/lib/clarify/bundle.rb +0 -40
  76. data/lib/clarify/metadata.rb +0 -26
  77. data/lib/clarify/request.rb +0 -37
  78. data/lib/clarify/search.rb +0 -10
  79. data/lib/clarify/track.rb +0 -40
  80. data/spec/lib/clarify/bundle_spec.rb +0 -43
  81. data/spec/lib/clarify/configuration_spec.rb +0 -19
  82. data/spec/lib/clarify/metadata_spec.rb +0 -36
  83. data/spec/lib/clarify/search_spec.rb +0 -22
  84. data/spec/lib/clarify/track_spec.rb +0 -81
@@ -0,0 +1,86 @@
1
+
2
+ describe Clarify::CollectionIterator do
3
+ describe '#collections' do
4
+ let(:coll1) { double(:collection1, next: 2, more?: true) }
5
+ let(:coll2) { double(:collection2, next: 3, more?: true) }
6
+ let(:coll3) { double(:collection2, more?: false) }
7
+
8
+ let(:restclient) do
9
+ restclient = double(:restclient)
10
+ allow(restclient).to receive(:get).with(any_args)
11
+ .and_return(coll1, coll2, coll3)
12
+
13
+ restclient
14
+ end
15
+ let(:iterator) do
16
+ Clarify::CollectionIterator.new(restclient, restclient.get(1))
17
+ end
18
+
19
+ it 'iterates over all three collections' do
20
+ expect(iterator.collections.to_a).to eq([coll1, coll2, coll3])
21
+ end
22
+ end
23
+
24
+ describe '#each' do
25
+ let(:coll1) do
26
+ c = double(:collection1, next: 2, more?: true)
27
+ allow(c).to receive(:each).once.and_yield(1).and_yield(2).and_yield(3)
28
+ c
29
+ end
30
+ let(:coll2) do
31
+ c = double(:collection2, next: 3, more?: true)
32
+ allow(c).to receive(:each).once.and_yield(4).and_yield(5).and_yield(6)
33
+ c
34
+ end
35
+ let(:coll3) do
36
+ c = double(:collection3, more?: false)
37
+ allow(c).to receive(:each).once.and_yield(7).and_yield(8).and_yield(9)
38
+ c
39
+ end
40
+
41
+ let(:restclient) do
42
+ restclient = double(:restclient)
43
+ allow(restclient).to receive(:get).with(any_args)
44
+ .and_return(coll1, coll2, coll3)
45
+
46
+ restclient
47
+ end
48
+ let(:iterator) do
49
+ Clarify::CollectionIterator.new(restclient, restclient.get(1))
50
+ end
51
+
52
+ it 'iterates over all the elements inside each collection' do
53
+ expect(iterator.to_a).to eq([1, 2, 3, 4, 5, 6, 7, 8, 9])
54
+ end
55
+ end
56
+
57
+ context 'with search results which yield two variables' do
58
+ describe '#each' do
59
+ let(:coll1) do
60
+ c = double(:collection1, next: 2, more?: true)
61
+ allow(c).to receive(:each).once.and_yield(1, 2).and_yield(3, 4)
62
+ c
63
+ end
64
+ let(:coll2) do
65
+ c = double(:collection2, more?: false)
66
+ allow(c).to receive(:each).once.and_yield(5, 6).and_yield(7, 8)
67
+ c
68
+ end
69
+
70
+ let(:restclient) do
71
+ restclient = double(:restclient)
72
+ allow(restclient).to receive(:get).with(any_args)
73
+ .and_return(coll1, coll2)
74
+
75
+ restclient
76
+ end
77
+ let(:iterator) do
78
+ Clarify::CollectionIterator.new(restclient, restclient.get(1))
79
+ end
80
+
81
+ it 'iterates over all the elements inside each collection' do
82
+ expect(iterator.to_a).to eq([[1, 2], [3, 4], [5, 6], [7, 8]])
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,77 @@
1
+
2
+ describe Clarify::Configuration do
3
+ let(:api_key) { double(:api_key) }
4
+ let(:server) { 'https://example.com/' }
5
+ let(:configuration) do
6
+ Clarify::Configuration.new(api_key: api_key, server: server)
7
+ end
8
+
9
+ describe '#api_key?' do
10
+ subject { configuration.api_key? }
11
+ context 'when the api key is nil' do
12
+ let(:api_key) { nil }
13
+ it { is_expected.to eq(false) }
14
+ end
15
+
16
+ context 'when the api key is empty' do
17
+ let(:api_key) { '' }
18
+ it { is_expected.to eq(false) }
19
+ end
20
+
21
+ context 'when the api key is correctly entered' do
22
+ let(:api_key) { 'abc123' }
23
+ it { is_expected.to eq(true) }
24
+ end
25
+ end
26
+
27
+ describe '#ssl?' do
28
+ subject { configuration.ssl? }
29
+ context 'the URL has https specified' do
30
+ let(:server) { 'https://example.com' }
31
+ it { is_expected.to eq(true) }
32
+ end
33
+
34
+ context 'the URL has http specified' do
35
+ let(:server) { 'http://example.com' }
36
+ it { is_expected.to eq(false) }
37
+ end
38
+
39
+ context 'the URL has nothing specified' do
40
+ let(:server) { 'example.com' }
41
+ it { is_expected.to eq(false) }
42
+ end
43
+ end
44
+
45
+ describe '#host' do
46
+ subject { configuration.host }
47
+ it { is_expected.to eq('example.com') }
48
+ end
49
+
50
+ describe '#port' do
51
+ subject { configuration.port }
52
+
53
+ context 'with https' do
54
+ context 'with a custom port' do
55
+ let(:server) { 'https://example.com:444' }
56
+ it { is_expected.to eq(444) }
57
+ end
58
+
59
+ context 'without a custom port' do
60
+ let(:server) { 'https://example.com' }
61
+ it { is_expected.to eq(443) }
62
+ end
63
+ end
64
+
65
+ context 'with http' do
66
+ context 'with a custom port' do
67
+ let(:server) { 'http://example.com:8080' }
68
+ it { is_expected.to eq(8080) }
69
+ end
70
+
71
+ context 'without a custom port' do
72
+ let(:server) { 'http://example.com' }
73
+ it { is_expected.to eq(80) }
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,15 @@
1
+
2
+ describe Clarify::UnrecognizedResponseError do
3
+ let(:response) { 'SomeFutureDataType' }
4
+ let(:error) do
5
+ begin
6
+ fail Clarify::UnrecognizedResponseError, response
7
+ rescue Clarify::UnrecognizedResponseError => e
8
+ e
9
+ end
10
+ end
11
+
12
+ it 'allows access to the failing response' do
13
+ expect(error.to_s).to eq 'Unrecognized response class SomeFutureDataType'
14
+ end
15
+ end
@@ -0,0 +1,51 @@
1
+
2
+ describe Clarify::ResponseFactory do
3
+ let(:factory) { Clarify::ResponseFactory.new }
4
+
5
+ describe '#make_result' do
6
+ context 'the server sent a Collection type' do
7
+ it 'returns a Clarify::Responses::Collection' do
8
+ response = double(:response,
9
+ body: '{"_class": "Collection"}',
10
+ code: '200'
11
+ )
12
+
13
+ result = factory.make_result(response)
14
+ expect(result).to be_a(Clarify::Responses::Collection)
15
+ end
16
+ end
17
+
18
+ context 'the server sent an unrecognized type' do
19
+ it 'raises an UnrecognizedResponseError' do
20
+ response = double(:response, body: '{"_class": "FooBarClass"}')
21
+ allow(factory).to receive(:raise_on_code!)
22
+
23
+ expect { factory.make_result(response) }.to raise_error(
24
+ Clarify::UnrecognizedResponseError,
25
+ 'Unrecognized response class FooBarClass')
26
+ end
27
+ end
28
+
29
+ context 'the response has no body' do
30
+ let(:response) do
31
+ double(:response, code: 204, body: nil)
32
+ end
33
+ subject { factory.make_result(response) }
34
+ it { is_expected.to be_a(Clarify::Responses::NoBody) }
35
+ end
36
+ end
37
+
38
+ describe '#raise_on_code!' do
39
+ it 'does nothing on a 200' do
40
+ response = double(:response, code: '200')
41
+ expect(factory.raise_on_code!(response)).to eq(nil)
42
+ end
43
+
44
+ it 'raises UnauthenticatedError on 401' do
45
+ response = double(:response, code: '401')
46
+ expect { factory.raise_on_code!(response) }.to raise_error(
47
+ Clarify::UnauthenticatedError
48
+ )
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,69 @@
1
+
2
+ describe Clarify::Response do
3
+ describe '#http_status_code' do
4
+ it 'returns the int version of the response status code' do
5
+ response = Clarify::Response.new(nil, double(:response, code: '201'))
6
+
7
+ expect(response.http_status_code).to eq(201)
8
+ end
9
+ end
10
+
11
+ describe '#relation!' do
12
+ context 'with a present relation' do
13
+ let(:body) { { '_links' => { 'self' => { 'href' => 'my-url' } } } }
14
+ it 'returns the value from the relation' do
15
+ expect(Clarify::Response.new(body, nil).relation!('self'))
16
+ .to eq('my-url')
17
+ end
18
+ end
19
+
20
+ context 'without a relation present' do
21
+ let(:body) { {} }
22
+ it 'raises an error' do
23
+ expect do
24
+ Clarify::Response.new(body, nil).relation!('self')
25
+ end.to raise_error ArgumentError
26
+ end
27
+ end
28
+ end
29
+
30
+ describe '#relation' do
31
+ it 'returns a href for a single value relation' do
32
+ body = {
33
+ '_links' => {
34
+ 'test_value' => {
35
+ 'href' => 'http://example.com'
36
+ }
37
+ }
38
+ }
39
+ response = Clarify::Response.new(body, nil)
40
+
41
+ expect(response.relation('test_value')).to eq('http://example.com')
42
+ end
43
+
44
+ context 'with a single element' do
45
+ subject do
46
+ Clarify::Responses::Collection.new(track_data, nil).relation('next')
47
+ end
48
+
49
+ context 'with a next url' do
50
+ let(:track_data) do
51
+ { '_links' => { 'next' => { 'href' => 'my-url' } } }
52
+ end
53
+ it { is_expected.to eq('my-url') }
54
+ end
55
+
56
+ context 'with no next url' do
57
+ let(:track_data) do
58
+ { '_links' => { 'prev' => { 'href' => 'my-url' } } }
59
+ end
60
+ it { is_expected.to eq(nil) }
61
+ end
62
+
63
+ context 'with no _links at all' do
64
+ let(:track_data) { {} }
65
+ it { is_expected.to eq(nil) }
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,8 @@
1
+
2
+ describe Clarify::Responses::Bundle do
3
+ describe '#name' do
4
+ let(:body) { { 'name' => 'foobar' } }
5
+ subject { Clarify::Responses::Bundle.new(body, nil).name }
6
+ it { is_expected.to eq('foobar') }
7
+ end
8
+ end
@@ -0,0 +1,58 @@
1
+
2
+ describe Clarify::Responses::Collection do
3
+ it 'iterates over each bundle' do
4
+ track_data = {
5
+ '_links' => {
6
+ 'items' => [
7
+ {
8
+ 'href' => 'a'
9
+ },
10
+ {
11
+ 'href' => 'b'
12
+ }
13
+ ]
14
+ }
15
+ }
16
+ response = Clarify::Responses::Collection.new(track_data, nil)
17
+ expect { |b| response.each(&b) }.to yield_control.twice
18
+ expect(response.to_a).to eq %w(a b)
19
+ end
20
+
21
+ describe '#next' do
22
+ subject { Clarify::Responses::Collection.new(track_data, nil).next }
23
+
24
+ context 'with a next url' do
25
+ let(:track_data) { { '_links' => { 'next' => { 'href' => 'my-url' } } } }
26
+ it { is_expected.to eq('my-url') }
27
+ end
28
+
29
+ context 'with no next url' do
30
+ let(:track_data) { { '_links' => { 'prev' => { 'href' => 'my-url' } } } }
31
+ it { is_expected.to eq(nil) }
32
+ end
33
+
34
+ context 'with no _links at all' do
35
+ let(:track_data) { {} }
36
+ it { is_expected.to eq(nil) }
37
+ end
38
+ end
39
+
40
+ describe '#more?' do
41
+ subject { Clarify::Responses::Collection.new(track_data, nil).more? }
42
+
43
+ context 'with a next url' do
44
+ let(:track_data) { { '_links' => { 'next' => { 'href' => 'my-url' } } } }
45
+ it { is_expected.to eq(true) }
46
+ end
47
+
48
+ context 'with no next url' do
49
+ let(:track_data) { { '_links' => { 'prev' => { 'href' => 'my-url' } } } }
50
+ it { is_expected.to eq(false) }
51
+ end
52
+
53
+ context 'with no _links at all' do
54
+ let(:track_data) { {} }
55
+ it { is_expected.to eq(false) }
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,40 @@
1
+
2
+ describe Clarify::Responses::SearchCollection do
3
+ let(:response) { double(:response) }
4
+ let(:body) do
5
+ {
6
+ 'item_results' => [
7
+ { 'name' => '1_result' },
8
+ { 'name' => '2_result' }
9
+ ],
10
+ '_links' => {
11
+ 'items' => [
12
+ { 'href' => '1_link' },
13
+ { 'href' => '2_link' }
14
+ ]
15
+ }
16
+ }
17
+ end
18
+ let(:search) { Clarify::Responses::SearchCollection.new(body, response) }
19
+
20
+ describe '#items' do
21
+ it 'returns an array of links and results' do
22
+ expect(search.items).to eq([
23
+ [{ 'name' => '1_result' }, { 'href' => '1_link' }],
24
+ [{ 'name' => '2_result' }, { 'href' => '2_link' }]
25
+ ])
26
+ end
27
+ end
28
+
29
+ describe '#each' do
30
+ it 'yields per result' do
31
+ expect { |b| search.each(&b) }.to yield_control.twice
32
+ end
33
+ it 'yields object, ul' do
34
+ expect(search.to_a).to eq([
35
+ [{ 'name' => '1_result' }, '1_link'],
36
+ [{ 'name' => '2_result' }, '2_link']
37
+ ])
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,18 @@
1
+
2
+ describe Clarify::Responses::Tracks do
3
+ it 'iterates over each track' do
4
+ track_data = {
5
+ 'tracks' => [
6
+ {
7
+ 'media_url' => 'a'
8
+ },
9
+ {
10
+ 'media_url' => 'b'
11
+ }
12
+ ]
13
+ }
14
+ response = Clarify::Responses::Tracks.new(track_data, nil)
15
+ expect { |b| response.each(&b) }.to yield_control.twice
16
+ expect(response.map { |t| t['media_url'] }).to eq %w(a b)
17
+ end
18
+ end
@@ -0,0 +1,222 @@
1
+
2
+ describe Clarify::RestClient do
3
+ let(:config) { double(:configuration) }
4
+ let(:restclient) { Clarify::RestClient.new(config) }
5
+
6
+ describe '#get' do
7
+ it 'creates a get_request and passes it to request' do
8
+ request = double(:request)
9
+ response = double(:response)
10
+
11
+ expect(restclient).to receive(:get_request)
12
+ .with('/hi', {}).and_return(request)
13
+ expect(restclient).to receive(:request)
14
+ .with(request).and_return(response)
15
+ expect(restclient.get('/hi')).to eq(response)
16
+ end
17
+ end
18
+
19
+ describe '#get_request' do
20
+ subject { restclient.get_request('/bundle/abc123', a: :b) }
21
+ it 'creates a Get request' do
22
+ expect(restclient).to receive(:make_get_url)
23
+ .with('/bundle/abc123', a: :b).and_return('/bundle/abc123?a=b')
24
+
25
+ expect(subject).to be_a(Net::HTTP::Get)
26
+ expect(subject.path).to eq('/bundle/abc123?a=b')
27
+ end
28
+ end
29
+
30
+ describe '#make_get_url' do
31
+ subject { restclient.make_get_url(url, params) }
32
+ context 'with no URL parameters on the input url' do
33
+ let(:url) { '/bundle/abc123' }
34
+
35
+ context 'with no query parameters' do
36
+ let(:params) { {} }
37
+
38
+ it { is_expected.to eq('/bundle/abc123') }
39
+ end
40
+
41
+ context 'with a few query parameters' do
42
+ let(:params) { { a: :b } }
43
+
44
+ it { is_expected.to eq('/bundle/abc123?a=b') }
45
+ end
46
+ end
47
+
48
+ context 'with URL parameters on the input url' do
49
+ let(:url) { '/bundle/abc123?a=b' }
50
+
51
+ context 'with no query parameters' do
52
+ let(:params) { {} }
53
+
54
+ it { is_expected.to eq('/bundle/abc123?a=b') }
55
+ end
56
+
57
+ context 'with a few query parameters' do
58
+ let(:params) { { c: :d } }
59
+
60
+ it { is_expected.to eq('/bundle/abc123?a=b&c=d') }
61
+ end
62
+ end
63
+ end
64
+
65
+ describe '#post' do
66
+ it 'creates a post_request and passes it to request' do
67
+ request = double(:request)
68
+ response = double(:response)
69
+
70
+ expect(restclient).to receive(:post_request)
71
+ .with('/hi', {}).and_return(request)
72
+ expect(restclient).to receive(:request)
73
+ .with(request).and_return(response)
74
+ expect(restclient.post('/hi')).to eq(response)
75
+ end
76
+ end
77
+
78
+ describe '#post_request' do
79
+ let(:url) { '/bundle/123' }
80
+ subject { restclient.post_request(url) }
81
+ it { is_expected.to be_a(Net::HTTP::Post) }
82
+
83
+ context 'with a body' do
84
+ let(:body) { { 'hi' => 'there' } }
85
+ subject { restclient.post_request(url, body).body }
86
+ it { is_expected.to eq('hi=there') }
87
+ end
88
+ end
89
+
90
+ describe '#put' do
91
+ it 'creates a put_request and passes it to request' do
92
+ request = double(:request)
93
+ response = double(:response)
94
+
95
+ expect(restclient).to receive(:put_request)
96
+ .with('/hi', {}).and_return(request)
97
+ expect(restclient).to receive(:request)
98
+ .with(request).and_return(response)
99
+ expect(restclient.put('/hi')).to eq(response)
100
+ end
101
+ end
102
+
103
+ describe '#put_request' do
104
+ let(:url) { '/bundle/123' }
105
+ subject { restclient.put_request(url) }
106
+ it { is_expected.to be_a(Net::HTTP::Put) }
107
+
108
+ context 'with a body' do
109
+ let(:body) { { 'hi' => 'there' } }
110
+ subject { restclient.put_request(url, body).body }
111
+ it { is_expected.to eq('hi=there') }
112
+ end
113
+ end
114
+
115
+ describe '#delete' do
116
+ it 'creates a delete_request and passes it to request' do
117
+ request = double(:request)
118
+ response = double(:response)
119
+
120
+ expect(restclient).to receive(:delete_request)
121
+ .with('/hi', {}).and_return(request)
122
+ expect(restclient).to receive(:request)
123
+ .with(request).and_return(response)
124
+ expect(restclient.delete('/hi')).to eq(response)
125
+ end
126
+ end
127
+
128
+ describe '#delete_request' do
129
+ let(:url) { '/bundle/123' }
130
+ subject { restclient.delete_request(url) }
131
+ it { is_expected.to be_a(Net::HTTP::Delete) }
132
+ end
133
+
134
+ describe '#request' do
135
+ let(:connection) { double(:connection) }
136
+ before(:each) do
137
+ expect(restclient).to receive(:connection).and_return(connection)
138
+ end
139
+
140
+ it 'pipelines data through blessing, then execution, then result' do
141
+ in_req = double(:request)
142
+
143
+ blessed = double(:blessed)
144
+ expect(restclient).to receive(:bless_request)
145
+ .with(in_req).and_return(blessed)
146
+
147
+ raw_result = double(:raw_result)
148
+ expect(connection).to receive(:request).with(blessed)
149
+ .and_return(raw_result)
150
+
151
+ out_result = double(:result)
152
+ expect(restclient).to receive(:make_result).and_return(out_result)
153
+
154
+ expect(restclient.request(in_req)).to eq(out_result)
155
+ end
156
+ end
157
+
158
+ describe '#bless_request' do
159
+ subject { restclient.bless_request({}) }
160
+ before(:each) { allow(restclient).to receive(:user_agent).and_return 'UA' }
161
+
162
+ context 'with no API key' do
163
+ let(:config) { double(:config, api_key?: false) }
164
+ it { is_expected.to eq('User-Agent' => 'UA') }
165
+ end
166
+
167
+ context 'with an API key' do
168
+ let(:config) { double(:config, api_key?: true, api_key: 'abc123') }
169
+ it do
170
+ headers = {
171
+ 'User-Agent' => 'UA',
172
+ 'Authorization' => 'Bearer abc123'
173
+ }
174
+ is_expected.to eq(headers)
175
+ end
176
+ end
177
+ end
178
+
179
+ describe '#user_agent' do
180
+ subject { restclient.user_agent }
181
+ it do
182
+ ruby_id = "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
183
+
184
+ expect(subject).to eq("clarify-ruby/#{Clarify::VERSION}/#{ruby_id}")
185
+ end
186
+ end
187
+
188
+ describe '#make_result' do
189
+ it 'passes the response to the factory' do
190
+ raw_response = double(:raw_response)
191
+ response = double(:response)
192
+ factory = double(:factory)
193
+ expect(factory).to receive(:make_result).with(raw_response)
194
+ .and_return(response)
195
+ expect(restclient).to receive(:response_factory).and_return(factory)
196
+
197
+ expect(restclient.make_result(raw_response)).to eq(response)
198
+ end
199
+ end
200
+
201
+ describe '#response_factory' do
202
+ it 'returns a ResponseFactory' do
203
+ expect(restclient.response_factory).to be_a(Clarify::ResponseFactory)
204
+ end
205
+ end
206
+
207
+ describe '#connection' do
208
+ let(:config) { double(:config, host: 'abc', port: 123, ssl?: true) }
209
+ it 'uses configuration options to generate the connection' do
210
+ expect(restclient.connection.address).to eq('abc')
211
+ expect(restclient.connection.port).to eq(123)
212
+ expect(restclient.connection.use_ssl?).to eq(true)
213
+ end
214
+
215
+ context 'without ssl' do
216
+ let(:config) { double(:config, host: 'abc', port: 123, ssl?: false) }
217
+ it 'uses configuration options to generate the connection' do
218
+ expect(restclient.connection.use_ssl?).to eq(false)
219
+ end
220
+ end
221
+ end
222
+ end