Module: Median::Primo::Search

Included in:
Median::Primo
Defined in:
lib/median/primo/search.rb

Instance Method Summary collapse

Instance Method Details

#search(search_request) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/median/primo/search.rb', line 4

def search(search_request)
  raise "the given parameter is not a valid search request" unless search_request.is_a?(SearchRequest)

  # Build the client
  wsdl_url      = "#{Median.config.primo_base_url}/PrimoWebServices/services/searcher?wsdl"
  wsdl_filename = File.join(Rails.root, 'tmp', 'cache', 'primo.wsdl.xml')
  unless File.exists?(wsdl_filename)
    File.open(wsdl_filename, 'wb') { |f| open(wsdl_url) { |u| f.write(u.read) } }
  end
  client = Savon::Client.new do |wsdl, http|
    wsdl.document = wsdl_filename
  end

  # Execute a search request
  begin
    response = client.request(:search_brief) do |soap|
      soap.body = {:query => search_request.to_xml.root.to_s}
    end
  rescue Exception => e
    raise Median::ConnectionError.new(e)
  end

  # Let's get the payload in XML and build the search result
  payload = Nokogiri::XML(response.to_xml).content
  SearchResult.new(payload, search_request)
end