foederati 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3103d55ec719c63f670ee42ac313112660d19a79
4
- data.tar.gz: 3ccb00693242f608b312f20235788170136ea203
3
+ metadata.gz: 6c5cfad12d15dcb7be6a6736da964e625af651a9
4
+ data.tar.gz: ec34ace9f9ee56f4d311c78bf2d7e893302b0184
5
5
  SHA512:
6
- metadata.gz: c272cff39bea08cadf79e0f2ef76e805af218ff3497ca36be087dcd6912985ff8add1a5da04a8de44a460be89036299afa78c938f9587e53b763e65c37fe4bc1
7
- data.tar.gz: 0dc042b82753b553db3b3cad95e6fb7ebe94b46ebc031eda1113ee28cf04dbb93c00acf50e995ce320063c2b3ef32430679a708322e3a308344473c2a31278fc
6
+ metadata.gz: 253ef90d2ee8609678e6109ffab70609cc5c311a97c0feb80d4e249507cc0ffefc872bb08c2b8363dd4810831c770ceab259f6db82756be98196aac25118ed8d
7
+ data.tar.gz: 901aa884c5d56172e0d3f3586eb6187fc1ae748390d9d05fab15dab97824b8d0f27c16084bc666766c675b59dea5939b25caf4f9cbb6d185386938bb09db4743
@@ -2,6 +2,7 @@
2
2
  require 'active_support/core_ext/module/delegation'
3
3
  require 'active_support/core_ext/object/blank'
4
4
  require 'active_support/hash_with_indifferent_access'
5
+ require 'active_support/inflector'
5
6
  require 'faraday'
6
7
  require 'faraday_middleware'
7
8
  require 'foederati/faraday_middleware'
@@ -11,14 +11,17 @@ module Foederati
11
11
 
12
12
  # TODO validate the type of values added to these
13
13
  Urls = Struct.new(:api, :site)
14
+ DefaultParams = Struct.new(:query)
14
15
  Results = Struct.new(:items, :total)
15
16
  Fields = Struct.new(:title, :thumbnail, :url)
16
17
 
17
- attr_reader :id, :urls, :results, :fields
18
+ attr_reader :id, :urls, :default_params, :results, :fields
19
+ attr_writer :name
18
20
 
19
21
  def initialize(id, &block)
20
22
  @id = id
21
23
  @urls = Urls.new
24
+ @default_params = DefaultParams.new
22
25
  @results = Results.new
23
26
  @fields = Fields.new
24
27
 
@@ -27,6 +30,10 @@ module Foederati
27
30
  self
28
31
  end
29
32
 
33
+ def name
34
+ @name || id.to_s.titleize
35
+ end
36
+
30
37
  # TODO sanity check things like presence of API URL
31
38
  def search(**params)
32
39
  request.execute(params).normalise
@@ -34,7 +34,9 @@ module Foederati
34
34
  #
35
35
  # @return [Hash]
36
36
  def default_params
37
- { api_key: Foederati.api_keys.send(id) }.merge(Foederati.defaults.to_h)
37
+ { api_key: Foederati.api_keys.send(id) }.
38
+ merge(Foederati.defaults.to_h).
39
+ merge(provider.default_params.to_h)
38
40
  end
39
41
 
40
42
  ##
@@ -43,7 +45,9 @@ module Foederati
43
45
  # @param params [Hash] query-specific URL parameters
44
46
  # @return [String] the provider's API URL with all necessary params
45
47
  def api_url(**params)
46
- format(urls.api, default_params.merge(params))
48
+ local_params = params.dup
49
+ local_params.delete(:query) if local_params[:query].blank? && provider.default_params.query.present?
50
+ format(urls.api, default_params.merge(local_params))
47
51
  end
48
52
  end
49
53
  end
