module Statsman::Accountable

Include this module into your ActiveRecord models to get some convenience methods for sending stats

Public Instance Methods

statsman_change(change_type, mrr_difference, meta = {}) click to toggle source

change_type can be anything you want, as it's just used as a part of the stat key name. Some common types could be:

referral, upgrade, downgrade, signup, 
conversion, cancel, lapse, unlapse

mrr_difference: in cents meta: optional hash of other data

# File lib/statsman/accountable.rb, line 25
def statsman_change(change_type, mrr_difference, meta = {})
  class_name = self.class.name.downcase

  Statsman.send_value "#{class_name}_change_#{change_type}",
                      mrr_difference,
                      set_class_id(meta)
end
statsman_counter(key, value, meta = {}) click to toggle source
# File lib/statsman/accountable.rb, line 9
def statsman_counter(key, value, meta = {})
  Statsman.send_counter key, value, set_class_id(meta)
end
statsman_payment(successful_payment, amount, meta = {}) click to toggle source
# File lib/statsman/accountable.rb, line 33
def statsman_payment(successful_payment, amount, meta = {})
  Statsman.send_value "payment_#{successful_payment ? "success" : "failed"}",
                      amount,
                      set_class_id(meta)
end
statsman_value(key, value, meta = {}) click to toggle source
# File lib/statsman/accountable.rb, line 13
def statsman_value(key, value, meta = {})
  Statsman.send_value key, value, set_class_id(meta)
end

Protected Instance Methods

set_class_id(meta) click to toggle source

Set a key named by this class's name with the value of this record's ID. For example, if we're using this in the context of an Account instance, we'll set “account_id” => self.id into the meta hash argument if it doesn't exist already.

# File lib/statsman/accountable.rb, line 46
def set_class_id(meta)
  key_name = "#{self.class.name.downcase}_id"
  new_meta = meta.dup
  new_meta.stringify_keys!

  unless new_meta.has_key?(key_name)
    new_meta[key_name] = id
  end

  new_meta
end