module Straight::OrderModule::Includable

Public Instance Methods

amount_in_btc(field: amount, as: :number) click to toggle source
# 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) click to toggle source

Recursion here! Keeps calling itself according to the schedule until either the status changes or the schedule tells it to stop.

# 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) click to toggle source

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.

# File lib/straight/order.rb, line 181
def start_periodic_status_check(duration: 600)
  check_status_on_schedule(duration: duration)
end
to_h() click to toggle source
# File lib/straight/order.rb, line 210
def to_h
  { status: status, amount: amount, address: address, tid: tid }
end
to_json() click to toggle source
# File lib/straight/order.rb, line 206
def to_json
  to_h.to_json
end
transaction(reload: false) click to toggle source

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.

# File lib/straight/order.rb, line 164
def transaction(reload: false)
  transactions(reload: reload).first
end
transactions(reload: false) click to toggle source

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.

# File lib/straight/order.rb, line 156
def transactions(reload: false)
  @transactions = gateway.fetch_transactions_for(address) if reload || !@transactions
  @transactions
end