-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_builder.rb
68 lines (55 loc) · 1.73 KB
/
post_builder.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
# frozen_string_literal: true
require_relative 'extensions'
require_relative 'board'
require_relative 'thread_file'
require_relative 'post'
require_relative 'string_helpers'
module Genkai
# レスが投稿された時に、Postオブジェクトを作るクラス。
class PostBuilder
include StringHelpers
class << self
def linkify(escaped_body, board_id, thread_id)
escaped_body.gsub(/>>(\d+(-\d+)?)/) do |str|
"<a href=\"/test/read.cgi/#{board_id}/#{thread_id}/#{$1}\">#{str}</a>"
end
end
def format_date(time)
time.strftime('%Y/%m/%d(%%s) %H:%M:%S') % '日月火水木金土'[time.wday]
end
end
attr_reader :id
def initialize(board, thread, remote_addr)
@board = board
@thread = thread
@id = Digest::MD5.base64digest(remote_addr)[0, 8]
end
def create_post(raw_name, raw_mail, raw_body, raw_title = '')
raw_name = @board.default_name if raw_name.blank?
date = PostBuilder.format_date(Time.now.localtime)
date = "#{date} ID:#{@id}" if requires_id?(raw_mail)
Post.new(escape_field(raw_name),
escape_field(raw_mail),
date,
escape_body(raw_body),
raw_title)
end
def requires_id?(mail)
case @board.id_policy
when :no then false
when :force then true
when :optional then mail.blank?
else raise 'panic'
end
end
private
def escape_body(body)
lines = linkify(escape_field(body)).each_line.map(&:chomp)
' ' + lines.join(' <br> ') + ' '
end
# レスアンカーをリンクにする
def linkify(escaped_body)
PostBuilder.linkify(escaped_body, @board.id, @thread.id)
end
end
end