Class: Simple2ch::Res

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

Constant Summary

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

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Res) initialize(res_num, author: '', author_id: '', date: nil, mail: '', contents: '', thre: nil)

Returns a new instance of Res

Parameters:

  • res_num (Fixnum)

    レス番号

  • author (String)

    投稿者名

  • author_id (String)

    ID

  • date (Time)

    書き込み日時

  • mail (String)

    メール欄

  • contents (String)

    内容



27
28
29
30
31
32
33
34
35
# File 'lib/simple2ch/res.rb', line 27

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

Instance Attribute Details

- (String) author (readonly)

Returns 投稿者名

Returns:

  • (String)

    投稿者名



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

def author
  @author
end

- (String) author_id (readonly)

Returns ID

Returns:

  • (String)

    ID



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

def author_id
  @author_id
end

- (String) contents (readonly)

Returns 内容

Returns:

  • (String)

    内容



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

def contents
  @contents
end

- (Time) date (readonly)

Returns 書き込み日時

Returns:

  • (Time)

    書き込み日時



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

def date
  @date
end

- (String) mail (readonly)

Returns メール欄

Returns:

  • (String)

    メール欄



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

def mail
  @mail
end

- (Fixnum) res_num (readonly)

Returns レス番号

Returns:

  • (Fixnum)

    レス番号



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

def res_num
  @res_num
end

- (Object) thre=(value) (writeonly)



16
17
18
# File 'lib/simple2ch/res.rb', line 16

def thre=(value)
  @thre = value
end

Class Method Details

+ (Res) parse(res_num, contents, thre = nil)

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

Parameters:

  • res_num (Fixnum)

    レス番号

  • contents (String)

    datのデータ1行

Returns:

  • (Res)

    新規Resオブジェクト

Raises:

  • (KakoLogException)

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



42
43
44
45
46
47
48
49
50
# File 'lib/simple2ch/res.rb', line 42

def self.parse(res_num, contents, thre=nil)
  unless contents.strip == KAKO_LOG_INFO
    hash = parse_dat(contents)
    hash[:thre] = thre if thre
    return self.new(res_num, hash)
  else
    raise KakoLogException
  end
end

Instance Method Details

- (Array<Fixnum>) anchors

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

Returns:

  • (Array<Fixnum>)

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/simple2ch/res.rb', line 54

def anchors
  arashi_removal_regex = /(?:\d{1,4}(?:\]*>)?(?:>|\[>,+-\]){1,2}){9}/
  unless 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
  else
    []
  end
end

- (Object) received_anchors

自レスへのアンカーが書き込まれているレス番号を返す



78
79
80
81
82
# File 'lib/simple2ch/res.rb', line 78

def received_anchors
  thre = get_thre
  received_anchors = thre.received_anchors
  received_anchors.fetch(@res_num, [])
end