Class: Irctc::Calculator

Inherits:
Object
  • Object
show all
Defined in:
lib/irctc/calculator.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Calculator

Returns a new instance of Calculator.



4
5
6
7
# File 'lib/irctc/calculator.rb', line 4

def initialize(opts)
  @present_date = opts.present_date
  @future_date = opts.future_date
end

Instance Method Details

#calculateObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/irctc/calculator.rb', line 9

def calculate
  op = ''
  # present
  if @present_date
    result = @present_date + IRCTC_DAYS
    op += "\nOn the given date, tickets are available for this date:"
  # future
  elsif @future_date
    result = @future_date - IRCTC_DAYS
    days_left = (@future_date - Date.today).to_i - IRCTC_DAYS
    if days_left <= 0
      op += "\nTicket counter already opened '#{days_left.abs}' days ago."
    else
      op += "\nDon't forget to book in '#{days_left}' days!"
    end
    op += "\n\nFor the given date, tickets are available from this date:"
  end

  op += result.strftime("\n(%a) %b %d, %Y")
  op
end