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
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ gem 'twitter-bootstrap-rails'
gem 'uglifier', '>= 1.3.0'

group :production do
gem 'dalli'
gem 'memcachier'
gem 'pg'
gem 'rails_12factor'
end
Expand Down
8 changes: 6 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ GEM
tins (>= 1.6.0, < 2)
crack (0.4.3)
safe_yaml (~> 1.0.0)
dalli (2.7.6)
debug_inspector (0.0.2)
devise (4.2.0)
bcrypt (~> 3.0)
Expand Down Expand Up @@ -135,6 +136,7 @@ GEM
nokogiri (>= 1.5.9)
mail (2.6.4)
mime-types (>= 1.16, < 4)
memcachier (0.0.2)
memoist (0.15.0)
method_source (0.8.2)
mime-types (3.1)
Expand Down Expand Up @@ -252,7 +254,7 @@ GEM
sprockets (3.7.0)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.1.1)
sprockets-rails (3.2.0)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
Expand All @@ -279,7 +281,7 @@ GEM
unf (0.1.4)
unf_ext
unf_ext (0.0.7.2)
unicode-display_width (1.1.0)
unicode-display_width (1.1.1)
vcr (3.0.3)
warden (1.2.6)
rack (>= 1.0)
Expand All @@ -305,13 +307,15 @@ DEPENDENCIES
annotate
byebug
coveralls
dalli
devise
google-api-client
hashie
http
http_logger
jquery-rails
less-rails
memcachier
minitest-rails
minitest-rails-capybara
minitest-reporters
Expand Down
3 changes: 2 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
}
},
"addons": [
"heroku-postgresql"
"heroku-postgresql",
"memcachier"
],
"buildpacks": [
{
Expand Down
23 changes: 20 additions & 3 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,29 @@ def index
end

def bento
@results = SearchEds.new.search(params[:q])

# TODO: it's generally bad form to have multiple instance Variables
# acessible in the views. However, this needs a refactor to allow for
# async searching anyway so I'm violating that rule for now with the
# anticipation of the async work.
@moar_results = SearchGoogle.new.search(params[:q])

today = Time.zone.today.strftime('%Y%m%d')

# NOTE: The cache keys used below use a combination of the api endpoint
# name, the search parameter, and today's date to allow us to cache calls
# for the current date without ever worrying about expiring caches.
# Instead, we'll rely on the cache itself to expire the oldest cached
# items when necessary.

@articles = Rails.cache.fetch("articles_#{params[:q]}_#{today}") do
SearchEds.new.search(params[:q], ENV['EDS_NO_ALEPH_PROFILE'])
end

@books = Rails.cache.fetch("books_#{params[:q]}_#{today}") do
SearchEds.new.search(params[:q], ENV['EDS_ALEPH_PROFILE'])
end

@google = Rails.cache.fetch("google_#{params[:q]}_#{today}") do
SearchGoogle.new.search(params[:q])
end
end
end
15 changes: 4 additions & 11 deletions app/models/search_eds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,20 @@ class SearchEds
attr_reader :results

EDS_URL = ENV['EDS_URL'].freeze
EDS_NO_ALEPH_PROFILE = ENV['EDS_NO_ALEPH_PROFILE'].freeze
EDS_ALEPH_PROFILE = ENV['EDS_ALEPH_PROFILE'].freeze
RESULTS_PER_BOX = ENV['RESULTS_PER_BOX'] || 3

def initialize
@auth_token = uid_auth
@results = {}
end

def search(term)
def search(term, profile)
return 'invalid credentials' unless @auth_token
@session_key = create_session(EDS_NO_ALEPH_PROFILE) if @auth_token
@results['raw_articles'] = search_filtered(term)
@session_key = create_session(profile) if @auth_token
@results["raw_#{profile}"] = search_filtered(term)
end_session

@session_key = create_session(EDS_ALEPH_PROFILE) if @auth_token
@results['raw_books'] = search_filtered(term)
end_session

@results['articles'] = to_result(@results['raw_articles'])
@results['books'] = to_result(@results['raw_books'])
@results[profile.to_s] = to_result(@results["raw_#{profile}"])
@results
end

Expand Down
18 changes: 9 additions & 9 deletions app/views/search/bento.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<div class="col-sm-4">
<h2>Articles and Stuff</h2>

<% @results['articles']['results'].each do |result| %>
<% @articles['apinoaleph']['results'].each do |result| %>
<%= render partial: "result", locals: {result: result} %>
<% end %>

<% if @results['articles']['total'] > 0 %>
<%= link_to("View all #{@results['articles']['total']} like this.",
<% if @articles['apinoaleph']['total'] > 0 %>
<%= link_to("View all #{@articles['apinoaleph']['total']} like this.",
"#{ENV['EDS_ALEPH_URI']}#{params[:q]}") %>
<% else %>
No results found.
Expand All @@ -18,12 +18,12 @@
<div class="col-sm-4">
<h2>Books and Stuff</h2>

<% @results['books']['results'].each do |result| %>
<% @books['apibarton']['results'].each do |result| %>
<%= render partial: "result", locals: {result: result} %>
<% end %>

<% if @results['books']['total'] > 0 %>
<%= link_to("View all #{@results['books']['total']} like this.",
<% if @books['apibarton']['total'] > 0 %>
<%= link_to("View all #{@books['apibarton']['total']} like this.",
"#{ENV['EDS_ALEPH_URI']}#{params[:q]}") %>
<% else %>
No results found.
Expand All @@ -33,12 +33,12 @@
<div class="col-sm-4">
<h2>Website and Guides</h2>

<% @moar_results['results'].each do |result| %>
<% @google['results'].each do |result| %>
<%= render partial: "result", locals: {result: result} %>
<% end %>

<% if @moar_results['total'] > 0 %>
<%= link_to("View all #{@moar_results['total']} like this.",
<% if @google['total'] > 0 %>
<%= link_to("View all #{@google['total']} like this.",
"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]}") %>
<% end %>
</div>
4 changes: 2 additions & 2 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
# config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
config.force_ssl = true

# Use the lowest log level to ensure availability of diagnostic information
# when problems arise.
Expand All @@ -50,7 +50,7 @@
config.log_tags = [ :request_id ]

# Use a different cache store in production.
# config.cache_store = :mem_cache_store
config.cache_store = :dalli_store

# Use a real queuing backend for Active Job (and separate queues per environment)
# config.active_job.queue_adapter = :resque
Expand Down
1 change: 1 addition & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.cache_store = :null_store

# Raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = false
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/search_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SearchControllerTest < ActionController::TestCase
end

test 'should get bento' do
VCR.use_cassette('valid search and credentials',
VCR.use_cassette('integrated search',
allow_playback_repeats: true) do
get :bento, params: { q: 'popcorn' }
assert_response :success
Expand Down
65 changes: 32 additions & 33 deletions test/models/search_eds_test.rb
Original file line number Diff line number Diff line change
@@ -1,86 +1,85 @@
require 'test_helper'

class SearchEdsTest < ActiveSupport::TestCase
test 'valid search with valid credentials returns results' do
VCR.use_cassette('valid eds search and credentials',
test 'can search articles' do
VCR.use_cassette('popcorn articles',
allow_playback_repeats: true) do
query = SearchEds.new.search('popcorn')
assert_equal(39_784, query['articles']['total'])
query = SearchEds.new.search('popcorn', 'apinoaleph')
assert_equal(39_812, query['apinoaleph']['total'])
end
end

test 'can search books' do
VCR.use_cassette('popcorn non articles',
allow_playback_repeats: true) do
query = SearchEds.new.search('popcorn', 'apibarton')
assert_equal(79, query['apibarton']['total'])
end
end

test 'invalid credentials' do
VCR.use_cassette('invalid credentials') do
query = SearchEds.new.search('popcorn')
query = SearchEds.new.search('popcorn', 'apibarton')
assert_equal('invalid credentials', query)
end
end

test 'normalized articles have expected title' do
VCR.use_cassette('valid eds search and credentials',
VCR.use_cassette('popcorn articles',
allow_playback_repeats: true) do
query = SearchEds.new.search('popcorn')
query = SearchEds.new.search('popcorn', 'apinoaleph')
assert_equal(
'Sowing time of popcorn during the summer harvest under',
query['articles']['results'].first.title.split[0...9].join(' ')
query['apinoaleph']['results'].first.title.split[0...9].join(' ')
)
end
end

test 'normalized articles have expected year' do
VCR.use_cassette('valid eds search and credentials',
VCR.use_cassette('popcorn articles',
allow_playback_repeats: true) do
query = SearchEds.new.search('popcorn')
assert_equal('2015', query['articles']['results'].first.year)
query = SearchEds.new.search('popcorn', 'apinoaleph')
assert_equal('2015', query['apinoaleph']['results'].first.year)
end
end

test 'normalized articles have expected url' do
VCR.use_cassette('valid eds search and credentials',
VCR.use_cassette('popcorn articles',
allow_playback_repeats: true) do
query = SearchEds.new.search('popcorn')
query = SearchEds.new.search('popcorn', 'apinoaleph')
assert_equal(
'http://search.ebscohost.com/login.aspx?direct=true&site=eds-live&db=edsihs&AN=221456993819438',
query['articles']['results'].first.url
query['apinoaleph']['results'].first.url
)
end
end

test 'normalized articles have expected type' do
VCR.use_cassette('valid eds search and credentials',
VCR.use_cassette('popcorn articles',
allow_playback_repeats: true) do
query = SearchEds.new.search('popcorn')
assert_equal('Academic Journal', query['articles']['results'].first.type)
query = SearchEds.new.search('popcorn', 'apinoaleph')
assert_equal('Academic Journal',
query['apinoaleph']['results'].first.type)
end
end

test 'normalized articles have expected authors' do
VCR.use_cassette('valid eds search and credentials',
VCR.use_cassette('popcorn articles',
allow_playback_repeats: true) do
query = SearchEds.new.search('popcorn')
query = SearchEds.new.search('popcorn', 'apinoaleph')
assert_equal(
'Marques, Odair Jose',
query['articles']['results'].first.authors.first
query['apinoaleph']['results'].first.authors.first
)
assert_equal(7, query['articles']['results'].first.authors.count)
assert_equal(7, query['apinoaleph']['results'].first.authors.count)
end
end

test 'searches with no results do not error' do
VCR.use_cassette('no results',
allow_playback_repeats: true) do
query = SearchEds.new.search('popcornandorangejuice')
assert_equal(0, query['articles']['total'])
assert_equal(0, query['books']['total'])
end
end

test 'searches with article results and no book results' do
VCR.use_cassette('article results with no book results',
allow_playback_repeats: true) do
query = SearchEds.new.search('"pokemon go"')
assert_equal(4574, query['articles']['total'])
assert_equal(0, query['books']['total'])
query = SearchEds.new.search('popcornandorangejuice', 'apinoaleph')
assert_equal(0, query['apinoaleph']['total'])
end
end
end
Loading