Module: Straight::OrderModule::Includable
- Defined in:
- lib/straight/order.rb
Instance Method Summary collapse
- #amount_in_btc(as: :number) ⇒ Object
-
#check_status_on_schedule(period: 10, iteration_index: 0, duration: 600, time_passed: 0) ⇒ Object
Recursion here! Keeps calling itself according to the schedule until either the status changes or the schedule tells it to stop.
-
#start_periodic_status_check(duration: 600) ⇒ Object
Starts a loop which calls #status(reload: true) according to the schedule determined in @status_check_schedule.
- #to_h ⇒ Object
- #to_json ⇒ Object
-
#transaction(reload: false) ⇒ Object
Last transaction made to the address.
-
#transactions(reload: false) ⇒ Object
Returns an array of transactions for the order’s address, each as a hash: [ “feba9e7bfea…”, amount: 1202000, … ].
Instance Method Details
#amount_in_btc(as: :number) ⇒ Object
203 204 205 206 |
# File 'lib/straight/order.rb', line 203 def amount_in_btc(as: :number) a = Satoshi.new(amount, from_unit: :satoshi, to_unit: :btc) as == :string ? a.to_unit(as: :string) : a.to_unit end |
#check_status_on_schedule(period: 10, iteration_index: 0, duration: 600, time_passed: 0) ⇒ Object
Recursion here! Keeps calling itself according to the schedule until either the status changes or the schedule tells it to stop.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/straight/order.rb', line 176 def check_status_on_schedule(period: 10, iteration_index: 0, duration: 600, time_passed: 0) self.status(reload: true) time_passed += period if duration >= time_passed # Stop checking if status is >= 2 if self.status < 2 schedule = gateway.status_check_schedule.call(period, iteration_index) sleep period check_status_on_schedule( period: schedule[:period], iteration_index: schedule[:iteration_index], duration: duration, time_passed: time_passed ) end elsif self.status < 2 self.status = STATUSES[:expired] end end |
#start_periodic_status_check(duration: 600) ⇒ Object
Starts a loop which calls #status(reload: true) according to the schedule determined in @status_check_schedule. This method is supposed to be called in a separate thread, for example:
Thread.new do
order.start_periodic_status_check
end
duration argument (value is in seconds) allows you to control in what time an order expires. In other words, we keep checking for new transactions until the time passes. Then we stop and set Order’s status to STATUS. See #check_status_on_schedule for the implementation details.
170 171 172 |
# File 'lib/straight/order.rb', line 170 def start_periodic_status_check(duration: 600) check_status_on_schedule(duration: duration) end |
#to_h ⇒ Object
199 200 201 |
# File 'lib/straight/order.rb', line 199 def to_h { status: status, amount: amount, address: address, tid: tid } end |
#to_json ⇒ Object
195 196 197 |
# File 'lib/straight/order.rb', line 195 def to_json to_h.to_json end |
#transaction(reload: false) ⇒ Object
Last transaction made to the address. Always use this method to check whether a transaction for this order has arrived. We pick last and not first because an address may be reused and we always assume it’s the last transaction that we want to check.
153 154 155 |
# File 'lib/straight/order.rb', line 153 def transaction(reload: false) transactions(reload: reload).first end |
#transactions(reload: false) ⇒ Object
Returns an array of transactions for the order’s address, each as a hash:
[ {tid: "feba9e7bfea...", amount: 1202000, ...} ]
An order is supposed to have only one transaction to its address, but we cannot always guarantee that (especially when a merchant decides to reuse the address for some reason – he shouldn’t but you know people).
Therefore, this method returns all of the transactions. For compliance, there’s also a #transaction method which always returns the last transaction made to the address.
145 146 147 148 |
# File 'lib/straight/order.rb', line 145 def transactions(reload: false) @transactions = gateway.fetch_transactions_for(address) if reload || !@transactions @transactions end |