class Ravelin::Event

Attributes

name[RW]
payload[RW]
timestamp[RW]

Public Class Methods

new(name:, payload:, timestamp: nil) click to toggle source
# File lib/ravelin/event.rb, line 5
def initialize(name:, payload:, timestamp: nil)
  @internal_name = name
  @name = convert_event_name(name)
  @payload = convert_to_ravelin_objects(payload)
  @timestamp = timestamp.nil? ? Time.now.to_i : convert_to_epoch(timestamp)

  validate_top_level_payload_params
end

Public Instance Methods

format_timestamp(timestamp) click to toggle source
# File lib/ravelin/event.rb, line 72
def format_timestamp(timestamp)
  case @internal_name
  when :ato_login
    timestamp * 1000
  when :ato_reclaim
    Time.at(timestamp).utc.to_datetime.to_s
  else
    timestamp
  end
end
object_classes() click to toggle source
# File lib/ravelin/event.rb, line 30
def object_classes
  {
    ato_login:                AtoLogin,
    authentication_mechanism: AuthenticationMechanism,
    chargeback:               Chargeback,
    customer:                 Customer,
    ato_reclaim:              AtoReclaim,
    device:                   Device,
    location:                 Location,
    login:                    Login,
    order:                    Order,
    password:                 Password,
    social:                   AuthenticationMechanisms::Social,
    sms_code:                 AuthenticationMechanisms::SmsCode,
    magic_link:               AuthenticationMechanisms::MagicLink,
    payment_method:           PaymentMethod,
    payment_methods:          PaymentMethods,
    supplier:                 Supplier,
    voucher_redemption:       VoucherRedemption,
    transaction:              transaction,
    label:                    Label,
    voucher:                  Voucher,
  }
end
plural_object_classes() click to toggle source
# File lib/ravelin/event.rb, line 55
def plural_object_classes
  {
    transactions: transaction
  }
end
serializable_hash() click to toggle source
# File lib/ravelin/event.rb, line 14
def serializable_hash
  payload_hash = hash_map(payload) do |k, v|
    k = Ravelin.camelize(k)

    if v.is_a?(Ravelin::RavelinObject)
      [k, v.serializable_hash]
    elsif v.is_a?(Array)
      [k, v.map { |elem| elem.respond_to?(:serializable_hash) ? elem.serializable_hash : elem }]
    else
      [k, v]
    end
  end

  payload_hash.merge('timestamp' => format_timestamp(timestamp))
end
transaction() click to toggle source
# File lib/ravelin/event.rb, line 61
def transaction
  case name
  when :pretransaction
    PreTransaction
  when :checkout
    CheckoutTransaction
  else
    Transaction
  end
end

Private Instance Methods

convert_event_name(str) click to toggle source
# File lib/ravelin/event.rb, line 162
def convert_event_name(str)
  underscore_mapping = {
    payment_method: :paymentmethod,
    pre_transaction: :pretransaction,
    ato_login: :login,
    ato_reclaim: :reclaim,
    label: 'label/customer'.to_sym
  }

  underscore_mapping.fetch(str.to_sym, str.to_sym)
end
convert_to_epoch(val) click to toggle source
# File lib/ravelin/event.rb, line 137
def convert_to_epoch(val)
  if val.is_a?(Time) || val.is_a?(Date) || val.is_a?(DateTime)
    Ravelin.datetime_to_epoch(val)
  elsif val.is_a?(Integer)
    val
  else
    raise TypeError, 'timestamp requires a Time or epoch Integer'
  end
end
convert_to_ravelin_objects(payload) click to toggle source
# File lib/ravelin/event.rb, line 147
def convert_to_ravelin_objects(payload)
  hash_map(payload) do |k, v|
    k = k.to_sym
    v = Ravelin.convert_ids_to_strings(k, v)

    if (v.is_a?(Hash) || v.is_a?(Array)) && (klass = object_classes[k])
      [k, klass.new(v)]
    elsif v.is_a?(Array) && (klass = plural_object_classes[k])
      [k, v.map { |elem| klass.new(elem) }]
    else
      [k, v]
    end
  end
end
hash_map(hash, &block) click to toggle source
# File lib/ravelin/event.rb, line 174
def hash_map(hash, &block)
  Hash[hash.map { |k, v| block.call(k, v) }]
end
payload_customer_reference_present?() click to toggle source
# File lib/ravelin/event.rb, line 133
def payload_customer_reference_present?
  payload.keys.any? { |k| %i[customer_id temp_customer_id].include? k }
end
validate_customer_id_presence_on(*events) click to toggle source
# File lib/ravelin/event.rb, line 109
def validate_customer_id_presence_on(*events)
  if events.include?(name) && !payload_customer_reference_present?
    raise ArgumentError, 'payload missing customer_id or temp_customer_id parameter'
  end
end
validate_payload_inclusion_of(*required_keys) click to toggle source
# File lib/ravelin/event.rb, line 115
def validate_payload_inclusion_of(*required_keys)
  missing = required_keys - payload.keys

  if missing.any?
    raise ArgumentError, "payload missing parameters: #{missing.join(', ')}"
  end
end
validate_payload_must_include_one_of(*mutually_exclusive_keys) click to toggle source
# File lib/ravelin/event.rb, line 123
def validate_payload_must_include_one_of(*mutually_exclusive_keys)
  intersection = mutually_exclusive_keys & payload.keys

  if intersection.size > 1
    raise ArgumentError, "parameters are mutally exclusive: #{mutually_exclusive_keys.join(', ')}"
  elsif intersection.empty?
    raise ArgumentError, "payload must include one of: #{mutually_exclusive_keys.join(', ')}"
  end
end
validate_top_level_payload_params() click to toggle source
# File lib/ravelin/event.rb, line 85
def validate_top_level_payload_params
  validate_customer_id_presence_on :order, :paymentmethod,
    :pretransaction, :transaction, :label
  case @internal_name
  when :customer
    validate_payload_inclusion_of :customer
  when :voucher
    validate_payload_inclusion_of :voucher
  when :'paymentmethod/voucher'
    validate_payload_inclusion_of :'voucher_redemption'
  when :pre_transaction, :transaction
    validate_payload_must_include_one_of(
      :payment_method_id, :payment_method
    )
    validate_payload_inclusion_of :order_id
  when :login
    validate_payload_inclusion_of :username, :success, :authentication_mechanism
  when :ato_login
    validate_payload_inclusion_of :login
  when :ato_reclaim
    validate_payload_inclusion_of :customers, :source
  end
end