class SidekiqStrategies::AccountHandler

Attributes

account[RW]
action[RW]
priority[RW]
transaction[RW]

Public Class Methods

new(account_id, transaction_id, action, priority, publisher) click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 6
def initialize(account_id, transaction_id, action, priority, publisher)
  self.action = action
  self.priority = priority
  load_account(account_id)
  load_transaction(transaction_id)
  self.class.send(:include, publisher)
end

Public Instance Methods

action_name() click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 78
def action_name
  action_name = self.action
  action_name = 'pay' if action_name == 'payment'
  action_name = 'revert' if action_name == 'reversion'

  action_name
end
error() click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 30
def error
  previous_account_state = self.account.state
  self.account.error
  publish(:state_changed , self.account, previous_account_state, self.account.state)
end
queue_for_retry() click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 42
def queue_for_retry
  previous_account_state = self.account.state
  self.transaction.retry_count += 1
  self.account.queue_for_retry
  self.transaction.save!(validate: false)
  publish(:state_changed , self.account, previous_account_state, self.account.state)
end
queue_for_wait_at_worker() click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 50
def queue_for_wait_at_worker
  previous_account_state = self.account.state
  self.account.queue_for_wait_at_worker
  publish(:state_changed , self.account, previous_account_state, self.account.state)
end
save_beanstalkd_jid(jid) click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 19
def save_beanstalkd_jid(jid)
  self.transaction.beanstalkd_jid = jid
  self.transaction.save!
end
save_exception(exception) click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 65
def save_exception(exception)
  parsed_exception = parse_exception(exception)
  
  self.transaction.response = parsed_exception
  self.transaction.response_code = parsed_exception[:code]
  self.transaction.response_message =  parsed_exception[:message]
  
  self.transaction.exception_message = parsed_exception[:message]
  self.transaction.exception_type = parsed_exception[:type]

  self.transaction.save!(validate: false)
end
save_sidekiq_jid(jid) click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 14
def save_sidekiq_jid(jid)
  self.transaction.sidekiq_jid = jid
  self.transaction.save!
end
save_success_response(body) click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 56
def save_success_response(body)
  self.transaction.response = body
  self.transaction.amount = body[:amount_due]

  action_response = self.send("#{action_name}_response")
  self.account.send(self.action).response = action_response
  self.transaction.save!(validate: false)
end
start_action() click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 36
def start_action
  previous_account_state = self.account.state
  self.account.send "start_#{self.action}!"
  publish(:state_changed , self.account, previous_account_state, self.account.state)
end
success() click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 24
def success
  previous_account_state = self.account.state
  self.account.success
  publish(:state_changed , self.account, previous_account_state, self.account.state)
end

Private Instance Methods

authorization_response() click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 121
def authorization_response
  {
    validity: (self.transaction.response[:validity] || self.transaction.response["validity"]),
    message: (self.transaction.response[:message] || self.transaction.response["message"])
  }
end
load_account(account_id) click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 88
def load_account(account_id)
  self.account = Account.find(account_id)
end
load_transaction(transaction_id) click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 92
def load_transaction(transaction_id)
  self.transaction = account.transactions.find(transaction_id)
end
parse_exception(exception) click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 132
def parse_exception(exception)
  unless exception.kind_of?(StandardError)
    ex = exception
  else
    ex = {
      type: exception.class.to_s,
      code: exception.code,
      message: exception.message,
      partner_code: ( exception.partner_code rescue 1000 ),
      partner_message: ( exception.partner_message rescue exception.to_s ),
      http_code: 500,
      retry_action: ( exception.retry_action rescue false )
    }
  end
  ex
end
payment_response() click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 115
def payment_response
  {
    reference: self.transaction.reference
  }
end
query_response() click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 96
def query_response
  body = self.transaction.response

  # public fields
  response = {
                name: ( body[:name] || body["name"] ),
                address: ( body[:address] || body["address"] ),
                amount_cents: self.transaction.amount,
                amount: Money.new(self.transaction.amount, self.account.currency).format }

  # private fields
  keys = body.keys.select { |k| k.to_s =~ /^_/ }
  keys.each do |k|
    response[k] = body[k]
  end

  response
end
reversion_response() click to toggle source
# File lib/sidekiq_strategies/account_handler.rb, line 128
def reversion_response
  {}
end