@@ -27,9 +27,11 @@ module Foederati
27
27
  id => {
28
28
  total: fetch_from_response(results.total, body) || 0,
29
29
  results: items_from_response.map do |item|
30
+ thumbnail = fetch_from_response(fields.thumbnail, item)
31
+ thumbnail = thumbnail.is_a?(Array) ? thumbnail.first : thumbnail
30
32
  {
31
33
  title: fetch_from_response(fields.title, item),
32
- thumbnail: fetch_from_response(fields.thumbnail, item),
34
+ thumbnail: thumbnail,
33
35
  url: fetch_from_response(fields.url, item)
34
36
  }
35
37
  end
@@ -2,6 +2,8 @@
2
2
 
3
3
  # DigitalNZ
4
4
  Foederati::Providers.register :digitalnz do
5
+ self.name = 'DigitalNZ'
6
+
5
7
  urls.api = 'https://api.digitalnz.org/v3/records.json?api_key=%{api_key}&text=%{query}&per_page=%{limit}'
6
8
  urls.site = 'https://digitalnz.org/records?text=%{query}'
7
9
 
@@ -2,6 +2,8 @@
2
2
 
3
3
  # DPLA
4
4
  Foederati::Providers.register :dpla do
5
+ self.name = 'DPLA'
6
+
5
7
  urls.api = 'https://api.dp.la/v2/items?api_key=%{api_key}&q=%{query}&page_size=%{limit}'
6
8
  urls.site = 'https://dp.la/search?q=%{query}'
7
9
 
@@ -5,10 +5,21 @@ Foederati::Providers.register :trove do
5
5
  urls.api = 'http://api.trove.nla.gov.au/result?key=%{api_key}&q=%{query}&n=%{limit}&zone=picture&encoding=json'
6
6
  urls.site = 'http://trove.nla.gov.au/result?q=%{query}'
7
7
 
8
- results.items = ->(response) { response['response']['zone'].detect { |zone| zone['name'] == 'picture' }['records']['work'] }
9
- results.total = ->(response) { response['response']['zone'].detect { |zone| zone['name'] == 'picture' }['records']['total'].to_i }
8
+ default_params.query = '%20'
9
+
10
+ results.items = lambda do |response|
11
+ response['response']['zone'] ? response['response']['zone'].detect { |zone| zone['name'] == 'picture' }['records']['work'] : []
12
+ end
13
+ results.total = lambda do |response|
14
+ response['response']['zone'] ? response['response']['zone'].detect { |zone| zone['name'] == 'picture' }['records']['total'].to_i : 0
15
+ end
10
16
 
11
17
  fields.title = 'title'
12
- fields.thumbnail = ->(item) { item['identifier'].detect { |identifier| identifier['linktype'] == 'thumbnail' }['value'] }
18
+ fields.thumbnail = lambda do |item|
19
+ if item['identifier']
20
+ thumb_identifier = item['identifier'].detect { |identifier| identifier['linktype'] == 'thumbnail' }
21
+ thumb_identifier ? thumb_identifier['value'] : nil
22
+ end
23
+ end
13
24
  fields.url = 'troveUrl'
14
25
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Foederati
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
@@ -5,10 +5,12 @@ RSpec.describe Foederati::Provider::Request do
5
5
  let(:provider) do
6
6
  Foederati::Provider.new(:good_provider).tap do |p|
7
7
  p.urls.api = "#{api_url}?q=%{query}&k=%{api_key}&l=%{limit}"
8
+ p.default_params.query = empty_query
8
9
  end
9
10
  end
10
11
  let(:api_url) { 'http://api.example.com/' }
11
12
  let(:query) { 'whale' }
13
+ let(:empty_query) { 'EMPTY' }
12
14
  let(:api_key) { 'moby' }
13
15
  let(:result_limit) { 10 }
14
16
 
@@ -68,14 +70,22 @@ RSpec.describe Foederati::Provider::Request do
68
70
  end
69
71
 
70
72
  describe '#api_url' do
71
- it 'replaces placeholders in API URL with params' do
72
- expect(subject.api_url(query: query)).to \
73
- eq("http://api.example.com/?q=#{query}&k=#{api_key}&l=#{result_limit}")
74
- end
73
+ context 'when the query is set' do
74
+ it 'replaces placeholders in API URL with params' do
75
+ expect(subject.api_url(query: query)).to \
76
+ eq("http://api.example.com/?q=#{query}&k=#{api_key}&l=#{result_limit}")
77
+ end
75
78
 
76
- it 'overrides defaults with args' do
77
- expect(subject.api_url(query: query, limit: 5)).to \
78
- eq("http://api.example.com/?q=#{query}&k=#{api_key}&l=5")
79
+ it 'overrides defaults with args' do
80
+ expect(subject.api_url(query: query, limit: 5)).to \
81
+ eq("http://api.example.com/?q=#{query}&k=#{api_key}&l=5")
82
+ end
83
+ end
84
+ context 'when the query is empty' do
85
+ it 'replaces the query with the default empty query' do
86
+ expect(subject.api_url(query: '')).to \
87
+ eq("http://api.example.com/?q=#{empty_query}&k=#{api_key}&l=#{result_limit}")
88
+ end
79
89
  end
80
90
  end
81
91
  end
@@ -6,6 +6,11 @@ RSpec.describe Foederati::Provider do
6
6
  it { is_expected.to respond_to :site }
7
7
  end
8
8
 
9
+ describe '#default_params' do
10
+ subject { described_class.new(:new_provider).default_params }
11
+ it { is_expected.to respond_to :query }
12
+ end
13
+
9
14
  describe '#results' do
10
15
  subject { described_class.new(:new_provider).results }
11
16
  it { is_expected.to respond_to :items }
@@ -19,6 +24,23 @@ RSpec.describe Foederati::Provider do
19
24
  it { is_expected.to respond_to :url }
20
25
  end
21
26
 
27
+ describe '#name' do
28
+ subject { described_class.new(:new_provider) }
29
+
30
+ context 'when not set' do
31
+ it 'is derived from ID' do
32
+ expect(subject.name).to eq('New Provider')
33
+ end
34
+ end
35
+
36
+ context 'when set' do
37
+ it 'is returned as set' do
38
+ subject.name = 'Nice name'
39
+ expect(subject.name).to eq('Nice name')
40
+ end
41
+ end
42
+ end
43
+
22
44
  describe '#initialize' do
23
45
  it 'evaluates a given block' do
24
46
  provider = described_class.new(:new_provider) do
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ RSpec.describe Foederati::Providers do
3
+ it 'has registered DigitalNZ' do
4
+ expect(described_class.registry).to have_key(:digitalnz)
5
+ end
6
+
7
+ describe 'DigitalNZ provider' do
8
+ let(:provider) { described_class.get(:digitalnz) }
9
+
10
+ describe '#name' do
11
+ subject { provider.name }
12
+ it { is_expected.to eq('DigitalNZ') }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ RSpec.describe Foederati::Providers do
3
+ it 'has registered DPLA' do
4
+ expect(described_class.registry).to have_key(:dpla)
5
+ end
6
+
7
+ describe 'DPLA provider' do
8
+ let(:provider) { described_class.get(:dpla) }
9
+
10
+ describe '#name' do
11
+ subject { provider.name }
12
+ it { is_expected.to eq('DPLA') }
13
+ end
14
+ end
15
+ end
@@ -3,4 +3,13 @@ RSpec.describe Foederati::Providers do
3
3
  it 'has registered Europeana' do
4
4
  expect(described_class.registry).to have_key(:europeana)
5
5
  end
6
+
7
+ describe 'Europeana provider' do
8
+ let(:provider) { described_class.get(:europeana) }
9
+
10
+ describe '#name' do
11
+ subject { provider.name }
12
+ it { is_expected.to eq('Europeana') }
13
+ end
14
+ end
6
15
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ RSpec.describe Foederati::Providers do
3
+ it 'has registered Trove' do
4
+ expect(described_class.registry).to have_key(:trove)
5
+ end
6
+
7
+ describe 'Trove provider' do
8
+ let(:provider) { described_class.get(:trove) }
9
+
10
+ describe '#name' do
11
+ subject { provider.name }
12
+ it { is_expected.to eq('Trove') }
13
+ end
14
+
15
+ describe '#default_params' do
16
+ describe '#query' do
17
+ subject { provider.default_params.query }
18
+ it { is_expected.to eq('%20') }
19
+ end
20
+ end
21
+ end
22
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foederati
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Doe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-18 00:00:00.000000000 Z
11
+ date: 2017-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -153,7 +153,10 @@ files:
153
153
  - spec/lib/foederati/provider/request_spec.rb
154
154
  - spec/lib/foederati/provider/response_spec.rb
155
155
  - spec/lib/foederati/provider_spec.rb
156
+ - spec/lib/foederati/providers/digitalnz_spec.rb
157
+ - spec/lib/foederati/providers/dpla_spec.rb
156
158
  - spec/lib/foederati/providers/europeana_spec.rb
159
+ - spec/lib/foederati/providers/trove_spec.rb
157
160
  - spec/lib/foederati/providers_spec.rb
158
161
  - spec/lib/foederati_spec.rb
159
162
  - spec/spec_helper.rb
@@ -177,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
180
  version: '0'
178
181
  requirements: []
179
182
  rubyforge_project:
180
- rubygems_version: 2.5.2
183
+ rubygems_version: 2.5.1
181
184
  signing_key:
182
185
  specification_version: 4
183
186
  summary: Federated search