europeana-api 0.5.2 → 1.0.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.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.travis.yml +4 -2
  4. data/Gemfile +1 -8
  5. data/README.md +103 -21
  6. data/Rakefile +2 -1
  7. data/bin/console +21 -0
  8. data/europeana-api.gemspec +12 -1
  9. data/lib/europeana/api.rb +44 -68
  10. data/lib/europeana/api/annotation.rb +20 -0
  11. data/lib/europeana/api/client.rb +51 -0
  12. data/lib/europeana/api/entity.rb +16 -0
  13. data/lib/europeana/api/errors.rb +25 -41
  14. data/lib/europeana/api/faraday_middleware.rb +26 -0
  15. data/lib/europeana/api/faraday_middleware/request/authenticated_request.rb +25 -0
  16. data/lib/europeana/api/faraday_middleware/request/parameter_repetition.rb +25 -0
  17. data/lib/europeana/api/faraday_middleware/response/handle_text.rb +19 -0
  18. data/lib/europeana/api/faraday_middleware/response/parse_json_to_various.rb +52 -0
  19. data/lib/europeana/api/logger.rb +10 -0
  20. data/lib/europeana/api/queue.rb +47 -0
  21. data/lib/europeana/api/record.rb +29 -83
  22. data/lib/europeana/api/request.rb +77 -38
  23. data/lib/europeana/api/resource.rb +30 -0
  24. data/lib/europeana/api/response.rb +47 -0
  25. data/lib/europeana/api/version.rb +2 -1
  26. data/spec/europeana/api/annotation_spec.rb +77 -0
  27. data/spec/europeana/api/client_spec.rb +46 -0
  28. data/spec/europeana/api/entity_spec.rb +6 -0
  29. data/spec/europeana/api/faraday_middleware/request/authenticated_request_spec.rb +22 -0
  30. data/spec/europeana/api/queue_spec.rb +4 -0
  31. data/spec/europeana/api/record_spec.rb +54 -104
  32. data/spec/europeana/api/request_spec.rb +3 -0
  33. data/spec/europeana/api/resource_spec.rb +47 -0
  34. data/spec/europeana/api/response_spec.rb +10 -0
  35. data/spec/europeana/api_spec.rb +34 -84
  36. data/spec/spec_helper.rb +8 -0
  37. data/spec/support/shared_examples/resource_endpoint.rb +11 -0
  38. metadata +158 -34
  39. data/lib/europeana/api/record/hierarchy.rb +0 -48
  40. data/lib/europeana/api/record/hierarchy/ancestor_self_siblings.rb +0 -12
  41. data/lib/europeana/api/record/hierarchy/base.rb +0 -30
  42. data/lib/europeana/api/record/hierarchy/children.rb +0 -12
  43. data/lib/europeana/api/record/hierarchy/following_siblings.rb +0 -12
  44. data/lib/europeana/api/record/hierarchy/parent.rb +0 -12
  45. data/lib/europeana/api/record/hierarchy/preceding_siblings.rb +0 -15
  46. data/lib/europeana/api/record/hierarchy/self.rb +0 -12
  47. data/lib/europeana/api/requestable.rb +0 -118
  48. data/lib/europeana/api/search.rb +0 -64
  49. data/lib/europeana/api/search/fields.rb +0 -112
  50. data/spec/europeana/api/errors_spec.rb +0 -23
  51. data/spec/europeana/api/record/hierarchy_spec.rb +0 -15
  52. data/spec/europeana/api/search_spec.rb +0 -97
  53. data/spec/support/shared_examples/api_request.rb +0 -65
  54. data/spec/support/shared_examples/record_request.rb +0 -26
  55. data/spec/support/shared_examples/search_request.rb +0 -42
  56. data/spec/support/webmock.rb +0 -14
