-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetdb.rb
70 lines (65 loc) · 1.35 KB
/
tweetdb.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
65
66
67
68
69
70
# coding: utf-8
class TweetDb
attr_accessor :records
def initialize(path)
db_version = "1.0"
@records = []
begin
db = open(path, "r")
begin
version = db.gets.chomp
if version == db_version
num_of_record = db.gets.chomp
num_of_record.to_i.times do
@records << {
id: db.gets.chomp,
date: db.gets.chomp,
status: db.gets.chomp
}
end
else
#version not match!
end
rescue => result
puts result
ensure
db.close
end
rescue
puts "DBリードエラー"
end
end
def clean_old(day)
newrecords = []
@records.each do |record|
parsedstr = Date.parse(record[:date])
date = parsedstr
date += Rational(9, 24)
date += day.to_i
today = DateTime.now
if today < date
newrecords.push(record)
end
end
@records = newrecords
end
def save(path)
db = open(path, "w")
db.puts "1.0"
db.puts @records.size
@records.each do |record|
#dbに書き込む
db.puts(record[:id])
db.puts(record[:date])
db.puts(record[:status])
end
db.close
end
def append(id: nil, date: nil, status: nil)
@records << {
id: id,
date: date,
status: status
}
end
end