Class: Tangocard::Reward
- Inherits:
-
Object
- Object
- Tangocard::Reward
- Defined in:
- lib/tangocard/reward.rb
Instance Attribute Summary collapse
-
#available ⇒ Object
readonly
Returns the value of attribute available.
-
#countries ⇒ Object
readonly
Returns the value of attribute countries.
-
#currency_code ⇒ Object
readonly
Returns the value of attribute currency_code.
-
#denomination ⇒ Object
readonly
Returns the value of attribute denomination.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#is_variable ⇒ Object
readonly
Returns the value of attribute is_variable.
-
#max_price ⇒ Object
readonly
Returns the value of attribute max_price.
-
#min_price ⇒ Object
readonly
Returns the value of attribute min_price.
-
#sku ⇒ Object
readonly
Returns the value of attribute sku.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(params) ⇒ Reward
constructor
A new instance of Reward.
-
#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.
-
#to_money(field_name) ⇒ Object
Converts price in cents for given field to Money object using currency_code.
-
#variable_price? ⇒ Boolean
Is this a variable-priced reward?.
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
#available ⇒ Object (readonly)
Returns the value of attribute available.
2 3 4 |
# File 'lib/tangocard/reward.rb', line 2 def available @available end |
#countries ⇒ Object (readonly)
Returns the value of attribute countries.
2 3 4 |
# File 'lib/tangocard/reward.rb', line 2 def countries @countries end |
#currency_code ⇒ Object (readonly)
Returns the value of attribute currency_code.
2 3 4 |
# File 'lib/tangocard/reward.rb', line 2 def currency_code @currency_code end |
#denomination ⇒ Object (readonly)
Returns the value of attribute denomination.
2 3 4 |
# File 'lib/tangocard/reward.rb', line 2 def denomination @denomination end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
2 3 4 |
# File 'lib/tangocard/reward.rb', line 2 def description @description end |
#is_variable ⇒ Object (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_price ⇒ Object (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_price ⇒ Object (readonly)
Returns the value of attribute min_price.
2 3 4 |
# File 'lib/tangocard/reward.rb', line 2 def min_price @min_price end |
#sku ⇒ Object (readonly)
Returns the value of attribute sku.
2 3 4 |
# File 'lib/tangocard/reward.rb', line 2 def sku @sku end |
#type ⇒ Object (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)
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
26 27 28 |
# File 'lib/tangocard/reward.rb', line 26 def variable_price? is_variable end |