Module: Blamescope

Defined in:
lib/blamescope.rb,
lib/blamescope/server.rb,
lib/blamescope/version.rb,
lib/blamescope/trackable.rb,
lib/blamescope/middleware.rb

Overview

Trackable module

Author:

  • Volodymyr Ladnik

Defined Under Namespace

Modules: Trackable Classes: Middleware, Server

Constant Summary collapse

VERSION =
"0.0.4"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.exchangeObject

Returns the value of attribute exchange.



14
15
16
# File 'lib/blamescope.rb', line 14

def exchange
  @exchange
end

Class Method Details

.configure(config) ⇒ Object

Configure Blamescope

Parameters:

  • config (Hash)

Options Hash (config):

  • :rabbit (String)

    Rabbit connection in format amqp://user:pass@host:port/vhost



26
27
28
# File 'lib/blamescope.rb', line 26

def configure(config)
  self.exchange = rabbit_connection(config[:rabbit])[:exchange]
end

.emit(attrs) ⇒ Object

Send event

Parameters:

  • attrs (type)

    event attributes

Options Hash (attrs):

  • :model (String)

    current class name

  • :action (String)

    current method

  • :attrs (String)

    object attributes



50
51
52
53
54
55
# File 'lib/blamescope.rb', line 50

def emit(attrs)
  attrs[:email] ||= user
  exchange.publish(attrs.to_json)
rescue Bunny::ConnectionClosedError
  puts "Bunny is down: couldn't send #{attrs[:action]} action for #{attrs[:model]} model"
end

.included(base) ⇒ Object



16
17
18
19
# File 'lib/blamescope.rb', line 16

def included(base)
  base.extend(Trackable)
  base.class.extend(Trackable)
end

.rabbit_connection(config) ⇒ Hash

Create RabbitMQ connection

Parameters:

  • config (String)

    Rabbit connection in format amqp://user:pass@host:port/vhost

Returns:

  • (Hash)

    rabbit connection, channel and exchange



62
63
64
65
66
67
68
# File 'lib/blamescope.rb', line 62

def rabbit_connection(config)
  conn = Bunny.new(config)
  conn.start
  channel = conn.create_channel
  exchange = channel.fanout("blamescope")
  {conn: conn, channel: channel, exchange: exchange}
end

.userString

Get current user

Returns:

  • (String)

    user email



34
35
36
37
38
39
40
41
# File 'lib/blamescope.rb', line 34

def user
  env = Thread.current[:env]
  if env && env['warden']
    env['warden'].user.email
  else
     "#{ENV['USER']}@#{Socket.gethostname}"
  end
end