@@ -1,23 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Europeana
4
- module API
5
- module Errors
6
- describe MissingAPIKeyError do
7
- subject { described_class.new }
8
-
9
- it 'has an informative message' do
10
- expect(subject.message).to match('Missing API key.')
11
- end
12
- end
13
-
14
- describe RequestError do
15
- it 'has an informative message'
16
- end
17
-
18
- describe ResponseError do
19
- it 'has an informative message'
20
- end
21
- end
22
- end
23
- end
@@ -1,15 +0,0 @@
1
- RSpec.describe Europeana::API::Record::Hierarchy do
2
- let(:record_id) { '/abc/1234' }
3
- let(:params) { { callback: 'doSomething();' } }
4
-
5
- subject { described_class.new(record_id) }
6
-
7
- %w(self parent children).each do |relation|
8
- describe "##{relation}" do
9
- it "should retrieve #{relation} hierarchy data from the API" do
10
- subject.send(relation)
11
- expect(a_request(:get, %r{www.europeana.eu/api/v2/record#{record_id}/#{relation}.json})).to have_been_made.once
12
- end
13
- end
14
- end
15
- end
@@ -1,97 +0,0 @@
1
- require 'spec_helper'
2
- require 'active_support/core_ext/hash'
3
-
4
- module Europeana
5
- module API
6
- describe Search do
7
- let(:api_key) { 'xyz' }
8
- let(:params) {
9
- HashWithIndifferentAccess.new({
10
- query: 'test', profile: 'standard', qf: "where:London", rows: 100, start: 1, callback: ''
11
- })
12
- }
13
-
14
- before do
15
- Europeana::API.api_key = api_key
16
- end
17
-
18
- describe "#new" do
19
- context "with no API request params" do
20
- subject { lambda { described_class.new } }
21
- it { should_not raise_error }
22
- end
23
-
24
- context "with API request params" do
25
- subject { described_class.new(params) }
26
- it "should not raise error" do
27
- expect { subject }.not_to raise_error
28
- end
29
- it "stores the request params" do
30
- expect(subject.instance_variable_get(:@params)).to eq(params)
31
- end
32
- end
33
- end
34
-
35
- describe "#params" do
36
- subject { described_class.new(params) }
37
- it "gets params attribute" do
38
- expect(subject.params).to eq(params)
39
- end
40
- end
41
-
42
- describe "#params=" do
43
- subject { described_class.new }
44
-
45
- context "valid params" do
46
- it "sets params attribute" do
47
- subject.params = params
48
- expect(subject.instance_variable_get(:@params)).to eq(params)
49
- end
50
- end
51
- end
52
-
53
- describe "#request_uri" do
54
- it "returns a URI" do
55
- expect(subject.request_uri).to be_a(URI)
56
- end
57
-
58
- it "includes request params" do
59
- subject.params[:query] = 'paris'
60
- expect(subject.request_uri.to_s).to eq("https://www.europeana.eu/api/v2/search.json?query=paris&wskey=#{api_key}")
61
- end
62
- end
63
-
64
- describe "#params_with_authentication" do
65
- subject { described_class.new }
66
-
67
- context "with API key" do
68
- it "adds API key to params" do
69
- subject.params = params
70
- expect(subject.params_with_authentication).to eq(params.merge(:wskey => api_key))
71
- end
72
- end
73
-
74
- context "without API key" do
75
- it "raises an error" do
76
- subject.params = params
77
- Europeana::API.api_key = nil
78
- expect { subject.params_with_authentication }.to raise_error(Europeana::API::Errors::MissingAPIKeyError)
79
- end
80
- end
81
- end
82
-
83
- describe "#execute" do
84
- subject { described_class.new(params).execute }
85
- it_behaves_like "search request"
86
- end
87
-
88
- describe '.escape' do
89
- %w<\\ + - & | ! ( ) { } [ ] ^ " ~ * ? : />.map do |char|
90
- it "escapes lucene special character #{char}" do
91
- expect(described_class.escape(char)).to eq("\\#{char}")
92
- end
93
- end
94
- end
95
- end
96
- end
97
- end
@@ -1,65 +0,0 @@
1
- shared_examples "API request" do
2
- context "without API key" do
3
- before(:each) do
4
- Europeana::API.api_key = nil
5
- end
6
-
7
- it "sends no HTTP request" do
8
- begin
9
- subject
10
- rescue Europeana::API::Errors::MissingAPIKeyError
11
- end
12
- expect(a_request(:get, /www.europeana.eu\/api\/v2/)).not_to have_been_made
13
- end
14
- end
15
-
16
- context "with API key" do
17
- before(:all) do
18
- Europeana::API.api_key = "xyz"
19
- end
20
-
21
- it "returns the response as a Hash" do
22
- response = subject
23
- expect(response).to be_a(Hash)
24
- end
25
-
26
- context "when the API is unavailable" do
27
- before(:each) do
28
- Europeana::API.retry_delay = 0
29
- end
30
-
31
- it "waits then retries" do
32
- stub_request(:get, /www.europeana.eu\/api\/v2/).
33
- to_timeout.times(1).then.
34
- to_return(:body => '{"success":true}')
35
- subject
36
- expect(a_request(:get, /www.europeana.eu\/api\/v2/)).to have_been_made.times(2)
37
- end
38
- end
39
-
40
- context "when API response is unsuccessful" do
41
- context "with error msg" do
42
- it "raises a RequestError with error msg" do
43
- stub_request(:get, /www.europeana.eu\/api\/v2/).to_return(body: '{"success":false,"error":"Something went wrong"}')
44
- expect { subject }.to raise_error(Europeana::API::Errors::RequestError, "Something went wrong")
45
- end
46
- end
47
-
48
- context "without error msg" do
49
- it "raises a RequestError with status code" do
50
- stub_request(:get, /www.europeana.eu\/api\/v2/).to_return(status: 400, body: '{"success":false}')
51
- expect { subject }.to raise_error(Europeana::API::Errors::RequestError, "400")
52
- end
53
- end
54
- end
55
-
56
- context "when API response is invalid JSON" do
57
- it "raises a ResponseError" do
58
- stub_request(:get, /www.europeana.eu\/api\/v2/).to_return(:body => 'invalid JSON')
59
- expect { subject }.to raise_error(Europeana::API::Errors::ResponseError)
60
- end
61
- end
62
- end
63
- end
64
-
65
-
@@ -1,26 +0,0 @@
1
- shared_examples 'record request' do
2
- let(:api_record_endpoint) { %r{www.europeana.eu/api/v2/record#{record_id}\.json} }
3
- let(:api_key) { 'xyz' }
4
- let(:record_id) { '/abc/1234' }
5
- let(:params) { { callback: 'doSomething();' } }
6
-
7
- before(:each) do
8
- stub_request(:get, api_record_endpoint).to_return(body: '{"success":true}')
9
- Europeana::API.api_key = api_key
10
- end
11
-
12
- it_behaves_like 'API request'
13
-
14
- it 'sends a Record request to the API' do
15
- subject
16
- expect(a_request(:get, api_record_endpoint)).to have_been_made.once
17
- end
18
-
19
- context 'when record ID is invalid' do
20
- it 'raises RequestError from HTML 404 response' do
21
- stub_request(:get, api_record_endpoint).
22
- to_return(body: '<html></html>', headers: { 'Content-Type' => 'text/html' }, status: 404)
23
- expect { subject }.to raise_error(Europeana::API::Errors::RequestError, "Invalid record identifier: #{record_id}")
24
- end
25
- end
26
- end
@@ -1,42 +0,0 @@
1
- shared_examples 'search request' do
2
- before(:each) do
3
- stub_request(:get, %r{https://www.europeana.eu/api/v2/search.json}).
4
- to_return(body: '{"success":true}')
5
- end
6
-
7
- it_behaves_like 'API request'
8
-
9
- context 'with API key' do
10
- let(:api_key) { 'xyz' }
11
- let(:params) { {} }
12
-
13
- before do
14
- Europeana::API.api_key = api_key
15
- end
16
-
17
- it 'sends a Search request to the API' do
18
- subject
19
- expect(a_request(:get, %r{https://www.europeana.eu/api/v2/search.json})).
20
- to have_been_made.once
21
- end
22
-
23
- context 'without query' do
24
- it 'sends without query' do
25
- subject
26
- expect(a_request(:get, %r{https://www.europeana.eu/api/v2/search.json})).
27
- to have_been_made.once
28
- end
29
- end
30
-
31
- context 'with query' do
32
- let(:params) { { query: 'test' } }
33
-
34
- it 'sends query' do
35
- subject
36
- expect(a_request(:get, 'https://www.europeana.eu/api/v2/search.json').
37
- with(query: hash_including({'query' => params[:query]}))).
38
- to have_been_made.once
39
- end
40
- end
41
- end
42
- end
@@ -1,14 +0,0 @@
1
- RSpec.configure do |config|
2
- config.before do
3
- Europeana::API.api_key = 'xyz'
4
-
5
- stub_request(:get, %r{www.europeana.eu/api/v2/record/[^/]+/[^/]+/self\.json}).
6
- to_return(body: lambda { |_| '{"success":true, "self":{"id":"' + record_id + '", "childrenCount":0, "hasChildren":false}}' })
7
-
8
- stub_request(:get, %r{www.europeana.eu/api/v2/record/[^/]+/[^/]+/parent\.json}).
9
- to_return(body: lambda { |_| '{"success":true, "self":{"id":"' + record_id + '", "childrenCount":5, "hasChildren":false}, "parent":{}}' })
10
-
11
- stub_request(:get, %r{www.europeana.eu/api/v2/record/[^/]+/[^/]+/children\.json}).
12
- to_return(body: lambda { |_| '{"success":true, "self":{"id":"' + record_id + '", "childrenCount":5, "hasChildren":false}, "children":[]}' })
13
- end
14
- end