require 'json'
require 'restclient'
module RailsConnector
# This class provides a basic implementation for accessing the search using the cms api.
# It can be activated by making it the superclass of SearchRequest.
# It should be customized by subclassing.
class CmsApiSearchRequest
# Takes +query_string+ and +options+ for accessing Cms Api Search.
#
# +options+ is a hash and may include:
#
# :limit:: The maximum number of hits
# :offset:: The search offset
# :url:: The URL to the CMS API search
# :login:: The login for the CMS API search
# :api_key:: The api_key for the CMS API search
def initialize(query_string, options = {})
@query_string = query_string
@options = Configuration.cms_api_options.merge(options)
end
# Accesses Cms Api Search using #query and fetches search hits.
def fetch_hits
headers = {
:content_type => :json,
:accept => :json,
:params => {
:query => @query_string,
:offset => @options[:offset],
:limit => @options[:limit],
},
}
headers[:host] = @options[:http_host] if @options[:http_host].present?
hits = JSON.parse(
RestClient::Request.execute({
:method => :get,
:url => url,
:user => @options[:login],
:password => @options[:api_key],
:headers => headers,
})
)
result = SES::SearchResult.new(hits['total'])
hits['results'].each do |hit|
hard_coded_score = 1
result << SES::Hit.new(hit, hard_coded_score, nil)
end
result
end
private
def url
"#{@options[:url]}/revisions/#{Revision.current.id}/objs/search"
end
end
end