class Transaction::Client

Attributes

attributes[R]
status[R]
transaction_id[R]

Public Class Methods

new(transaction_id: nil, options: {}) click to toggle source
# File lib/transaction.rb, line 54
def initialize(transaction_id: nil, options: {})
  @transaction_id = transaction_id ||
                    "transact-#{SecureRandom.urlsafe_base64(16)}"
  @redis_client = Transaction.redis
  @pubsub_client = Transaction.pubsub_client

  options = DEFAULT_ATTRIBUTES.merge(options)

  @attributes = parsed_attributes || {}
  update_attributes(options) if @attributes.empty?

  @status = @attributes[:status].to_s
end

Public Instance Methods

clear!() click to toggle source
# File lib/transaction.rb, line 96
def clear!
  @attributes = @status = nil
  redis_delete
end
finish!(status: 'success', clear: false, data: {}) click to toggle source
# File lib/transaction.rb, line 90
def finish!(status: 'success', clear: false, data: {})
  update_status(status)
  trigger_event!({ message: 'Done' }.merge(data))
  redis_delete if clear
end
refresh!() click to toggle source
# File lib/transaction.rb, line 101
def refresh!
  @attributes = parsed_attributes
  raise StandardError, 'Transaction expired' if @attributes.nil?

  @status = @attributes[:status].to_s
end
start!() click to toggle source
# File lib/transaction.rb, line 85
def start!
  update_status(:processing)
  trigger_event!(message: 'Processing')
end
trigger_event!(data = {}) click to toggle source
# File lib/transaction.rb, line 108
def trigger_event!(data = {})
  return if @pubsub_client.nil?

  data[:status] = @status
  channel_name = @pubsub_client[:channel_name] || @transaction_id
  client = @pubsub_client[:client]
  trigger = @pubsub_client[:trigger]
  event = @pubsub_client[:event]

  client.send(trigger, channel_name, event, data)
end
update_attributes(options) click to toggle source
# File lib/transaction.rb, line 68
def update_attributes(options)
  unless options.is_a? Hash
    raise ArgumentError, 'Invalid type. Expected Hash'
  end

  @attributes = symbolize_keys!(@attributes.merge!(options))
  redis_set(@transaction_id, @attributes.to_json)
  @status = @attributes[:status].to_s
end
update_status(status) click to toggle source
# File lib/transaction.rb, line 78
def update_status(status)
  status = status.to_sym
  raise 'Invalid Status' unless STATUSES.include?(status.to_sym)

  update_attributes(status: status)
end

Private Instance Methods

parsed_attributes() click to toggle source
# File lib/transaction.rb, line 122
def parsed_attributes
  data = redis_get
  return nil if data.nil?

  begin
    response = JSON.parse(data)
    raise 'Invalid type. Expected Hash' unless response.is_a? Hash

    response = symbolize_keys!(response)
  rescue JSON::ParserError
    raise 'Invalid attempt to update the attributes'
  end

  response
end
redis_delete() click to toggle source
# File lib/transaction.rb, line 147
def redis_delete
  @redis_client.del(@transaction_id)
end
redis_get() click to toggle source

redis methods

# File lib/transaction.rb, line 139
def redis_get
  @redis_client.get(@transaction_id)
end
redis_set(key, value) click to toggle source
# File lib/transaction.rb, line 143
def redis_set(key, value)
  @redis_client.set(key, value)
end