class Tangocard::Reward

Attributes

available[R]
countries[R]
currency_code[R]
denomination[R]
description[R]
is_variable[R]
max_price[R]
min_price[R]
sku[R]
type[R]

Public Class Methods

new(params) click to toggle source
# File lib/tangocard/reward.rb, line 5
def initialize(params)
  @type          = params['type']
  @description   = params['description']
  @sku           = params['sku']
  @is_variable   = params['is_variable']
  @denomination  = params['denomination'].to_i
  @min_price     = params['min_price'].to_i
  @max_price     = params['max_price'].to_i
  @currency_code = params['currency_code']
  @available     = params['available']
  @countries     = params['countries']
end

Public Instance Methods

purchasable?(balance_in_cents) click to toggle source

Is this reward purchasable given a certain number of cents available to purchase it? True if reward is available and user has enough cents False if reward is unavailable OR user doesn't have enough cents

Example:

>> reward.purchasable?(500)
 => true # reward is available and costs <= 500 cents

Arguments:

balance_in_cents: (Integer)
# File lib/tangocard/reward.rb, line 40
def purchasable?(balance_in_cents)
  return false unless available

  if variable_price?
    min_price <= balance_in_cents
  else
    denomination <= balance_in_cents
  end
end
to_money(field_name) click to toggle source

Converts price in cents for given field to Money object using currency_code

Example:

>> reward.to_money(:unit_price)
 => #<Money fractional:5000 currency:USD>

Arguments:

field_name: (Symbol - must be :min_price, :max_price, or :denomination)
# File lib/tangocard/reward.rb, line 58
def to_money(field_name)
  return nil unless [:min_price, :max_price, :denomination].include?(field_name)

  Money.new(self.send(field_name), currency_code)
end
variable_price?() click to toggle source

Is this a variable-priced reward?

Example:

>> reward.variable_price?
 => true # reward is variable-priced

Arguments:

none
# File lib/tangocard/reward.rb, line 26
def variable_price?
  is_variable
end