Module: Straight::OrderModule::Includable
- Defined in:
- lib/straight/order.rb
Instance Method Summary collapse
- #amount_in_btc(field: amount, 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(field: amount, as: :number) ⇒ Object
214 215 216 217 |
# File 'lib/straight/order.rb', line 214 def amount_in_btc(field: amount, as: :number) a = Satoshi.new(field, 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.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/straight/order.rb', line 187 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.
181 182 183 |
# File 'lib/straight/order.rb', line 181 def start_periodic_status_check(duration: 600) check_status_on_schedule(duration: duration) end |
#to_h ⇒ Object
210 211 212 |
# File 'lib/straight/order.rb', line 210 def to_h { status: status, amount: amount, address: address, tid: tid } end |
#to_json ⇒ Object
206 207 208 |
# File 'lib/straight/order.rb', line 206 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.
164 165 166 |
# File 'lib/straight/order.rb', line 164 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.
156 157 158 159 |
# File 'lib/straight/order.rb', line 156 def transactions(reload: false) @transactions = gateway.fetch_transactions_for(address) if reload || !@transactions @transactions end |