Skip to content

Commit

Permalink
Merge 585c346 into ac2ad4a
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jun 27, 2018
2 parents ac2ad4a + 585c346 commit 55bf71b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
17 changes: 3 additions & 14 deletions app/models/blacklight/suggest_search.rb
@@ -1,10 +1,12 @@
# frozen_string_literal: true

module Blacklight
class SuggestSearch
attr_reader :request_params, :repository

##
# @param [Hash] params
# @param [Blacklight::AbstractRepository] repository
def initialize(params, repository)
@request_params = { q: params[:q] }
@repository = repository
Expand All @@ -15,20 +17,7 @@ def initialize(params, repository)
# Blacklight::Suggest::Response
# @return [Blacklight::Suggest::Response]
def suggestions
Blacklight::Suggest::Response.new suggest_results, request_params, suggest_handler_path
end

##
# Query the suggest handler using RSolr::Client::send_and_receive
# @return [RSolr::HashWithResponse]
def suggest_results
repository.connection.send_and_receive(suggest_handler_path, params: request_params)
end

##
# @return [String]
def suggest_handler_path
repository.blacklight_config.autocomplete_path
repository.suggestions(request_params)
end
end
end
14 changes: 14 additions & 0 deletions lib/blacklight/solr/repository.rb
Expand Up @@ -22,8 +22,16 @@ def search params = {}
send_and_receive blacklight_config.solr_path, params.reverse_merge(qt: blacklight_config.qt)
end

# @param [Hash] params
# @return [Blacklight::Suggest::Response]
def suggestions(request_params)
suggest_results = connection.send_and_receive(suggest_handler_path, params: request_params)
Blacklight::Suggest::Response.new suggest_results, request_params, suggest_handler_path
end

##
# Execute a solr query
# TODO: Make this private after we have a way to abstract admin/luke and ping
# @see [RSolr::Client#send_and_receive]
# @overload find(solr_path, params)
# Execute a solr query at the given path with the parameters
Expand Down Expand Up @@ -51,6 +59,12 @@ def send_and_receive(path, solr_params = {})

private

##
# @return [String]
def suggest_handler_path
blacklight_config.autocomplete_path
end

def build_connection
RSolr.connect(connection_config.merge(adapter: connection_config[:http_adapter]))
end
Expand Down
19 changes: 6 additions & 13 deletions spec/models/blacklight/suggest_search_spec.rb
Expand Up @@ -2,21 +2,14 @@

RSpec.describe Blacklight::SuggestSearch do
let(:params) { {q: 'test'} }
let(:suggest_path) { 'suggest' }
let(:connection) { instance_double(RSolr::Client, send_and_receive: 'sent')}
let(:repository) { instance_double(Blacklight::Solr::Repository, connection: connection) }
let(:response) { instance_double(Blacklight::Suggest::Response)}
let(:repository) { instance_double(Blacklight::Solr::Repository, suggestions: response) }
let(:suggest_search) { described_class.new(params, repository)}

describe '#suggestions' do
it 'returns a Blacklight::Suggest::Response' do
expect(suggest_search).to receive(:suggest_results).and_return([])
expect(suggest_search).to receive(:suggest_handler_path).and_return(suggest_path)
expect(suggest_search.suggestions).to be_an Blacklight::Suggest::Response
end
end
describe '#suggest_results' do
it 'calls send_and_recieve from a repository connection' do
expect(suggest_search).to receive(:suggest_handler_path).and_return(suggest_path)
expect(suggest_search.suggest_results).to eq 'sent'
it 'delegates to the repository' do
expect(repository).to receive(:suggestions).with(q: 'test').and_return(response)
expect(suggest_search.suggestions).to eq response
end
end
end

0 comments on commit 55bf71b

Please sign in to comment.