-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.rb
48 lines (39 loc) · 982 Bytes
/
post.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
# frozen_string_literal: true
require 'json'
module Genkai
# DATファイルの一行で表わされるレスを表わすクラス。
class Post
class << self
def from_line(str, number = nil)
new(*str.chomp.split('<>', 5), number)
end
end
attr :number
attr_accessor :date, :subject
attr_reader :name, :mail, :body
def initialize(name, mail, date, body, subject, number = nil)
@name = name
@mail = mail
@date = date
@body = body
@subject = subject
@number = number
end
def to_line
[name, mail, date, body, subject].join('<>') + "\n"
end
def id
return nil unless date =~ %r{ ID:([A-Za-z0-9+/]+)}
$1
end
def date_proper
date.sub(%r{ ID:[A-Za-z0-9+/]+}, '')
end
def to_h
{ name: name, mail: mail, date: date, body: body, subject: subject, number: number }
end
def to_json(*args)
to_h.to_json(*args)
end
end
end