Class: Tinypass::PriceOption

Inherits:
Object
  • Object
show all
Defined in:
lib/tinypass/price_option.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(price, access_period = nil, start_date_in_secs = nil, end_date_in_secs = nil) ⇒ PriceOption



6
7
8
9
10
11
12
13
# File 'lib/tinypass/price_option.rb', line 6

def initialize(price, access_period = nil, start_date_in_secs = nil, end_date_in_secs = nil)
  @split_pays = {}

  @price, @access_period = price, access_period

  @start_date_in_secs = TokenData.convert_to_epoch_seconds(start_date_in_secs) if start_date_in_secs
  @end_date_in_secs = TokenData.convert_to_epoch_seconds(end_date_in_secs) if end_date_in_secs
end

Instance Attribute Details

#access_periodObject

Returns the value of attribute access_period.



3
4
5
# File 'lib/tinypass/price_option.rb', line 3

def access_period
  @access_period
end

#captionObject

Returns the value of attribute caption.



3
4
5
# File 'lib/tinypass/price_option.rb', line 3

def caption
  @caption
end

#end_date_in_secsObject

Returns the value of attribute end_date_in_secs.



3
4
5
# File 'lib/tinypass/price_option.rb', line 3

def end_date_in_secs
  @end_date_in_secs
end

#priceObject

Returns the value of attribute price.



3
4
5
# File 'lib/tinypass/price_option.rb', line 3

def price
  @price
end

#split_paysObject (readonly)

Returns the value of attribute split_pays.



4
5
6
# File 'lib/tinypass/price_option.rb', line 4

def split_pays
  @split_pays
end

#start_date_in_secsObject

Returns the value of attribute start_date_in_secs.



3
4
5
# File 'lib/tinypass/price_option.rb', line 3

def start_date_in_secs
  @start_date_in_secs
end

Instance Method Details

#access_period_in_msecsObject



15
16
17
# File 'lib/tinypass/price_option.rb', line 15

def access_period_in_msecs
  Utils.parse_loose_period_in_msecs(@access_period)
end

#access_period_in_secsObject



19
20
21
# File 'lib/tinypass/price_option.rb', line 19

def access_period_in_secs
  access_period_in_msecs / 1000
end

#active?(timestamp = nil) ⇒ Boolean



23
24
25
26
27
28
29
30
# File 'lib/tinypass/price_option.rb', line 23

def active?(timestamp = nil)
  timestamp ||= Time.now.to_i
  timestamp = TokenData.convert_to_epoch_seconds(timestamp)

  return false if start_date_in_secs && timestamp < start_date_in_secs
  return false if end_date_in_secs && timestamp > end_date_in_secs
  return true
end

#add_split_pay(email, amount) ⇒ Object



37
38
39
40
41
42
# File 'lib/tinypass/price_option.rb', line 37

def add_split_pay(email, amount)
  amount = amount[0..-2].to_f / 100.0 if amount.end_with?('%')
  amount = amount.to_f

  @split_pays[email] = amount
end

#to_sObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tinypass/price_option.rb', line 44

def to_s
  string = "Price:#{ price }\tPeriod:#{ access_period }\tTrial Period:#{ access_period }"

  if start_date_in_secs
    string << "\tStart:#{ start_date_in_secs }:#{ Time.at(start_date_in_secs).strftime('%a, %d %b %Y %H %M %S') }"
  end

  if end_date_in_secs
    string << "\tEnd:#{ end_date_in_secs }:#{ Time.at(end_date_in_secs).strftime('%a, %d %b %Y %H %M %S') }"
  end

  string << "\tCaption:#{ caption }" if caption

  if @split_pays.any?
    @split_pays.each do |email, amount|
      string << "\tSplit:#{ email }:#{ amount }"
    end
  end

  string
end