Class: Transaction::Client
- Inherits:
-
Object
- Object
- Transaction::Client
- Defined in:
- lib/transaction.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#transaction_id ⇒ Object
readonly
Returns the value of attribute transaction_id.
Instance Method Summary collapse
- #clear! ⇒ Object
- #finish!(status: 'success', clear: false, data: {}) ⇒ Object
-
#initialize(transaction_id: nil, options: {}) ⇒ Client
constructor
A new instance of Client.
- #refresh! ⇒ Object
- #start! ⇒ Object
- #trigger_event!(data = {}) ⇒ Object
- #update_attributes(options) ⇒ Object
- #update_status(status) ⇒ Object
Constructor Details
#initialize(transaction_id: nil, options: {}) ⇒ Client
Returns a new instance of Client.
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# 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 = DEFAULT_ATTRIBUTES.merge() @attributes = parsed_attributes || {} update_attributes() if @attributes.empty? @status = @attributes[:status].to_s end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
52 53 54 |
# File 'lib/transaction.rb', line 52 def attributes @attributes end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
52 53 54 |
# File 'lib/transaction.rb', line 52 def status @status end |
#transaction_id ⇒ Object (readonly)
Returns the value of attribute transaction_id.
52 53 54 |
# File 'lib/transaction.rb', line 52 def transaction_id @transaction_id end |
Instance Method Details
#clear! ⇒ Object
96 97 98 99 |
# File 'lib/transaction.rb', line 96 def clear! @attributes = @status = nil redis_delete end |
#finish!(status: 'success', clear: false, data: {}) ⇒ Object
90 91 92 93 94 |
# 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! ⇒ Object
101 102 103 104 105 106 |
# 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! ⇒ Object
85 86 87 88 |
# File 'lib/transaction.rb', line 85 def start! update_status(:processing) trigger_event!(message: 'Processing') end |
#trigger_event!(data = {}) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 |
# 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) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/transaction.rb', line 68 def update_attributes() unless .is_a? Hash raise ArgumentError, 'Invalid type. Expected Hash' end @attributes = symbolize_keys!(@attributes.merge!()) redis_set(@transaction_id, @attributes.to_json) @status = @attributes[:status].to_s end |
#update_status(status) ⇒ Object
78 79 80 81 82 83 |
# 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 |