Class: Kasabi::Search::Client

Inherits:
BaseClient show all
Defined in:
lib/kasabi/api/search.rb

Overview

Client object for working with a Kasabi Search API

Instance Attribute Summary

Attributes inherited from BaseClient

#apikey, #client, #endpoint

Instance Method Summary collapse

Methods inherited from BaseClient

#client_options, #get, #post, #validate_response

Constructor Details

#initialize(endpoint, options = {}) ⇒ Client

Initialize the client to work with a specific endpoint

The options hash can contain the following values:

  • :apikey: required. apikey authorized to use the API

  • :client: HTTPClient object instance



13
14
15
# File 'lib/kasabi/api/search.rb', line 13

def initialize(endpoint, options={})
  super(endpoint, options)
end

Instance Method Details

#build_search_params(query, params) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/kasabi/api/search.rb', line 62

def build_search_params(query, params)
  if params != nil
    search_params = params.clone()
  else
    search_params = Hash.new  
  end
  search_params[:query] = query
  return search_params      
end

#facet(query, facets, params = Hash.new) ⇒ Object

The params hash can contain the following values:

  • :top: the maximum number of results to return for each facet

  • :output: the preferred response format, can be html or xml (the default)



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kasabi/api/search.rb', line 49

def facet(query, facets, params=Hash.new)
  if facets == nil or facets.empty?
    throw "Must supply at least one facet"
  end
  search_params = build_search_params( query, params)
  search_params[:fields] = facets.join(",")
  response = get(facet_url(), search_params)
  
  validate_response(response)
  
  return Kasabi::Search::Facet::Results.parse( response.content )  
end

#facet_urlObject



38
39
40
# File 'lib/kasabi/api/search.rb', line 38

def facet_url()
  return "#{@endpoint}/facet"
end

#search(query, params = nil) ⇒ Object

Perform a search

query

the query to perform.

params

additional query parameters (see below)

The params hash can contain the following values:

  • :max: The maximum number of results to return (default is 10)

  • :offset: Offset into the query results (for paging; default is 0)

  • :sort: ordered list of fields to be used when applying sorting



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kasabi/api/search.rb', line 26

def search(query, params=nil)
  search_params = build_search_params(query, params)
  search_params[:output] = "json"
  response = get(search_url(), search_params)
  
  validate_response(response)
  
  #TODO provide a better structure?
  return JSON.parse( response.content )
  
end

#search_urlObject



42
43
44
# File 'lib/kasabi/api/search.rb', line 42

def search_url()
  return "#{@endpoint}/search"
end