Class: Suggester::Client
- Inherits:
-
Object
- Object
- Suggester::Client
- 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
-
#host ⇒ Object
Returns the value of attribute host.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#port ⇒ Object
Returns the value of attribute port.
Instance Method Summary collapse
-
#find(type, query, options = {}) ⇒ Object
Find returns an array of data returned for records that start with query string provided.
-
#initialize(options = {}) ⇒ Client
constructor
Create an instance of the client.
-
#match(type, query, options = {}) ⇒ Object
Match returns an array of data returned that is an exact match for the query string provided.
-
#refresh(type) ⇒ Object
Refresh tells the server to force a reload of its cached data.
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( = {}) @host = [:host] || DEFAULT_HOST @port = [:port] || DEFAULT_PORT @logger = [:logger] end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
14 15 16 |
# File 'lib/suggester/client.rb', line 14 def host @host end |
#logger ⇒ Object
Returns the value of attribute logger.
15 16 17 |
# File 'lib/suggester/client.rb', line 15 def logger @logger end |
#port ⇒ Object
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, = {}) fetch_response(build_url(type, "find", query, )) 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, = {}) fetch_response(build_url(type, "match", query, )) 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 |