Class: EventMachine::StatHat

Inherits:
Object
  • Object
show all
Defined in:
lib/em-stathat.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

@@config =
Config.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri = 'http://api.stathat.com') ⇒ StatHat

Returns a new instance of StatHat.

Raises:

  • (RuntimeError)


30
31
32
33
# File 'lib/em-stathat.rb', line 30

def initialize(base_uri = 'http://api.stathat.com')
  raise(RuntimeError, "You must configure EM::StatHat before using it!") unless self.class.configured?
  @base_uri = base_uri
end

Class Method Details

.config(&block) ⇒ Object

Configure EM::StatHat

Examples:

EM::StatHat.config do |c|
  c.ukey  = 'Qu9sj34ncWXi0e83'
  u.email = '[email protected]'
end


17
18
19
20
21
22
23
# File 'lib/em-stathat.rb', line 17

def config(&block)
  if block_given?
    @@config.tap(&block)
  else
    @@config
  end
end

.configured?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/em-stathat.rb', line 25

def configured?
  !!config.ukey && !!config.email
end

Instance Method Details

#configObject



35
# File 'lib/em-stathat.rb', line 35

def config; self.class.config; end

#ez_count(stat_name, count = 1) ⇒ Object

Increment a counter via the EZ api



66
67
68
# File 'lib/em-stathat.rb', line 66

def ez_count(stat_name, count = 1)
  ez(stat_name, {:count => count})
end

#ez_value(stat_name, value) ⇒ Object

Record a value via the EZ api



61
62
63
# File 'lib/em-stathat.rb', line 61

def ez_value(stat_name, value)
  ez(stat_name, {:value => value})
end

#time(name, opts = {}) ⇒ Object

Time a block of code and send the duration as a value

Examples:

StatHat.new.time('some identifying name') do
  # code
end

Parameters:

  • name (String)

    the name of the stat

  • opts (Hash) (defaults to: {})

    a hash of options

  • [Symbol] (Hash)

    a customizable set of options



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/em-stathat.rb', line 47

def time(name, opts = {})
  opts[:ez] ||= true

  start = Time.now
  yield if block_given?

  if opts[:ez] == true
    ez_value(name, (Time.now - start))
  else
    value(name, (Time.now - start))
  end
end