Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
"EDS_WHATNOT_URI": {
"required": true
},
"EDS_ARTICLE_FACETS": {
"required": true
},
"EDS_BOOK_FACETS": {
"required": true
},
"EDS_WHATNOT_FACETS": {
"required": true
},
"EDS_PASSWORD": {
"required": true
},
Expand Down
14 changes: 9 additions & 5 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,22 @@ def search_target

# Seaches EDS
def search_eds
raw_results = SearchEds.new.search(strip_q, eds_profile)
raw_results = SearchEds.new.search(strip_q, eds_profile, eds_facets)
NormalizeEds.new.to_result(raw_results, params[:target])
end

# Determines appropriate EDS profile
def eds_profile
ENV['EDS_WHATNOT_PROFILE']
end

def eds_facets
if params[:target] == 'articles'
ENV['EDS_NO_ALEPH_PROFILE']
ENV['EDS_ARTICLE_FACETS']
elsif params[:target] == 'books'
ENV['EDS_BOOK_FACETS']
elsif params[:target] == 'whatnot'
ENV['EDS_WHATNOT_PROFILE']
else
ENV['EDS_ALEPH_PROFILE']
ENV['EDS_WHATNOT_FACETS']
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/normalize_eds_articles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def doi
end

def journal_title
return unless bibentity['Titles']
return [] unless bibentity['Titles']
[bibentity['Titles'][0]['TitleFull'],
'https://sfx.mit.edu/sfx_local?' + 'rft.jtitle=' +
URI.encode_www_form_component(
Expand Down
13 changes: 7 additions & 6 deletions app/models/search_eds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def initialize
@results = {}
end

def search(term, profile)
def search(term, profile, facets)
return 'invalid credentials' unless @auth_token
@session_key = create_session(profile) if @auth_token
raw_results = search_filtered(term)
raw_results = search_filtered(term, facets)
end_session
raw_results
end
Expand All @@ -37,18 +37,19 @@ def clean_term(term)
URI.encode(term.strip.tr(' ', '+').delete(','))
end

def search_url(term)
def search_url(term, facets)
[EDS_URL, '/edsapi/rest/Search?query=', clean_term(term),
'&searchmode=all', "&resultsperpage=#{RESULTS_PER_BOX}",
'&pagenumber=1', '&sort=relevance', '&highlight=n', '&includefacets=n',
'&view=detailed', '&autosuggest=n', '&expander=fulltext'].join('')
'&view=detailed', '&autosuggest=n', '&expander=fulltext',
facets].join('')
end

def search_filtered(term)
def search_filtered(term, facets)
result = HTTP.headers(accept: 'application/json',
'x-authenticationToken': @auth_token,
'x-sessionToken': @session_key)
.get(search_url(term).to_s).to_s
.get(search_url(term, facets).to_s).to_s
JSON.parse(result)
end

Expand Down
17 changes: 2 additions & 15 deletions app/views/search/search.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,8 @@
"View all #{number_with_delimiter(@results['total'])} website results",
"https://cse.google.com/cse?cx=#{ENV['GOOGLE_CUSTOM_SEARCH_ID']}&ie=UTF-8&q=#{params[:q]}&sa=Search#gsc.tab=0&gsc.q=#{params[:q]}",
data: {count: @results['total']}) %>
<% elsif params[:target] == 'articles' %>
<%= link_to(
"View all #{number_with_delimiter(@results['total'])} articles",
"#{ENV['EDS_NO_ALEPH_URI']}#{params[:q]}",
data: {count: @results['total']}) %>
<% elsif params[:target] == 'books' %>
<%= link_to(
"View all #{number_with_delimiter(@results['total'])} books and media",
"#{ENV['EDS_ALEPH_URI']}#{params[:q]}",
data: {count: @results['total']}) %>
<% elsif params[:target] == 'whatnot' %>
<%= link_to(
"View all #{number_with_delimiter(@results['total'])} like this",
"#{ENV['EDS_WHATNOT_URI']}#{params[:q]}",
data: {count: @results['total']}) %>
<% elsif %w(articles books whatnot).include?(params[:target]) %>
View all <%= number_with_delimiter(@results['total']) %> like this link returning soon(ish)!
<% end %>
<% else %>
No results found.
Expand Down
4 changes: 2 additions & 2 deletions test/models/normalize_eds_articles_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class NormalizeEdsArticlesTest < ActiveSupport::TestCase
def popcorn_articles
VCR.use_cassette('popcorn articles',
allow_playback_repeats: true) do
raw_query = SearchEds.new.search('popcorn', 'apinoaleph')
raw_query = SearchEds.new.search('popcorn', 'apiwhatnot', '')
NormalizeEds.new.to_result(raw_query, 'articles')
end
end
Expand Down Expand Up @@ -63,7 +63,7 @@ def popcorn_articles
test 'normalized articles can handle no authors' do
VCR.use_cassette('no article authors',
allow_playback_repeats: true) do
raw_query = SearchEds.new.search('orange', 'apinoaleph')
raw_query = SearchEds.new.search('orange', 'apinoaleph', '')
query = NormalizeEds.new.to_result(raw_query, 'articles')
assert_nil(query['results'][0].authors)
end
Expand Down
10 changes: 5 additions & 5 deletions test/models/normalize_eds_books_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class NormalizeEdsBooksTest < ActiveSupport::TestCase
def popcorn_books
VCR.use_cassette('popcorn books',
allow_playback_repeats: true) do
raw_query = SearchEds.new.search('popcorn', 'apibarton')
raw_query = SearchEds.new.search('popcorn', 'apiwhatnot', '')
NormalizeEds.new.to_result(raw_query, 'books')
end
end
Expand Down Expand Up @@ -37,7 +37,7 @@ def popcorn_books
test 'normalized books have expected multiple authors' do
VCR.use_cassette('multiple book authors',
allow_playback_repeats: true) do
raw_query = SearchEds.new.search('vonnegut', 'apibarton')
raw_query = SearchEds.new.search('vonnegut', 'apibarton', '')
query = NormalizeEds.new.to_result(raw_query, 'books')
assert_equal(
['Vonnegut, Kurt', 'Wakefield, Dan'],
Expand All @@ -49,7 +49,7 @@ def popcorn_books
test 'normalized books can handle no authors' do
VCR.use_cassette('no book authors',
allow_playback_repeats: true) do
raw_query = SearchEds.new.search('orange', 'apibarton')
raw_query = SearchEds.new.search('orange', 'apibarton', '')
query = NormalizeEds.new.to_result(raw_query, 'books')
assert_nil(query['results'][0].authors)
end
Expand Down Expand Up @@ -88,7 +88,7 @@ def popcorn_books
test 'normalized books can handle multiple subjects' do
VCR.use_cassette('multiple book subjects',
allow_playback_repeats: true) do
raw_query = SearchEds.new.search('orange', 'apibarton')
raw_query = SearchEds.new.search('orange', 'apibarton', '')
query = NormalizeEds.new.to_result(raw_query, 'books')
assert_equal(4, query['results'][1].subjects.count)
end
Expand All @@ -108,7 +108,7 @@ def popcorn_books
test 'normalized books can have no locations' do
VCR.use_cassette('multiple book subjects',
allow_playback_repeats: true) do
raw_query = SearchEds.new.search('orange', 'apibarton')
raw_query = SearchEds.new.search('orange', 'apibarton', '')
query = NormalizeEds.new.to_result(raw_query, 'books')
assert_nil(query['results'][1].location)
end
Expand Down
13 changes: 7 additions & 6 deletions test/models/normalize_eds_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class NormalizeEdsTest < ActiveSupport::TestCase
test 'searches with no results do not error' do
VCR.use_cassette('no results',
allow_playback_repeats: true) do
raw_query = SearchEds.new.search('popcornandorangejuice', 'apinoaleph')
raw_query = SearchEds.new.search('popcornandorangejuice',
'apiwhatnot', '')
query = NormalizeEds.new.to_result(raw_query, 'articles')
assert_equal(0, query['total'])
end
Expand All @@ -16,7 +17,7 @@ class NormalizeEdsTest < ActiveSupport::TestCase
VCR.use_cassette('rollbar4') do
searchterm = 'R. F. Harrington, Field computation by moment methods. '\
'Macmillan, 1968'
raw_query = SearchEds.new.search(searchterm, 'apinoaleph')
raw_query = SearchEds.new.search(searchterm, 'apiwhatnot', '')
query = NormalizeEds.new.to_result(raw_query, 'articles')
assert_equal(77_612_346, query['total'])
end
Expand All @@ -28,7 +29,7 @@ class NormalizeEdsTest < ActiveSupport::TestCase
VCR.use_cassette('rollbar4a') do
searchterm = 'R. F. Harrington, Field computation by moment methods. '\
'Macmillan, 1968'
raw_query = SearchEds.new.search(searchterm, 'apibarton')
raw_query = SearchEds.new.search(searchterm, 'apiwhatnot', '')
query = NormalizeEds.new.to_result(raw_query, 'books')
assert_equal(1_268_662, query['total'])
end
Expand All @@ -48,7 +49,7 @@ class NormalizeEdsTest < ActiveSupport::TestCase
test 'can handle missing years' do
VCR.use_cassette('rollbar8') do
searchterm = 'web of science'
raw_query = SearchEds.new.search(searchterm, 'apinoaleph')
raw_query = SearchEds.new.search(searchterm, 'apiwhatnot', '')
query = NormalizeEds.new.to_result(raw_query, 'articles')
assert_equal(37_667_909, query['total'])
assert_nil(query['results'][0].year)
Expand All @@ -58,7 +59,7 @@ class NormalizeEdsTest < ActiveSupport::TestCase
test 'can handle missing title titlefull with item title' do
VCR.use_cassette('popcorn books',
allow_playback_repeats: true) do
raw_query = SearchEds.new.search('popcorn', 'apibarton')
raw_query = SearchEds.new.search('popcorn', 'apiwhatnot', '')
query = NormalizeEds.new.to_result(raw_query, 'books')
assert_equal('Popcorn handbook', query['results'][3].title)
end
Expand All @@ -67,7 +68,7 @@ class NormalizeEdsTest < ActiveSupport::TestCase
test 'can handle missing title titlefull and no item title' do
VCR.use_cassette('popcorn books',
allow_playback_repeats: true) do
raw_query = SearchEds.new.search('popcorn', 'apibarton')
raw_query = SearchEds.new.search('popcorn', 'apiwhatnot', '')
query = NormalizeEds.new.to_result(raw_query, 'books')
assert_equal('unknown title', query['results'][4].title)
end
Expand Down
6 changes: 3 additions & 3 deletions test/models/search_eds_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ class SearchEdsTest < ActiveSupport::TestCase
test 'can search articles' do
VCR.use_cassette('popcorn articles',
allow_playback_repeats: true) do
query = SearchEds.new.search('popcorn', 'apinoaleph')
query = SearchEds.new.search('popcorn', 'apiwhatnot', '')
assert_equal(Hash, query.class)
end
end

test 'can search books' do
VCR.use_cassette('popcorn non articles',
allow_playback_repeats: true) do
query = SearchEds.new.search('popcorn', 'apibarton')
query = SearchEds.new.search('popcorn', 'apiwhatnot', '')
assert_equal(Hash, query.class)
end
end

test 'invalid credentials' do
VCR.use_cassette('invalid credentials') do
query = SearchEds.new.search('popcorn', 'apibarton')
query = SearchEds.new.search('popcorn', 'apiwhatnot', '')
assert_equal('invalid credentials', query)
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/vcr_cassettes/no_results.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/vcr_cassettes/popcorn_articles.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/vcr_cassettes/popcorn_books.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/vcr_cassettes/popcorn_non_articles.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/vcr_cassettes/rollbar4.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/vcr_cassettes/rollbar4a.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/vcr_cassettes/rollbar8.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.