europeana-api 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,89 @@
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("http://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
+ end
88
+ end
89
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ module Europeana
4
+ describe API do
5
+ let(:api_key) { 'xyz' }
6
+ let(:record_id) { '/abc/1234' }
7
+ let(:params) { { callback: 'doSomething();' } }
8
+
9
+ before(:each) do
10
+ described_class.defaults!
11
+ end
12
+
13
+ describe '.url' do
14
+ it 'defaults to the live API URL' do
15
+ expect(subject.url).to eq('http://www.europeana.eu/api/v2')
16
+ end
17
+ end
18
+
19
+ describe '.url=' do
20
+ let(:url) { 'http://www.example.com/v2' }
21
+ it 'accepts override of the API URL' do
22
+ described_class.url = url
23
+ expect(described_class.instance_variable_get(:@url)).to eq(url)
24
+ end
25
+ end
26
+
27
+ describe ".api_key=" do
28
+ it "sets the API key" do
29
+ described_class.api_key = api_key
30
+ expect(described_class.instance_variable_get(:@api_key)).to eq(api_key)
31
+ end
32
+ end
33
+
34
+ describe ".api_key" do
35
+ it "gets the API key" do
36
+ described_class.instance_variable_set(:@api_key, api_key)
37
+ expect(described_class.api_key).to eq(api_key)
38
+ end
39
+ end
40
+
41
+ describe ".max_retries" do
42
+ it "defaults to 5" do
43
+ expect(described_class.max_retries).to eq(5)
44
+ end
45
+ end
46
+
47
+ describe ".max_retries=" do
48
+ it "sets the maximum number of retries" do
49
+ described_class.max_retries = 2
50
+ expect(described_class.max_retries).to eq(2)
51
+ end
52
+ end
53
+
54
+ describe ".retry_delay" do
55
+ it "defaults to 10" do
56
+ expect(described_class.retry_delay).to eq(10)
57
+ end
58
+ end
59
+
60
+ describe ".retry_delay=" do
61
+ it "sets the retry delay" do
62
+ described_class.retry_delay = 3
63
+ expect(described_class.retry_delay).to eq(3)
64
+ end
65
+ end
66
+
67
+ describe ".defaults!" do
68
+ it "sets the API URL to its default" do
69
+ described_class.url = 'http://www.example.com/v2'
70
+ expect { described_class.defaults! }.
71
+ to change { described_class.url }.
72
+ from('http://www.example.com/v2').
73
+ to('http://www.europeana.eu/api/v2')
74
+ end
75
+
76
+ it "sets retry delay to its default" do
77
+ described_class.retry_delay = 3
78
+ expect { described_class.defaults! }.to change { described_class.retry_delay }.from(3).to(10)
79
+ end
80
+
81
+ it "sets max retries to its default" do
82
+ described_class.max_retries = 3
83
+ expect { described_class.defaults! }.to change { described_class.max_retries }.from(3).to(5)
84
+ end
85
+ end
86
+
87
+ describe ".search" do
88
+ subject { described_class.search(params) }
89
+ it_behaves_like "search request"
90
+ end
91
+
92
+ describe ".record" do
93
+ subject { described_class.record(record_id, params) }
94
+ it_behaves_like "record request"
95
+ end
96
+ end
97
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'europeana'
1
+ require 'europeana/api'
2
2
  require 'webmock/rspec'
3
3
 
4
4
  Dir["./spec/support/**/*.rb"].each { |f| require f }
@@ -1,33 +1,33 @@
1
1
  shared_examples "API request" do
2
2
  context "without API key" do
3
3
  before(:each) do
4
- Europeana.api_key = nil
4
+ Europeana::API.api_key = nil
5
5
  end
6
-
6
+
7
7
  it "sends no HTTP request" do
8
8
  begin
9
9
  subject
10
- rescue Europeana::Errors::MissingAPIKeyError
10
+ rescue Europeana::API::Errors::MissingAPIKeyError
11
11
  end
12
12
  expect(a_request(:get, /www.europeana.eu\/api\/v2/)).not_to have_been_made
13
13
  end
14
14
  end
15
-
15
+
16
16
  context "with API key" do
17
17
  before(:all) do
18
- Europeana.api_key = "xyz"
18
+ Europeana::API.api_key = "xyz"
19
19
  end
20
-
20
+
21
21
  it "returns the response as a Hash" do
22
22
  response = subject
23
23
  expect(response).to be_a(Hash)
24
24
  end
25
-
25
+
26
26
  context "when the API is unavailable" do
27
27
  before(:each) do
28
- Europeana.retry_delay = 0
28
+ Europeana::API.retry_delay = 0
29
29
  end
30
-
30
+
31
31
  it "waits then retries" do
32
32
  stub_request(:get, /www.europeana.eu\/api\/v2/).
33
33
  to_timeout.times(1).then.
@@ -36,18 +36,27 @@ shared_examples "API request" do
36
36
  expect(a_request(:get, /www.europeana.eu\/api\/v2/)).to have_been_made.times(2)
37
37
  end
38
38
  end
39
-
39
+
40
40
  context "when API response is unsuccessful" do
41
- it "raises a RequestError" do
42
- stub_request(:get, /www.europeana.eu\/api\/v2/).to_return(:body => '{"success":false,"error":"Something went wrong"}')
43
- expect { subject }.to raise_error(Europeana::Errors::RequestError, "Something went wrong")
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
44
53
  end
45
54
  end
46
-
55
+
47
56
  context "when API response is invalid JSON" do
48
57
  it "raises a ResponseError" do
49
58
  stub_request(:get, /www.europeana.eu\/api\/v2/).to_return(:body => 'invalid JSON')
50
- expect { subject }.to raise_error(Europeana::Errors::ResponseError)
59
+ expect { subject }.to raise_error(Europeana::API::Errors::ResponseError)
51
60
  end
52
61
  end
53
62
  end
@@ -1,31 +1,30 @@
1
1
  shared_examples "record request" do
2
- before(:all) do
3
- @api_key = "xyz"
4
- @record_id = "/abc/1234"
5
- @params = { :callback => "doSomething();" }
6
- end
7
-
8
2
  before(:each) do
9
- stub_request(:get, /www.europeana.eu\/api\/v2\/record#{@record_id}\.json/).to_return(:body => '{"success":true}')
3
+ stub_request(:get, /www.europeana.eu\/api\/v2\/record#{record_id}\.json/).
4
+ to_return(body: '{"success":true}')
10
5
  end
11
-
6
+
12
7
  it_behaves_like "API request"
13
-
8
+
14
9
  context "with API key" do
15
- before(:all) do
16
- Europeana.api_key = @api_key
10
+ before(:each) do
11
+ Europeana::API.api_key = api_key
17
12
  end
18
-
13
+
14
+ let(:api_key) { 'xyz' }
15
+ let(:record_id) { '/abc/1234' }
16
+ let(:params) { { callback: 'doSomething();' } }
17
+
19
18
  it "sends a Record request to the API" do
20
19
  subject
21
- expect(a_request(:get, /www.europeana.eu\/api\/v2\/record#{@record_id}\.json/)).to have_been_made.once
20
+ expect(a_request(:get, /www.europeana.eu\/api\/v2\/record#{record_id}\.json/)).to have_been_made.once
22
21
  end
23
-
22
+
24
23
  context "when record ID is invalid" do
25
24
  it "raises RequestError from HTML 404 response" do
26
- stub_request(:get, /www.europeana.eu\/api\/v2\/record#{@record_id}\.json/).
27
- to_return(:body => '<html></html>', :headers => { 'Content-Type' => 'text/html' }, :status => 404)
28
- expect { subject }.to raise_error(Europeana::Errors::RequestError, "Invalid record identifier: #{@record_id}")
25
+ stub_request(:get, /www.europeana.eu\/api\/v2\/record#{record_id}\.json/).
26
+ to_return(body: '<html></html>', headers: { 'Content-Type' => 'text/html' }, status: 404)
27
+ expect { subject }.to raise_error(Europeana::API::Errors::RequestError, "Invalid record identifier: #{record_id}")
29
28
  end
30
29
  end
31
30
  end
@@ -1,39 +1,40 @@
1
1
  shared_examples "search request" do
2
- before(:all) do
3
- @api_key = "xyz"
4
- @params = {}
5
- end
6
-
7
2
  before(:each) do
8
- stub_request(:get, /www.europeana.eu\/api\/v2\/search\.json/).to_return(:body => '{"success":true}')
3
+ stub_request(:get, %r{www.europeana.eu/api/v2/search.json}).
4
+ to_return(body: '{"success":true}')
9
5
  end
10
-
6
+
11
7
  it_behaves_like "API request"
12
-
8
+
13
9
  context "with API key" do
14
- before(:all) do
15
- Europeana.api_key = @api_key
10
+ let(:api_key) { 'xyz' }
11
+ let(:params) { {} }
12
+
13
+ before do
14
+ Europeana::API.api_key = api_key
16
15
  end
17
-
16
+
18
17
  it "sends a Search request to the API" do
19
18
  subject
20
- expect(a_request(:get, /www.europeana.eu\/api\/v2\/search\.json/)).to have_been_made.once
19
+ expect(a_request(:get, %r{www.europeana.eu/api/v2/search.json})).
20
+ to have_been_made.once
21
21
  end
22
-
22
+
23
23
  context "without query" do
24
24
  it "sets an empty query" do
25
25
  subject
26
- expect(a_request(:get, /www.europeana.eu\/api\/v2\/search\.json\?query=/)).to have_been_made.once
26
+ expect(a_request(:get, %r{www.europeana.eu/api/v2/search.json?query=})).
27
+ to have_been_made.once
27
28
  end
28
29
  end
29
-
30
+
30
31
  context "with query" do
31
- before(:all) do
32
- @params = { :query => "test" }
33
- end
32
+ let(:params) { { query: 'test' } }
33
+
34
34
  it "sends query" do
35
35
  subject
36
- expect(a_request(:get, /www.europeana.eu\/api\/v2\/search\.json\?query=test/)).to have_been_made.once
36
+ expect(a_request(:get, %r{www.europeana.eu/api/v2/search.json?query=test})).
37
+ to have_been_made.once
37
38
  end
38
39
  end
39
40
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: europeana-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Doe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-19 00:00:00.000000000 Z
11
+ date: 2015-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -102,8 +102,13 @@ extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
104
  - ".gitignore"
105
+ - ".hound.yml"
106
+ - ".rspec"
107
+ - ".rubocop.yml"
108
+ - ".ruby-style.yml"
109
+ - ".travis.yml"
105
110
  - Gemfile
106
- - LICENSE.pdf
111
+ - LICENSE.md
107
112
  - README.md
108
113
  - Rakefile
109
114
  - europeana-api.gemspec
@@ -114,17 +119,17 @@ files:
114
119
  - lib/europeana/api/search.rb
115
120
  - lib/europeana/api/search/fields.rb
116
121
  - lib/europeana/api/version.rb
117
- - spec/europeana/errors_spec.rb
118
- - spec/europeana/record_spec.rb
119
- - spec/europeana/search_spec.rb
120
- - spec/europeana_spec.rb
122
+ - spec/europeana/api/errors_spec.rb
123
+ - spec/europeana/api/record_spec.rb
124
+ - spec/europeana/api/search_spec.rb
125
+ - spec/europeana/api_spec.rb
121
126
  - spec/spec_helper.rb
122
127
  - spec/support/shared_examples/api_request.rb
123
128
  - spec/support/shared_examples/record_request.rb
124
129
  - spec/support/shared_examples/search_request.rb
125
130
  homepage: https://github.com/rwd/europeana-api-client-ruby
126
131
  licenses:
127
- - EUPL 1.1
132
+ - EUPL V.1.1
128
133
  metadata: {}
129
134
  post_install_message:
130
135
  rdoc_options: []
@@ -142,15 +147,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
147
  version: '0'
143
148
  requirements: []
144
149
  rubyforge_project:
145
- rubygems_version: 2.4.6
150
+ rubygems_version: 2.4.8
146
151
  signing_key:
147
152
  specification_version: 4
148
153
  summary: Ruby client library for the Europeana API
149
154
  test_files:
150
- - spec/europeana/errors_spec.rb
151
- - spec/europeana/record_spec.rb
152
- - spec/europeana/search_spec.rb
153
- - spec/europeana_spec.rb
155
+ - spec/europeana/api/errors_spec.rb
156
+ - spec/europeana/api/record_spec.rb
157
+ - spec/europeana/api/search_spec.rb
158
+ - spec/europeana/api_spec.rb
154
159
  - spec/spec_helper.rb
155
160
  - spec/support/shared_examples/api_request.rb
156
161
  - spec/support/shared_examples/record_request.rb