Class: Suggester::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/suggester/client.rb

Overview

Simple client to access a Suggester::Server instance

Constant Summary collapse

DEFAULT_HOST =

server uses sinatra with default port 17500

"localhost"
DEFAULT_PORT =
17500

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Create an instance of the client



22
23
24
25
26
# File 'lib/suggester/client.rb', line 22

def initialize(options = {})
  @host = options[:host] || DEFAULT_HOST
  @port = options[:port] || DEFAULT_PORT
  @logger = options[:logger]
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



14
15
16
# File 'lib/suggester/client.rb', line 14

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



15
16
17
# File 'lib/suggester/client.rb', line 15

def logger
  @logger
end

#portObject

Returns the value of attribute port.



14
15
16
# File 'lib/suggester/client.rb', line 14

def port
  @port
end

Instance Method Details

#find(type, query, options = {}) ⇒ Object

Find returns an array of data returned for records that start with query string provided

Parameters

  • type - The name of the handler like “book”

  • query - The search term you are searching on

  • options - Hash of optionally parameters which are passed as query parameters. Includes the :limit option.

Examples

# find exact matches
client = Suggester::Client.new
client.match("book", "A Tale")                # returns all books that match "A Tale of Two Cities"
client.match("book", "A Tale", :limit => 1)   # return the first match for "A Tale of Two Cities"


64
65
66
# File 'lib/suggester/client.rb', line 64

def find(type, query, options = {})
  fetch_response(build_url(type, "find", query, options))
end

#match(type, query, options = {}) ⇒ Object

Match returns an array of data returned that is an exact match for the query string provided

Parameters

  • type - The name of the handler like “book”

  • query - The search term you are matching on

  • options - Hash of optionally parameters which are passed as query parameters. Includes the :limit option.

Examples

# find exact matches
client = Suggester::Client.new
client.match("book", "A Tale of Two Cities")                # returns all books that match "A Tale of Two Cities"
client.match("book", "A Tale of Two Cities", :limit => 1)   # return the first match for "A Tale of Two Cities"


44
45
46
# File 'lib/suggester/client.rb', line 44

def match(type, query, options = {})
  fetch_response(build_url(type, "match", query, options))
end

#refresh(type) ⇒ Object

Refresh tells the server to force a reload of its cached data

Parameters

  • type - The name of the handler to refresh



73
74
75
76
77
# File 'lib/suggester/client.rb', line 73

def refresh(type)
  url = "http://#{@host}:#{@port}/#{type}/refresh.json"
  response = fetch_response(url)
  return response.is_a?(Hash) && response["return"] == "OK"
end