-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.rb
64 lines (55 loc) · 1.26 KB
/
parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/ruby -w
# --- Log opened Sat Jan 06 18:28:43 2014
#--- Day changed Sat Jan 25 2014
#18:30 kubunto: but then again money is in streaming anyway
require 'date'
class Parser
OC_FORMAT = '%a %b %d %H:%M:%S %Y'
DC_FORMAT = '%a %b %d %Y'
D_FORMAT = '%m/%d/%Y'
OPEN = "Log opened "
CLOSE = "Log closed "
CHANGE = "Day changed "
def initialize
@fastforward = true
@play = false
end
def parse(line)
date = get_date(line)
if date === @date && @fastforward
@play = true
@fastforward = false
puts line
elsif @play
if date && date != @date
@play = false
else
puts line
end
end
end
#once function finishes, it automatically returns nil
def get_date(line)
return unless line.starts_with?('---')
source = get_source(line)
format = (line[OPEN] || line[CLOSE]) ? OC_FORMAT : DC_FORMAT
if source
return Date.strptime(source, format)
end
end
def get_source(line)
if line[OPEN]
return line.split(OPEN)[1]
elsif line[CLOSE]
return line.split(CLOSE)[1]
elsif line[CHANGE]
return line.split(CHANGE)[1]
end
end
def state
return @play || @fastforward
end
def set_date(date)
@date = date
end
end