-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.rb
146 lines (124 loc) · 3.72 KB
/
program.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
module Syobocalite
class Program
# @!attribute pid
# @return [Integer]
attr_accessor :pid
# @!attribute tid
# @return [Integer]
attr_accessor :tid
# @!attribute st_time
# @return [ActiveSupport::TimeWithZone]
attr_accessor :st_time
# @!attribute ed_time
# @return [ActiveSupport::TimeWithZone]
attr_accessor :ed_time
# @!attribute ch_name
# @return [String]
attr_accessor :ch_name
# @!attribute ch_id
# @return [Integer]
attr_accessor :ch_id
# @!attribute count
# @return [Integer]
attr_accessor :count
# @!attribute st_offset
# @return [Integer]
attr_accessor :st_offset
# @!attribute sub_title
# @return [String]
attr_accessor :sub_title
# @!attribute title
# @return [String]
attr_accessor :title
# @!attribute prog_comment
# @return [String]
attr_accessor :prog_comment
# @param pid [Integer]
# @param tid [Integer]
# @param st_time [ActiveSupport::TimeWithZone]
# @param ed_time [ActiveSupport::TimeWithZone]
# @param ch_name [String]
# @param ch_id [Integer]
# @param count [Integer]
# @param st_offset [Integer]
# @param sub_title [String]
# @param title [String]
# @param prog_comment [String]
def initialize(pid: nil, tid: nil, st_time: nil, ed_time: nil, ch_name: nil, ch_id: nil,
count: nil, st_offset: nil, sub_title: nil, title: nil, prog_comment: nil)
@pid = pid
@tid = tid
@st_time = st_time
@ed_time = ed_time
@ch_name = ch_name
@ch_id = ch_id
@count = count
@st_offset = st_offset
@sub_title = sub_title
@title = title
@prog_comment = prog_comment
end
alias_method :story_number, :count
alias_method :story_number=, :count=
# Get program's flag
# @return [Integer]
# @see https://docs.cal.syoboi.jp/spec/proginfo-flag/
def flag
@flag ||= Syobocalite::DbClient.new.get_program_flag(tid: tid, pid: pid)
end
# Whether contains remark
# @return [Boolean]
# @see https://docs.cal.syoboi.jp/spec/proginfo-flag/
def remark?
flag & 0x01 != 0
end
# Whether new program
# @return [Boolean]
# @see https://docs.cal.syoboi.jp/spec/proginfo-flag/
def new?
flag & 0x02 != 0
end
# Whether final program
# @return [Boolean]
# @see https://docs.cal.syoboi.jp/spec/proginfo-flag/
def final?
flag & 0x04 != 0
end
# Whether re-air program
# @return [Boolean]
# @see https://docs.cal.syoboi.jp/spec/proginfo-flag/
def re_air?
flag & 0x08 != 0
end
# @param attrs [Hash]
#
# @return [Syobocalite::Program]
def self.from_prog_item(attrs = {})
Syobocalite::Program.new(
pid: attrs["PID"]&.to_i,
tid: attrs["TID"]&.to_i,
st_time: to_time(attrs["StTime"]),
ed_time: to_time(attrs["EdTime"]),
ch_name: attrs["ChName"],
ch_id: attrs["ChID"]&.to_i,
count: attrs["Count"]&.to_i,
st_offset: attrs["StOffset"]&.to_i,
sub_title: sanitize_text(attrs["SubTitle"]),
title: sanitize_text(attrs["Title"]),
prog_comment: attrs["ProgComment"]&.gsub(/^!/, ""),
)
end
def self.to_time(str)
return nil unless str
Time.use_zone("Tokyo") do
Time.zone.parse(str)
end
end
private_class_method :to_time
def self.sanitize_text(str)
return nil unless str
CGI.unescapeHTML(str)
end
private_class_method :sanitize_text
end
end