Class: Blurrily::Client
- Inherits:
-
Object
- Object
- Blurrily::Client
- Defined in:
- lib/blurrily/client.rb
Constant Summary collapse
- Error =
Class.new(RuntimeError)
Instance Method Summary collapse
- #clear ⇒ Object
- #delete(ref) ⇒ Object
-
#find(needle, limit = nil) ⇒ Object
Find record references based on a given string (needle).
- #initialize(options = {}) ⇒ Client constructor
-
#put(needle, ref, weight = 0) ⇒ Object
Index a given record.
Constructor Details
#initialize(options = {}) ⇒ Client
Initialize a new Blurrily::Client connection to Server.
Examples
Blurrily::Client.new('127.0.0.1', 12021, 'location_en')
# => #<Blurrily::Client:0x007fcd0d33e708 @host="127.0.0.1", @port=12021, @db_name="location_en">
28 29 30 31 32 |
# File 'lib/blurrily/client.rb', line 28 def initialize( = {}) @host = .fetch(:host, DEFAULT_HOST) @port = .fetch(:port, DEFAULT_PORT) @db_name = .fetch(:db_name, DEFAULT_DATABASE) end |
Instance Method Details
#clear ⇒ Object
93 94 95 96 |
# File 'lib/blurrily/client.rb', line 93 def clear() send_cmd_and_get_results(['CLEAR', @db_name]) return end |
#delete(ref) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/blurrily/client.rb', line 86 def delete(ref) check_valid_ref(ref) cmd = ['DELETE', @db_name, ref] send_cmd_and_get_results(cmd) return end |
#find(needle, limit = nil) ⇒ Object
Find record references based on a given string (needle)
Examples
@client.find('London')
# => [[123,6,3],[124,5,3]...]
Note that unless modified, weight
is simply the string length.
52 53 54 55 56 57 58 59 |
# File 'lib/blurrily/client.rb', line 52 def find(needle, limit = nil) limit ||= LIMIT_DEFAULT check_valid_needle(needle) raise(ArgumentError, "LIMIT value must be in #{LIMIT_RANGE}") unless LIMIT_RANGE.include?(limit) cmd = ["FIND", @db_name, needle, limit] send_cmd_and_get_results(cmd).map(&:to_i).each_slice(3).to_a end |
#put(needle, ref, weight = 0) ⇒ Object
Index a given record.
Examples
@client.put('location_en', 'London', 123, 0)
# => OK
76 77 78 79 80 81 82 83 84 |
# File 'lib/blurrily/client.rb', line 76 def put(needle, ref, weight = 0) check_valid_needle(needle) check_valid_ref(ref) raise(ArgumentError, "WEIGHT value must be in #{WEIGHT_RANGE}") unless WEIGHT_RANGE.include?(weight) cmd = ["PUT", @db_name, needle, ref, weight] send_cmd_and_get_results(cmd) return end |