Class: Blurrily::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/blurrily/client.rb

Constant Summary collapse

Error =
Class.new(RuntimeError)

Instance Method Summary collapse

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">

Parameters:

  • host

    IP Address or FQDN of the Blurrily::Server. Defaults to Blurrily::DEFAULT_HOST.

  • port

    Port Blurrily::Server is listening on. Defaults to Blurrily::DEFAULT_PORT.

  • db_name

    Name of the data store being targeted. Defaults to Blurrily::DEFAULT_DATABASE.



28
29
30
31
32
# File 'lib/blurrily/client.rb', line 28

def initialize(options = {})
  @host    = options.fetch(:host,     DEFAULT_HOST)
  @port    = options.fetch(:port,     DEFAULT_PORT)
  @db_name = options.fetch(:db_name,  DEFAULT_DATABASE)
end

Instance Method Details

#clearObject



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.

Parameters:

  • needle

    The string you're searching for matches on. Must not contain tabs. Required

  • limit (defaults to: nil)

    Limit the number of results retruned (default: 10). Must be numeric. Optional

Raises:

  • (ArgumentError)


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

Parameters:

  • db_name

    The name of the data store being targeted. Required

  • needle

    The string you wish to index. Must not contain tabs. Required

  • ref

    The indentifying value of the record being indexed. Must be numeric. Required

  • weight (defaults to: 0)

    Weight of this particular reference. Default 0. Don't change unless you know what you're doing. Optional.

Raises:

  • (ArgumentError)


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