Class: Uglier::Client

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

Constant Summary collapse

DEFAULT_CONFIG =
{
  :host => '127.0.0.1',
  :port => '8124',
  :flags => 0,
  :namespace => '',
  :namespace_delimeter => '.',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Client

Returns a new instance of Client.



17
18
19
20
# File 'lib/uglier/client.rb', line 17

def initialize(config = {})
  @config = DEFAULT_CONFIG.merge(config)
  reset_queue
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



15
16
17
# File 'lib/uglier/client.rb', line 15

def config
  @config
end

Instance Method Details

#flush_queueObject



43
44
45
46
# File 'lib/uglier/client.rb', line 43

def flush_queue
  log(@queue)
  reset_queue
end

#log(data) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/uglier/client.rb', line 22

def log(data)
  return if data.size == 0 # no data is being logged

  # Add namespace prefix, if it exists
  if (ns_prefix = namespace_prefix).size > 0
    data = data.inject({}) do |memo, (k, v)|
      memo[ns_prefix + k.to_s] = v
      memo
    end
  end

  socket.send(data.to_json, @config[:flags], @config[:host], @config[:port])
end

#namespace_prefixObject



52
53
54
55
# File 'lib/uglier/client.rb', line 52

def namespace_prefix
  ns = @config[:namespace].to_s
  ns.size > 0 ? (ns + @config[:namespace_delimeter].to_s) : ''
end

#queue(data) ⇒ Object



36
37
38
39
40
41
# File 'lib/uglier/client.rb', line 36

def queue(data)
  data.each do |k, v|
    @queue[k] ||= []
    @queue[k].concat(v.is_a?(Array) ? v : [v])
  end
end

#reset_queueObject



48
49
50
# File 'lib/uglier/client.rb', line 48

def reset_queue
  @queue = {}
end