Class: Simple2ch::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/simple2ch/response.rb

Constant Summary collapse

KAKO_LOG_INFO =
'過去ログ ★<><>[過去ログ]<><em>■ このスレッドは過去ログ倉庫に格納されています</em><>'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(res_num, author: '', author_id: '', date: nil, mail: '', contents: '') ⇒ Response

Returns a new instance of Response.

Parameters:

  • res_num (Fixnum)

    レス番号

  • author (String) (defaults to: '')

    投稿者名

  • author_id (String) (defaults to: '')

    ID

  • date (Time) (defaults to: nil)

    書き込み日時

  • mail (String) (defaults to: '')

    メール欄

  • contents (String) (defaults to: '')

    内容



24
25
26
27
28
29
30
31
# File 'lib/simple2ch/response.rb', line 24

def initialize(res_num, author: '', author_id: '', date: nil, mail: '', contents: '')
  @res_num = res_num
  @author = author
  @author_id = author_id
  @date = date
  @mail = mail
  @contents = contents
end

Instance Attribute Details

#authorString (readonly)

Returns 投稿者名.

Returns:

  • (String)

    投稿者名



6
7
8
# File 'lib/simple2ch/response.rb', line 6

def author
  @author
end

#author_idString (readonly)

Returns ID.

Returns:

  • (String)

    ID



8
9
10
# File 'lib/simple2ch/response.rb', line 8

def author_id
  @author_id
end

#contentsString (readonly)

Returns 内容.

Returns:

  • (String)

    内容



14
15
16
# File 'lib/simple2ch/response.rb', line 14

def contents
  @contents
end

#dateTime (readonly)

Returns 書き込み日時.

Returns:

  • (Time)

    書き込み日時



10
11
12
# File 'lib/simple2ch/response.rb', line 10

def date
  @date
end

#mailString (readonly)

Returns メール欄.

Returns:

  • (String)

    メール欄



12
13
14
# File 'lib/simple2ch/response.rb', line 12

def mail
  @mail
end

#res_numFixnum (readonly)

Returns レス番号.

Returns:

  • (Fixnum)

    レス番号



4
5
6
# File 'lib/simple2ch/response.rb', line 4

def res_num
  @res_num
end

Class Method Details

.parse(res_num, contents) ⇒ Res

Datの1行から各項目を分離して、Resオブジェクトを返す

Parameters:

  • res_num (Fixnum)

    レス番号

  • contents (String)

    datのデータ1行

Returns:

  • (Res)

    新規Resオブジェクト

Raises:

  • (KakoLogError)

    過去ログ情報をパースしようとした際に発生



38
39
40
41
42
# File 'lib/simple2ch/response.rb', line 38

def self.parse(res_num, contents)
  raise KakoLogError if contents.strip == KAKO_LOG_INFO
  hash = parse_dat(contents)
  self.new(res_num, hash)
end

Instance Method Details

#abone?Boolean

あぼーんレスか否か

Returns:

  • (Boolean)

    あぼーんならtrue



67
68
69
# File 'lib/simple2ch/response.rb', line 67

def abone?
  @date == 'あぼーん'
end

#anchorsArray<Fixnum>

アンカーを抽出する 荒らしの場合は空配列を返す

Returns:

  • (Array<Fixnum>)

    昇順ソート済みアンカー、荒らしの場合は空配列



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/simple2ch/response.rb', line 46

def anchors
  arashi_removal_regex = /(?:\d{1,4}(?:\]*>)?(?:>|\[>,+-\]){1,2}){9}/
  return [] if self.contents =~ arashi_removal_regex
  splitter_regex = '[,、,  ]'
  digit_regex = '(?:\d|[0-9])+'
  hyphen_regex = '[−ーー\-〜~〜]'
  extracted = self.contents.scan /&gt;((?:#{digit_regex}(?:#{splitter_regex}|#{hyphen_regex})*)+)/
  anchors = extracted.flatten.to_s.gsub(/[\"\[\]]/, '').split(/#{splitter_regex}/)
  anchors.delete('')
  anchors.map! do |a|
    if a =~ /(#{digit_regex})#{hyphen_regex}(#{digit_regex})/
      (Range.new parseInt($1), parseInt($2)).to_a
    else
      parseInt(a)
    end
  end
  anchors.flatten.uniq.sort
end

#contents_textString

レスの内容をテキスト情報で得る。&nbsp, &lt, &gt,
はそれぞれ「 」、「<」、「>」、「(改行)」に置換される。

Returns:

  • (String)

    テキスト情報でのレスの内容



73
74
75
76
77
78
# File 'lib/simple2ch/response.rb', line 73

def contents_text
  require 'htmlentities'
  anchor_regex = /<a href="\.\.\/test\/read.cgi\/.+\/\d{10}\/\d{1,4}" target="_blank">(>>\d{1,4})<\/a>/
  @htmlentities ||= HTMLEntities.new
  @htmlentities.decode(@contents).gsub('<br>', "\n").gsub(/<\/?b>/, '').gsub(anchor_regex, '\1')
end

#res_author_textString

HTMLタグを取り除いた投稿者名

Returns:

  • (String)

    HTMLタグを取り除いた投稿者名



82
83
84
85
86
# File 'lib/simple2ch/response.rb', line 82

def res_author_text
  require 'htmlentities'
  @htmlentities ||= HTMLEntities.new
  @htmlentities.decode(@author).gsub('<br>', "\n").gsub(/<\/?b>/, '')
end