Class: Picky::Client
- Defined in:
- lib/picky-client/client.rb,
lib/picky-client/client_index.rb,
lib/picky-client/client/active_record.rb
Direct Known Subclasses
Defined Under Namespace
Classes: ActiveRecord
Constant Summary collapse
- @@parser_options =
Searches the index. Use this method.
Returns a hash. Extend with Convenience.
{ :symbolize_keys => true }
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#path ⇒ Object
Returns the value of attribute path.
-
#port ⇒ Object
Returns the value of attribute port.
Class Method Summary collapse
Instance Method Summary collapse
- #default_configuration ⇒ Object
- #default_params ⇒ Object
-
#defaultize(params = {}) ⇒ Object
Merges the given params, overriding the defaults.
-
#initialize(hash_or_uri = {}) ⇒ Client
constructor
A new instance of Client.
- #initialize_from_hash(options) ⇒ Object
- #initialize_from_uri(uri) ⇒ Object
-
#remove(index_name, data) ⇒ Object
Removes an item from the index.
-
#replace(index_name, data) ⇒ Object
Replaces an item in the index (adds it if not indexed yet).
- #search(query, params = {}) ⇒ Object
-
#search_unparsed(query, params = {}) ⇒ Object
Use this method for live queries – they can pass the JSON string with the results through without parsing.
-
#send_off(request, index_name, data = {}) ⇒ Object
Sends a request to the Picky server.
-
#send_search(params = {}) ⇒ Object
Sends a search to the configured address.
- #to_s ⇒ Object
Constructor Details
#initialize(hash_or_uri = {}) ⇒ Client
Returns a new instance of Client.
53 54 55 56 57 58 59 |
# 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 |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
51 52 53 |
# File 'lib/picky-client/client.rb', line 51 def host @host end |
#path ⇒ Object
Returns the value of attribute path.
51 52 53 |
# File 'lib/picky-client/client.rb', line 51 def path @path end |
#port ⇒ Object
Returns the value of attribute port.
51 52 53 |
# File 'lib/picky-client/client.rb', line 51 def port @port end |
Class Method Details
.default_configuration(options = {}) ⇒ Object
76 77 78 79 80 |
# File 'lib/picky-client/client.rb', line 76 def self.default_configuration = {} define_method :default_configuration do end end |
.default_params(options = {}) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/picky-client/client.rb', line 87 def self.default_params = {} .stringify_keys! if .respond_to?(:stringify_keys!) define_method :default_params do end end |
Instance Method Details
#default_configuration ⇒ Object
73 74 75 |
# File 'lib/picky-client/client.rb', line 73 def default_configuration {} end |
#default_params ⇒ Object
84 85 86 |
# File 'lib/picky-client/client.rb', line 84 def default_params {} end |
#defaultize(params = {}) ⇒ Object
Merges the given params, overriding the defaults.
96 97 98 |
# File 'lib/picky-client/client.rb', line 96 def defaultize params = {} default_params.merge params end |
#initialize_from_hash(options) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/picky-client/client.rb', line 66 def initialize_from_hash = default_configuration.merge @host = [:host] @port = [:port] @path = [:path] end |
#initialize_from_uri(uri) ⇒ Object
60 61 62 63 64 65 |
# 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) ⇒ Object
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 }.
22 23 24 |
# 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) ⇒ Object
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', ... }.
12 13 14 |
# 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 = {}) ⇒ Object
105 106 107 108 109 |
# 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 = {}) ⇒ Object
Use this method for live queries – they can pass the JSON string with the results through without parsing.
113 114 115 116 117 |
# 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 = {}) ⇒ Object
Sends a request to the Picky server.
Note: Data is JSON encoded.
30 31 32 33 |
# 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 = {}) ⇒ Object
Sends a search to the configured address.
Note: For live queries, parsing is actually not really necessary.
123 124 125 126 |
# 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 ⇒ Object
130 131 132 |
# File 'lib/picky-client/client.rb', line 130 def to_s "#{self.class}(http://#{host}:#{port}#{path})" end |