kosapi_client 0.5.0 → 0.6.0
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.
- checksums.yaml +4 -4
- data/LICENSE +22 -0
- data/README.md +89 -0
- data/kosapi_client.gemspec +38 -0
- data/lib/kosapi_client/api_client.rb +1 -0
- data/lib/kosapi_client/entity/division.rb +13 -0
- data/lib/kosapi_client/entity/exam.rb +3 -1
- data/lib/kosapi_client/entity/id.rb +0 -1
- data/lib/kosapi_client/entity/parallel.rb +0 -1
- data/lib/kosapi_client/entity.rb +1 -0
- data/lib/kosapi_client/oauth2_http_adapter.rb +9 -4
- data/lib/kosapi_client/request_builder.rb +0 -1
- data/lib/kosapi_client/resource/course_events_builder.rb +1 -1
- data/lib/kosapi_client/resource/courses_builder.rb +7 -0
- data/lib/kosapi_client/resource/exams_builder.rb +1 -1
- data/lib/kosapi_client/resource/parallels_builder.rb +0 -1
- data/lib/kosapi_client/version.rb +1 -1
- metadata +25 -99
- data/bin/console +0 -15
- data/bin/setup +0 -23
- data/spec/integration/course_events_spec.rb +0 -13
- data/spec/integration/courses_spec.rb +0 -28
- data/spec/integration/exams_spec.rb +0 -20
- data/spec/integration/parallels_spec.rb +0 -68
- data/spec/kosapi_client/api_client_spec.rb +0 -22
- data/spec/kosapi_client/configuration_spec.rb +0 -30
- data/spec/kosapi_client/entity/base_entity_spec.rb +0 -20
- data/spec/kosapi_client/entity/base_person_spec.rb +0 -19
- data/spec/kosapi_client/entity/boolean_spec.rb +0 -23
- data/spec/kosapi_client/entity/course_event_spec.rb +0 -30
- data/spec/kosapi_client/entity/data_mappings_spec.rb +0 -154
- data/spec/kosapi_client/entity/enum_spec.rb +0 -20
- data/spec/kosapi_client/entity/id_spec.rb +0 -13
- data/spec/kosapi_client/entity/link_spec.rb +0 -89
- data/spec/kosapi_client/entity/ml_string_spec.rb +0 -52
- data/spec/kosapi_client/entity/parallel_spec.rb +0 -18
- data/spec/kosapi_client/entity/result_page_spec.rb +0 -42
- data/spec/kosapi_client/entity/teacher_timetable_slot_spec.rb +0 -19
- data/spec/kosapi_client/entity/timetable_slot_spec.rb +0 -22
- data/spec/kosapi_client/hash_utils_spec.rb +0 -20
- data/spec/kosapi_client/http_client_spec.rb +0 -30
- data/spec/kosapi_client/kosapi_client_spec.rb +0 -57
- data/spec/kosapi_client/kosapi_response_spec.rb +0 -96
- data/spec/kosapi_client/oauth2_http_adapter_spec.rb +0 -25
- data/spec/kosapi_client/request_builder_delegator_spec.rb +0 -72
- data/spec/kosapi_client/request_builder_spec.rb +0 -80
- data/spec/kosapi_client/resource/courses_builder_spec.rb +0 -20
- data/spec/kosapi_client/resource/parallels_builder_spec.rb +0 -49
- data/spec/kosapi_client/resource_mapper_spec.rb +0 -33
- data/spec/kosapi_client/response_converter_spec.rb +0 -58
- data/spec/kosapi_client/response_links_spec.rb +0 -52
- data/spec/kosapi_client/response_preprocessor_spec.rb +0 -51
- data/spec/kosapi_client/url_builder_spec.rb +0 -44
- data/spec/spec_helper.rb +0 -40
- data/spec/support/client_helpers.rb +0 -11
- data/spec/support/helpers.rb +0 -7
- data/spec/support/shared_examples_for_fluent_api.rb +0 -7
@@ -1,30 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe KOSapiClient::HTTPClient do
|
4
|
-
|
5
|
-
let(:response) { double(parsed: {}) }
|
6
|
-
let(:http_adapter) { instance_double(KOSapiClient::OAuth2HttpAdapter) }
|
7
|
-
let(:preprocessed_hash) { {} }
|
8
|
-
let(:preprocessor) { instance_double(KOSapiClient::ResponsePreprocessor, preprocess: preprocessed_hash) }
|
9
|
-
let(:converter) { instance_double(KOSapiClient::ResponseConverter, convert: nil) }
|
10
|
-
subject(:client) { KOSapiClient::HTTPClient.new(http_adapter, preprocessor, converter) }
|
11
|
-
|
12
|
-
describe '#send_request' do
|
13
|
-
|
14
|
-
it 'calls http adapter' do
|
15
|
-
expect(http_adapter).to receive(:send_request).and_return(response)
|
16
|
-
client.send_request(:get, 'http://example.com')
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
describe '#process_response' do
|
22
|
-
|
23
|
-
it 'converts items with converter' do
|
24
|
-
expect(converter).to receive(:convert).and_return(:foo)
|
25
|
-
expect(client.process_response(nil)).to eq(:foo)
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe KOSapiClient do
|
4
|
-
|
5
|
-
before do
|
6
|
-
KOSapiClient.configure do |c|
|
7
|
-
c.client_id = 'foo'
|
8
|
-
c.client_secret = 'bar'
|
9
|
-
end
|
10
|
-
@client = KOSapiClient.client
|
11
|
-
end
|
12
|
-
|
13
|
-
describe '.configure' do
|
14
|
-
|
15
|
-
it 'can be configured via block' do
|
16
|
-
expect(@client).not_to be_nil
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
describe '.reset' do
|
22
|
-
|
23
|
-
it 'cleans stored ApiClient instance' do
|
24
|
-
KOSapiClient.reset
|
25
|
-
expect(KOSapiClient.client).not_to be @client
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
describe '.client' do
|
31
|
-
|
32
|
-
it 'provides client instance via reader' do
|
33
|
-
expect(KOSapiClient.client).to eq @client
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'delegates missing methods to stored client instance' do
|
39
|
-
expect(@client).to receive(:course_events)
|
40
|
-
KOSapiClient.course_events
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'handles missing methods not defined on client' do
|
44
|
-
expect { KOSapiClient.foo }.to raise_error NoMethodError
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'throws error when sending request with no configured credentials' do
|
48
|
-
KOSapiClient.reset
|
49
|
-
expect { KOSapiClient.course_events.items }.to raise_error(RuntimeError)
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'responds to client methods' do
|
53
|
-
allow(@client).to receive(:foo)
|
54
|
-
expect(KOSapiClient).to respond_to(:foo)
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
@@ -1,96 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe KOSapiClient::KOSapiResponse do
|
4
|
-
|
5
|
-
subject(:response) { KOSapiClient::KOSapiResponse.new(result) }
|
6
|
-
let(:client) { instance_double(KOSapiClient::HTTPClient) }
|
7
|
-
|
8
|
-
context 'with paginated response' do
|
9
|
-
let(:result) { {atom_feed: {atom_entry: [:entry1, :entry2], atom_link: [{rel: 'prev', href: 'courses/?offset=0&limit=10'}, {rel: 'next', href: 'courses/?offset=20&limit=10'}]}} }
|
10
|
-
|
11
|
-
describe '#is_paginated?' do
|
12
|
-
|
13
|
-
it 'returns true when result contains atom feed' do
|
14
|
-
expect(response.is_paginated?).to be
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
describe '#items' do
|
20
|
-
|
21
|
-
it 'returns all entries' do
|
22
|
-
expect(response.items).to eq [:entry1, :entry2]
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
describe '#item' do
|
28
|
-
|
29
|
-
it 'returns first entry' do
|
30
|
-
expect(response.item).to eq :entry1
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
describe '#links_hash' do
|
36
|
-
|
37
|
-
it 'extracts links from feed' do
|
38
|
-
expect(response.links_hash).to eq [{rel: 'prev', href: 'courses/?offset=0&limit=10'}, {rel: 'next', href: 'courses/?offset=20&limit=10'}]
|
39
|
-
end
|
40
|
-
|
41
|
-
|
42
|
-
context 'with single link' do
|
43
|
-
let(:result) { {atom_feed: {atom_link: {rel: 'next', href: 'courses/?offset=10&limit=10'}}} }
|
44
|
-
|
45
|
-
it 'extracts next link from feed' do
|
46
|
-
expect(response.links_hash).to eq({rel: 'next', href: 'courses/?offset=10&limit=10'})
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
context 'with no links' do
|
51
|
-
let(:result) { {atom_feed: {}} }
|
52
|
-
|
53
|
-
it 'returns null' do
|
54
|
-
expect(response.links_hash).not_to be
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
context 'with not-paginated response' do
|
63
|
-
let(:result) { { atom_entry: :entry} }
|
64
|
-
|
65
|
-
describe '#is_paginated?' do
|
66
|
-
|
67
|
-
it 'returns false when result contains atom feed' do
|
68
|
-
expect(response.is_paginated?).not_to be
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
describe '#items' do
|
74
|
-
|
75
|
-
it 'returns single entry wrapped in array' do
|
76
|
-
expect(response.items).to eq [:entry]
|
77
|
-
end
|
78
|
-
|
79
|
-
end
|
80
|
-
|
81
|
-
describe '#item' do
|
82
|
-
|
83
|
-
it 'returns first entry' do
|
84
|
-
expect(response.item).to eq :entry
|
85
|
-
end
|
86
|
-
|
87
|
-
end
|
88
|
-
|
89
|
-
describe '#links_hash' do
|
90
|
-
it 'returns null' do
|
91
|
-
expect(response.links_hash).not_to be
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe KOSapiClient::OAuth2HttpAdapter, :vcr do
|
4
|
-
KOSAPI_ROOT_URL = 'https://kosapi.fit.cvut.cz/api/3/'
|
5
|
-
subject(:client) { KOSapiClient::OAuth2HttpAdapter.new(credentials, KOSAPI_ROOT_URL) }
|
6
|
-
|
7
|
-
|
8
|
-
context 'with invalid OAUTH credentials' do
|
9
|
-
let(:credentials) { { client_id: 'invalid_client_id', client_secret: 'invalid_secret' } }
|
10
|
-
|
11
|
-
it 'throws authentication error when fetching resource' do
|
12
|
-
expect { client.send_request(:get, 'courses') }.to raise_error(OAuth2::Error)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
context 'with valid OAUTH credentials' do
|
17
|
-
let(:credentials) { { client_id: ENV['KOSAPI_OAUTH_CLIENT_ID'], client_secret: ENV['KOSAPI_OAUTH_CLIENT_SECRET'] } }
|
18
|
-
|
19
|
-
it 'fetches response from a REST resource' do
|
20
|
-
response = client.send_request(:get, 'courses')
|
21
|
-
expect(response).not_to be_nil
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
end
|
@@ -1,72 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe KOSapiClient::RequestBuilderDelegator do
|
4
|
-
|
5
|
-
let(:response) { double(:response) }
|
6
|
-
let(:builder) { double(:builder, response: response, finalize: nil) }
|
7
|
-
let(:block) { -> { 1 + 1 } }
|
8
|
-
subject(:delegator) { KOSapiClient::RequestBuilderDelegator.new(builder) }
|
9
|
-
|
10
|
-
describe '#method_missing' do
|
11
|
-
|
12
|
-
it 'delegates methods which builder responds to, to the builder' do
|
13
|
-
expect(builder).to receive(:foo)
|
14
|
-
delegator.foo
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'evaluates builder when builder is not responding to a delegated method' do
|
18
|
-
expect(builder).to receive(:finalize)
|
19
|
-
expect(response).to receive(:bar)
|
20
|
-
delegator.bar
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'delegates all methods to response once RequestBuilder is evaluated' do
|
24
|
-
allow(response).to receive(:bar)
|
25
|
-
expect(builder).not_to receive(:foo)
|
26
|
-
delegator.bar
|
27
|
-
expect { delegator.foo }.to raise_error(NoMethodError)
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'throws error when method is not defined on builder or response' do
|
31
|
-
expect { delegator.foo }.to raise_error(NoMethodError)
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'replaces builder.self with self' do
|
35
|
-
allow(builder).to receive(:foo).and_return(builder)
|
36
|
-
expect(delegator.foo).to be delegator
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'delegates call with block to builder' do
|
40
|
-
allow(builder).to receive(:foo).with(:x) do |arg, &block|
|
41
|
-
expect(block).not_to be_nil
|
42
|
-
end
|
43
|
-
|
44
|
-
delegator.foo(:x, &block)
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'delegates call with block to response' do
|
48
|
-
allow(response).to receive(:foo).with(:x) do |arg, &block|
|
49
|
-
expect(block).not_to be_nil
|
50
|
-
end
|
51
|
-
|
52
|
-
delegator.foo(:x, &block)
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
describe '#respond_to_missing?' do
|
58
|
-
|
59
|
-
it 'responds to methods on builder' do
|
60
|
-
allow(builder).to receive(:foo)
|
61
|
-
expect(delegator).to respond_to(:foo)
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'responds to methods on response when evaluated' do
|
65
|
-
allow(response).to receive(:foo)
|
66
|
-
delegator.foo
|
67
|
-
expect(delegator).to respond_to(:foo)
|
68
|
-
end
|
69
|
-
|
70
|
-
end
|
71
|
-
|
72
|
-
end
|
@@ -1,80 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe KOSapiClient::RequestBuilder do
|
4
|
-
|
5
|
-
let(:url_builder) { instance_double(KOSapiClient::URLBuilder, url: 'http://example.com', set_path: nil, set_query_param: nil) }
|
6
|
-
let(:response) { double(foo: :bar) }
|
7
|
-
let(:http_client) { double(send_request: response) }
|
8
|
-
subject(:builder) { KOSapiClient::RequestBuilder.new('http://example.com', http_client, url_builder) }
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
describe '#finalize' do
|
13
|
-
|
14
|
-
it 'sends http request' do
|
15
|
-
expect(http_client).to receive(:send_request)
|
16
|
-
builder.finalize
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'stores response' do
|
20
|
-
builder.finalize
|
21
|
-
expect(builder.response).to eq response
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
describe '#find' do
|
27
|
-
|
28
|
-
it_behaves_like 'fluent api command', method_name, 10
|
29
|
-
|
30
|
-
it 'sets path in URL' do
|
31
|
-
expect(url_builder).to receive(:set_path).with(10)
|
32
|
-
builder.find(10)
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
describe '#offset' do
|
38
|
-
|
39
|
-
it_behaves_like 'fluent api command', method_name, 10
|
40
|
-
|
41
|
-
it 'sets query param' do
|
42
|
-
expect(url_builder).to receive(:set_query_param).with(:offset, 10)
|
43
|
-
builder.offset(10)
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
describe '#limit' do
|
49
|
-
|
50
|
-
it_behaves_like 'fluent api command', method_name, 10
|
51
|
-
|
52
|
-
it 'sets query param' do
|
53
|
-
expect(url_builder).to receive(:set_query_param).with(:limit, 10)
|
54
|
-
builder.limit(10)
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
58
|
-
|
59
|
-
describe '#query' do
|
60
|
-
|
61
|
-
it_behaves_like 'fluent api command', method_name, foo: 10
|
62
|
-
|
63
|
-
it 'sets query param' do
|
64
|
-
expect(url_builder).to receive(:set_query_param).with(:query, 'foo==10;bar==20')
|
65
|
-
builder.query(foo: 10, bar: 20)
|
66
|
-
end
|
67
|
-
|
68
|
-
it 'throws error when params are empty' do
|
69
|
-
expect { builder.query({}) }.to raise_error(RuntimeError)
|
70
|
-
end
|
71
|
-
|
72
|
-
it 'accepts string instead of a hash' do
|
73
|
-
expect(url_builder).to receive(:set_query_param).with(:query, 'foo==10;bar==20')
|
74
|
-
builder.query('foo==10;bar==20')
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
|
80
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe KOSapiClient::Resource::CoursesBuilder do
|
4
|
-
|
5
|
-
let(:url_builder) { instance_double(KOSapiClient::URLBuilder, url: 'http://example.com', set_query_param: nil) }
|
6
|
-
let(:response) { double(foo: :bar) }
|
7
|
-
let(:http_client) { double(send_request: response) }
|
8
|
-
subject(:builder) { KOSapiClient::Resource::CoursesBuilder.new('http://example.com', http_client, url_builder) }
|
9
|
-
|
10
|
-
describe '#detail' do
|
11
|
-
|
12
|
-
it_behaves_like 'fluent api command', method_name, 10
|
13
|
-
|
14
|
-
it 'sets detail query parameter' do
|
15
|
-
expect(url_builder).to receive(:set_query_param).with(:detail, 1)
|
16
|
-
builder.detail
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe KOSapiClient::Resource::ParallelsBuilder do
|
4
|
-
|
5
|
-
let(:url_builder) { instance_double(KOSapiClient::URLBuilder, url: 'http://example.com') }
|
6
|
-
let(:response) { double(foo: :bar) }
|
7
|
-
let(:http_client) { double(send_request: response) }
|
8
|
-
subject(:builder) { KOSapiClient::Resource::ParallelsBuilder.new('http://example.com', http_client, url_builder) }
|
9
|
-
|
10
|
-
describe '#related' do
|
11
|
-
|
12
|
-
before { allow(url_builder).to receive(:set_path) }
|
13
|
-
|
14
|
-
it 'throws error when parallel id not set' do
|
15
|
-
expect { builder.related }.to raise_error(RuntimeError)
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'returns self' do
|
19
|
-
builder.find(42)
|
20
|
-
expect(builder.related).to eq builder
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'adds related to URL' do
|
24
|
-
expect(url_builder).to receive(:set_path).with(42, 'related')
|
25
|
-
builder.find(42).related
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
describe '#students' do
|
31
|
-
|
32
|
-
before { allow(url_builder).to receive(:set_path) }
|
33
|
-
|
34
|
-
it 'throws error when parallel id not set' do
|
35
|
-
expect { builder.students }.to raise_error(RuntimeError)
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'returns self' do
|
39
|
-
builder.find(42)
|
40
|
-
expect(builder.students).to eq builder
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'adds students to URL' do
|
44
|
-
expect(url_builder).to receive(:set_path).with(42, 'students')
|
45
|
-
builder.find(42).students
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe KOSapiClient::ResourceMapper do
|
4
|
-
|
5
|
-
let(:dummy_class) { Class.new { include KOSapiClient::ResourceMapper } }
|
6
|
-
let(:dummy_instance) { dummy_class.new }
|
7
|
-
|
8
|
-
it 'defines class method .resource' do
|
9
|
-
expect(dummy_class).to respond_to(:resource)
|
10
|
-
end
|
11
|
-
|
12
|
-
describe '.resource' do
|
13
|
-
|
14
|
-
it 'creates instance method with the resource name' do
|
15
|
-
dummy_class.resource(:foo)
|
16
|
-
expect(dummy_instance).to respond_to(:foo)
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'creates method which returns builder delegator' do
|
20
|
-
dummy_class.resource(:foo)
|
21
|
-
allow(dummy_instance).to receive(:create_builder)
|
22
|
-
expect(dummy_instance.foo).to be_an_instance_of(KOSapiClient::RequestBuilderDelegator)
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'calls #create_builder' do
|
26
|
-
dummy_class.resource(:foo)
|
27
|
-
expect(dummy_instance).to receive(:create_builder).with(:foo)
|
28
|
-
dummy_instance.foo
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
@@ -1,58 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe KOSapiClient::ResponseConverter do
|
4
|
-
|
5
|
-
let(:client) { instance_double(KOSapiClient::HTTPClient) }
|
6
|
-
subject(:converter) { described_class.new(client) }
|
7
|
-
|
8
|
-
describe '#convert' do
|
9
|
-
|
10
|
-
context 'with paginated response' do
|
11
|
-
|
12
|
-
let(:next_link) { instance_double(KOSapiClient::Entity::Link) }
|
13
|
-
let(:prev_link) { instance_double(KOSapiClient::Entity::Link) }
|
14
|
-
let(:links) { instance_double(KOSapiClient::ResponseLinks, next: next_link, prev: prev_link) }
|
15
|
-
let(:api_response) { double(is_paginated?: true, items: [{xsi_type: 'courseEvent', capacity: 70}, {xsi_type: 'courseEvent', capacity: 40}], links_hash: links) }
|
16
|
-
|
17
|
-
before(:each) { allow(converter).to receive(:create_links).and_return(links) }
|
18
|
-
|
19
|
-
it 'processes paginated response' do
|
20
|
-
result = converter.convert(api_response)
|
21
|
-
expect(result).to be_an_instance_of(KOSapiClient::Entity::ResultPage)
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'creates next link' do
|
25
|
-
result = converter.convert(api_response)
|
26
|
-
expect(result.next).to be next_link
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'creates prev link' do
|
30
|
-
result = converter.convert(api_response)
|
31
|
-
expect(result.prev).to be prev_link
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context 'with single entry response' do
|
36
|
-
|
37
|
-
let(:api_response) { double(is_paginated?: false, item: {xsi_type: 'courseEvent', capacity: 70}) }
|
38
|
-
|
39
|
-
it 'processes non-paginated response' do
|
40
|
-
result = converter.convert(api_response)
|
41
|
-
expect(result).to be_an_instance_of(KOSapiClient::Entity::CourseEvent)
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|
45
|
-
|
46
|
-
context 'unknown type' do
|
47
|
-
|
48
|
-
let(:api_response) { double(is_paginated?: false, item: {xsi_type: 'unknownType'}) }
|
49
|
-
|
50
|
-
it 'raises error when type not found' do
|
51
|
-
expect { converter.convert(api_response) }.to raise_error(RuntimeError)
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
@@ -1,52 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe KOSapiClient::ResponseLinks do
|
4
|
-
|
5
|
-
let(:client) { instance_double(KOSapiClient::HTTPClient) }
|
6
|
-
subject(:links) { described_class.new }
|
7
|
-
|
8
|
-
describe '.parse' do
|
9
|
-
|
10
|
-
context 'with no links' do
|
11
|
-
it 'returns instance with no links set' do
|
12
|
-
links = described_class.parse(nil, client)
|
13
|
-
expect(links.next).not_to be
|
14
|
-
expect(links.prev).not_to be
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
context 'with both links' do
|
19
|
-
it 'parses both links' do
|
20
|
-
links = described_class.parse([{rel: 'prev', href: 'courses/?offset=0&limit=10'}, {rel: 'next', href: 'courses/?offset=20&limit=10'}], client)
|
21
|
-
expect(links.next.link_href).to eq 'courses/?offset=20&limit=10'
|
22
|
-
expect(links.prev.link_href).to eq 'courses/?offset=0&limit=10'
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
context 'with next link' do
|
27
|
-
it 'parses next link' do
|
28
|
-
links = described_class.parse({rel: 'next', href: 'courses/?offset=20&limit=10'}, client)
|
29
|
-
expect(links.next.link_href).to eq 'courses/?offset=20&limit=10'
|
30
|
-
expect(links.next.link_rel).to eq 'next'
|
31
|
-
expect(links.prev).not_to be
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context 'with prev link' do
|
36
|
-
it 'parses prev link' do
|
37
|
-
links = described_class.parse({rel: 'prev', href: 'courses/?offset=20&limit=10'}, client)
|
38
|
-
expect(links.next).not_to be
|
39
|
-
expect(links.prev.link_href).to eq 'courses/?offset=20&limit=10'
|
40
|
-
expect(links.prev.link_rel).to eq 'prev'
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'injects http client' do
|
45
|
-
links = described_class.parse({rel: 'next', href: 'courses/?offset=20&limit=10'}, client)
|
46
|
-
expect(client).to receive(:send_request)
|
47
|
-
links.next.follow
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe KOSapiClient::ResponsePreprocessor do
|
4
|
-
subject(:preprocessor) { KOSapiClient::ResponsePreprocessor.new }
|
5
|
-
let(:response) { double(parsed: parsed_response) }
|
6
|
-
let(:parsed_response) { {atom_entry: {}, 'sampleKey' => 'value', 'nestedHash' => ['stuff', {'foo'=>'bar'}]} }
|
7
|
-
|
8
|
-
it 'stringifies hash keys and make then snakecase' do
|
9
|
-
result = preprocessor.preprocess(response)
|
10
|
-
expect(result).to eq({atom_entry: {}, sample_key: 'value', nested_hash: ['stuff', {foo: 'bar'}]})
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'converts entries to array when required' do
|
14
|
-
resp = {atom_feed: {
|
15
|
-
atom_entry: {id: '1234'}
|
16
|
-
}}
|
17
|
-
result = preprocessor.preprocess(double(parsed: resp))
|
18
|
-
expect(result).to eq({atom_feed: {atom_entry: [{id: '1234'}]}})
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'merges contents to entries in feed' do
|
22
|
-
resp = {atom_feed: {
|
23
|
-
atom_entry: [{id: '1234', atom_content: {foo: :bar}}]
|
24
|
-
}}
|
25
|
-
result = preprocessor.preprocess(double(parsed: resp))
|
26
|
-
expect(result).to eq({atom_feed: {atom_entry: [{id: '1234', foo: :bar}]}})
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'merges content in single entry' do
|
30
|
-
resp = {atom_entry: {id: '1234', atom_content: {foo: :bar}}}
|
31
|
-
result = preprocessor.preprocess(double(parsed: resp))
|
32
|
-
expect(result).to eq({atom_entry: {id: '1234', foo: :bar}})
|
33
|
-
end
|
34
|
-
|
35
|
-
it 'handles empty response' do
|
36
|
-
resp = {atom_feed: {} }
|
37
|
-
result = preprocessor.preprocess(double(parsed: resp))
|
38
|
-
expect(result).to eq({atom_feed: {}})
|
39
|
-
end
|
40
|
-
|
41
|
-
context 'with invalid response' do
|
42
|
-
|
43
|
-
let(:parsed_response) { 'foo' }
|
44
|
-
|
45
|
-
it 'raises error when parsed type is not a hash' do
|
46
|
-
expect { preprocessor.preprocess(response) }.to raise_error(RuntimeError)
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
@@ -1,44 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe KOSapiClient::URLBuilder do
|
4
|
-
|
5
|
-
let(:root) { 'http://example.com' }
|
6
|
-
subject(:builder) { KOSapiClient::URLBuilder.new(root) }
|
7
|
-
|
8
|
-
describe '#set_query_param' do
|
9
|
-
|
10
|
-
it 'adds new query parameters to ur' do
|
11
|
-
builder.set_query_param('foo', 'bar')
|
12
|
-
builder.set_query_param('boo', 'far')
|
13
|
-
expect(builder.url).to eq 'http://example.com?foo=bar&boo=far'
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'overwrites already used query parameters' do
|
17
|
-
builder.set_query_param('foo', 'bar')
|
18
|
-
builder.set_query_param('foo', 'far')
|
19
|
-
expect(builder.url).to eq 'http://example.com?foo=far'
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
describe '#set_path' do
|
25
|
-
|
26
|
-
it 'sets path correctly' do
|
27
|
-
builder.set_path('foo', 'bar')
|
28
|
-
expect(builder.url).to eq 'http://example.com/foo/bar'
|
29
|
-
end
|
30
|
-
|
31
|
-
context 'with custom root' do
|
32
|
-
|
33
|
-
let(:root) { 'https://kosapi.fit.cvut.cz/api/3' }
|
34
|
-
|
35
|
-
it 'creates correct path' do
|
36
|
-
builder.set_path('bar')
|
37
|
-
expect(builder.url).to eq 'https://kosapi.fit.cvut.cz/api/3/bar'
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|