class Picky::Client
Attributes
host[RW]
path[RW]
port[RW]
Public Class Methods
default_configuration(options = {})
click to toggle source
# File lib/picky-client/client.rb, line 76 def self.default_configuration options = {} define_method :default_configuration do options end end
default_params(options = {})
click to toggle source
# File lib/picky-client/client.rb, line 87 def self.default_params options = {} options.stringify_keys! if options.respond_to?(:stringify_keys!) define_method :default_params do options end end
new(hash_or_uri = {})
click to toggle source
# File lib/picky-client/client.rb, line 53 def initialize hash_or_uri = {} if hash_or_uri.respond_to? :to_hash initialize_from_hash hash_or_uri else initialize_from_uri hash_or_uri end end
Public Instance Methods
default_configuration()
click to toggle source
# File lib/picky-client/client.rb, line 73 def default_configuration {} end
default_params()
click to toggle source
# File lib/picky-client/client.rb, line 84 def default_params {} end
defaultize(params = {})
click to toggle source
Merges the given params, overriding the defaults.
# File lib/picky-client/client.rb, line 96 def defaultize params = {} default_params.merge params end
initialize_from_hash(options)
click to toggle source
# File lib/picky-client/client.rb, line 66 def initialize_from_hash options options = default_configuration.merge options @host = options[:host] @port = options[:port] @path = options[:path] end
initialize_from_uri(uri)
click to toggle source
# File lib/picky-client/client.rb, line 60 def initialize_from_uri uri initialize_from_hash :host => uri.host, :port => uri.port, :path => uri.path end
remove(index_name, data)
click to toggle source
Removes an item from the index.
Parameters:
* index_name: An index that exists in the Picky server. * data: A hash in the form of { :id => 1234 }.
# File lib/picky-client/client_index.rb, line 22 def remove index_name, data send_off Net::HTTP::Delete.new(self.path), index_name, data end
replace(index_name, data)
click to toggle source
Replaces an item in the index (adds it if not indexed yet).
Parameters:
* index_name: An index that exists in the Picky server. * data: A hash in the form of { :id => 1234, :attr1 => 'attr1', :attr2 => 'attr2', ... }.
# File lib/picky-client/client_index.rb, line 12 def replace index_name, data send_off Net::HTTP::Put.new(self.path), index_name, data end
search(query, params = {})
click to toggle source
# File lib/picky-client/client.rb, line 105 def search query, params = {} return {} unless query && !query.empty? ::Yajl::Parser.parse search_unparsed(query, params), @@parser_options end
search_unparsed(query, params = {})
click to toggle source
Use this method for live queries – they can pass the JSON string with the results through without parsing.
# File lib/picky-client/client.rb, line 113 def search_unparsed query, params = {} return '' unless query && !query.empty? send_search params.merge :query => query end
send_off(request, index_name, data = {})
click to toggle source
Sends a request to the Picky
server.
Note: Data is JSON encoded.
# File lib/picky-client/client_index.rb, line 30 def send_off request, index_name, data = {} request.form_data = { :index => index_name, :data => ActiveSupport::JSON.encode(data) } Net::HTTP.new(self.host, self.port).start { |http| http.request request } end
send_search(params = {})
click to toggle source
Sends a search to the configured address.
Note: For live queries, parsing is actually not really necessary.
# File lib/picky-client/client.rb, line 123 def send_search params = {} params = defaultize params ::Net::HTTP.get self.host, "#{self.path}?#{params.to_query}", self.port end
to_s()
click to toggle source
# File lib/picky-client/client.rb, line 130 def to_s "#{self.class}(http://#{host}:#{port}#{path})" end