Class: Asari

Inherits:
Object
  • Object
show all
Defined in:
lib/makasi/asari_patch.rb

Overview

Allow the new api syntax of version 2013-01-01

Defined Under Namespace

Classes: Collection

Instance Method Summary collapse

Instance Method Details

#search(term, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/makasi/asari_patch.rb', line 26

def search(term, options = {})
  return Asari::Collection.sandbox_fake if self.class.mode == :sandbox
  term,options = "",term if term.is_a?(Hash) and options.empty?

  if options[:filter]
    bq = boolean_query(options[:filter])
    bq = "(and '#{term.to_s.gsub("'", " ")}' #{bq})" if term.present?
  end
  page_size = options[:page_size].nil? ? 10 : options[:page_size].to_i

  url = "http://search-#{search_domain}.#{aws_region}.cloudsearch.amazonaws.com/#{api_version}/search"
  if options[:filter]
    url += "?q.parser=structured&q=#{CGI.escape(bq)}"
  else
    url += "?q=#{CGI.escape(term.to_s)}"
  end
  url += "&size=#{page_size}"
  url += "&return-fields=#{options[:return_fields].join ','}" if options[:return_fields]

  if options[:page]
    start = (options[:page].to_i - 1) * page_size
    url << "&start=#{start}"
  end

  if options[:rank]
    rank = normalize_rank(options[:rank])
    url << "&rank=#{rank}"
  end

  begin
    response = HTTParty.get(url)
  rescue Exception => e
    ae = Asari::SearchException.new("#{e.class}: #{e.message} (#{url})")
    ae.set_backtrace e.backtrace
    raise ae
  end

  unless response.response.code == "200"
    raise Asari::SearchException.new("#{response.response.code}: #{response.response.msg} (#{url})")
  end

  Asari::Collection.new(response, page_size)
end