Class: Profit::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
# File 'lib/profit/client.rb', line 11

def initialize(options = {})
  @ctx            = options[:ctx]            || ZMQ::Context.new
  @server_address = options[:server_address] || "tcp://127.0.0.1:5556"
  @pending        = {}
end

Instance Attribute Details

#ctxObject

Returns the value of attribute ctx.



9
10
11
# File 'lib/profit/client.rb', line 9

def ctx
  @ctx
end

#pendingObject

Returns the value of attribute pending.



9
10
11
# File 'lib/profit/client.rb', line 9

def pending
  @pending
end

Instance Method Details

#socketObject



35
36
37
# File 'lib/profit/client.rb', line 35

def socket
  Thread.current[:socket] ||= @ctx.connect(:PUSH, @server_address)
end

#start(metric_key) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/profit/client.rb', line 17

def start(metric_key)
  pending_key = pending_key_for(Key.new(metric_key).to_s)
  metric      = Metric.new(metric_key: metric_key,
                           start_line: caller[0],
                           start_time: Time.now)

  pending[pending_key] = metric
end

#stop(metric_key) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/profit/client.rb', line 26

def stop(metric_key)
  pending_key          = pending_key_for(Key.new(metric_key).to_s)
  metric               = pending.delete pending_key
  metric.recorded_time = Time.now - metric.start_time
  metric.stop_line     = caller[0]

  socket.send(metric.to_json)
end