Class: Picky::Client

Inherits:
Object show all
Defined in:
lib/picky-client/client.rb,
lib/picky-client/client_index.rb,
lib/picky-client/client/active_record.rb

Direct Known Subclasses

TestClient

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

Class Method Summary collapse

Instance Method Summary collapse

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

#hostObject

Returns the value of attribute host.



51
52
53
# File 'lib/picky-client/client.rb', line 51

def host
  @host
end

#pathObject

Returns the value of attribute path.



51
52
53
# File 'lib/picky-client/client.rb', line 51

def path
  @path
end

#portObject

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 options = {}
  define_method :default_configuration do
    options
  end
end

.default_params(options = {}) ⇒ Object



87
88
89
90
91
92
# 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

Instance Method Details

#default_configurationObject



73
74
75
# File 'lib/picky-client/client.rb', line 73

def default_configuration
  {}
end

#default_paramsObject



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 options
  options = default_configuration.merge options

  @host = options[:host]
  @port = options[:port]
  @path = options[: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_sObject



130
131
132
# File 'lib/picky-client/client.rb', line 130

def to_s
  "#{self.class}(http://#{host}:#{port}#{path})"
end