Class: Konbikol::Ticket

Inherits:
Object
  • Object
show all
Defined in:
lib/konbikol/ticket.rb

Constant Summary collapse

TIMEZONE_ID =
'Europe/Warsaw'

Instance Method Summary collapse

Constructor Details

#initialize(ticket_text) ⇒ Ticket

Returns a new instance of Ticket.



11
12
13
# File 'lib/konbikol/ticket.rb', line 11

def initialize(ticket_text)
  @ticket_text = ticket_text
end

Instance Method Details

#arrival_dateObject



82
83
84
# File 'lib/konbikol/ticket.rb', line 82

def arrival_date
  arrival.fetch(:date)
end

#arrival_datetimeObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/konbikol/ticket.rb', line 86

def arrival_datetime
  return @arrival_datetime if @arrival_datetime

  datetime = Time.parse("#{departure_datetime.year}#{arrival_date.split('.').reverse.join('')}#{arrival_time.sub(':', '')}")

  if departure_datetime > datetime
    # Add one year.
    datetime = Time.parse("#{departure_datetime.year + 1}#{arrival_date.split('.').reverse.join('')}#{arrival_time.sub(':', '')}")
  end

  @arrival_datetime = datetime
end

#arrival_stationObject



99
100
101
# File 'lib/konbikol/ticket.rb', line 99

def arrival_station
  arrival.fetch(:station)
end

#arrival_timeObject



78
79
80
# File 'lib/konbikol/ticket.rb', line 78

def arrival_time
  arrival.fetch(:time)
end

#carriageObject



103
104
105
# File 'lib/konbikol/ticket.rb', line 103

def carriage
  arrival.fetch(:carriage)
end

#departure_dateObject

The format is ‘dd.mm` E.g. `23.04`.



57
58
59
# File 'lib/konbikol/ticket.rb', line 57

def departure_date
  departure.fetch(:date)
end

#departure_datetimeObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/konbikol/ticket.rb', line 61

def departure_datetime
  return @departure_datetime if @departure_datetime

  datetime = Time.parse("#{purchase_time.year}#{departure_date.split('.').reverse.join('')}#{departure_time.sub(':', '')}")

  if datetime < purchase_time
    # Add one year.
    datetime = Time.parse("#{purchase_time.year + 1}#{departure_date.split('.').reverse.join('')}#{departure_time.sub(':', '')}")
  end

  @departure_datetime = datetime
end

#departure_stationObject



74
75
76
# File 'lib/konbikol/ticket.rb', line 74

def departure_station
  departure.fetch(:station)
end

#departure_timeObject



52
53
54
# File 'lib/konbikol/ticket.rb', line 52

def departure_time
  departure.fetch(:time)
end

#purchase_timeObject



115
116
117
118
119
120
121
122
123
# File 'lib/konbikol/ticket.rb', line 115

def purchase_time
  return @purchase_time if @purchase_time

  line = find_next_non_empty_line_below_line_matching_regex(/Zapłacono i wystawiono dnia/)

  @purchase_time ||= Time.parse(
    line.split(/\s{2,}/).last.match(/(.+)\(.+/)[1]
  )
end

#seatObject



107
108
109
# File 'lib/konbikol/ticket.rb', line 107

def seat
  departure.fetch(:seat)
end

#to_filenameObject



34
35
36
37
38
39
40
41
# File 'lib/konbikol/ticket.rb', line 34

def to_filename
  basename = "#{departure_station}-#{arrival_station}__#{Time.now.to_i.to_s(32)}"
    .to_ascii
    .gsub('.', '')
    .gsub(' ', '_')

  "#{basename}.ics"
end

#to_formatted_sObject



43
44
45
46
47
48
49
50
# File 'lib/konbikol/ticket.rb', line 43

def to_formatted_s
  <<~EVENT
    #{departure_station}#{arrival_station}
    #{departure_datetime}#{arrival_datetime}
    #{train}
    wagon #{carriage} miejsce #{seat}
  EVENT
end

#to_icalObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/konbikol/ticket.rb', line 15

def to_ical
  calendar = Icalendar::Calendar.new
  timezone = TZInfo::Timezone.get TIMEZONE_ID
  ical_timezone = timezone.ical_timezone departure_date

  calendar.add_timezone ical_timezone
  # TODO: Add PRODID.
  # TODO: Search for other useful things to set in an ics file.

  calendar.event do |event|
    event.dtstart = Icalendar::Values::DateTime.new departure_datetime, 'tzid' => TIMEZONE_ID
    event.dtend = Icalendar::Values::DateTime.new arrival_datetime, 'tzid' => TIMEZONE_ID
    event.summary = "#{departure_station}#{arrival_station}"
    event.description = "#{train}\nwagon #{carriage} miejsce #{seat}"
  end

  calendar.to_ical
end

#trainObject



111
112
113
# File 'lib/konbikol/ticket.rb', line 111

def train
  departure.fetch(:train)
end