Class: Tangocard::Reward

Inherits:
Object
  • Object
show all
Defined in:
lib/tangocard/reward.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Reward

Returns a new instance of Reward.


5
6
7
8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#availableObject (readonly)

Returns the value of attribute available.


2
3
4
# File 'lib/tangocard/reward.rb', line 2

def available
  @available
end

#countriesObject (readonly)

Returns the value of attribute countries.


2
3
4
# File 'lib/tangocard/reward.rb', line 2

def countries
  @countries
end

#currency_codeObject (readonly)

Returns the value of attribute currency_code.


2
3
4
# File 'lib/tangocard/reward.rb', line 2

def currency_code
  @currency_code
end

#denominationObject (readonly)

Returns the value of attribute denomination.


2
3
4
# File 'lib/tangocard/reward.rb', line 2

def denomination
  @denomination
end

#descriptionObject (readonly)

Returns the value of attribute description.


2
3
4
# File 'lib/tangocard/reward.rb', line 2

def description
  @description
end

#is_variableObject (readonly)

Returns the value of attribute is_variable.


2
3
4
# File 'lib/tangocard/reward.rb', line 2

def is_variable
  @is_variable
end

#max_priceObject (readonly)

Returns the value of attribute max_price.


2
3
4
# File 'lib/tangocard/reward.rb', line 2

def max_price
  @max_price
end

#min_priceObject (readonly)

Returns the value of attribute min_price.


2
3
4
# File 'lib/tangocard/reward.rb', line 2

def min_price
  @min_price
end

#skuObject (readonly)

Returns the value of attribute sku.


2
3
4
# File 'lib/tangocard/reward.rb', line 2

def sku
  @sku
end

#typeObject (readonly)

Returns the value of attribute type.


2
3
4
# File 'lib/tangocard/reward.rb', line 2

def type
  @type
end

Instance Method Details

#purchasable?(balance_in_cents) ⇒ Boolean

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)

Returns:

  • (Boolean)

40
41
42
43
44
45
46
47
48
# 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) ⇒ Object

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)

58
59
60
61
62
# 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?Boolean

Is this a variable-priced reward?

Example:

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

Arguments:

none

Returns:

  • (Boolean)

26
27
28
# File 'lib/tangocard/reward.rb', line 26

def variable_price?
  is_variable
end