Class: Simple2ch::Thread

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, title: nil) ⇒ Thread

Returns a new instance of Thread.

Parameters:

  • URL (Bbs2chUrlValidator::UrlInfo)
  • title (String) (defaults to: nil)

    スレッド名



10
11
12
13
14
15
16
17
18
# File 'lib/simple2ch/thread.rb', line 10

def initialize(url, title: nil)
  @title = title
  @url = if url.instance_of?(Bbs2chUrlValidator::UrlInfo)
           url
         else
           Bbs2chUrlValidator::URL.parse(url)
         end
  raise NotA2chThreadUrlError.new "url: #{url}, title: #{title}" if !@url || @url.thread_key.nil?
end

Instance Attribute Details

#thread_keyString (readonly)

Returns スレッドキー(Unix time).

Returns:

  • (String)

    スレッドキー(Unix time)



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

def thread_key
  @thread_key
end

#urlBbs2chUrlValidator::UrlInfo (readonly)

Returns URL.

Returns:

  • (Bbs2chUrlValidator::UrlInfo)

    URL



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

def url
  @url
end

Class Method Details

.parse(board_url, thread_data) ⇒ Simple2ch::Thread

板オブジェクトとsubject.txtの1行データを渡すとスレオブジェクトを返す

Parameters:

  • board_url (Bbs2chUrlValidator::UrlInfo)

    スレッドが属する板情報

  • thread_data (String)

    0000000000.dat<>スレッドタイトル (レス数)

Returns:



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

def self.parse(board_url, thread_data)
  thread_data.match(/(\d{10})\.dat<>(.+) \((\d+)\)/) do |m|
    thread_key = m[1]
    title = m[2].force_encoding('utf-8').chomp
    thread_url = Simple2ch.generate_url(:thread, board_url, thread_key: thread_key)
    self.new(thread_url, title: title)
  end
end

Instance Method Details

#kako_log?Boolean

過去ログかどうかを返す

Returns:

  • (Boolean)

    過去ログか否か



51
52
53
54
# File 'lib/simple2ch/thread.rb', line 51

def kako_log?
  fetch_dat if @is_kako_log.nil?
  @is_kako_log
end

#received_anchorsHash

全てのレスに対し、あるレスへのアンカーが書き込まれているレス番号のハッシュを返す

Returns:

  • (Hash)

    { res_num<Fixnum> => res_nums<Array<Fixnum>> } レス番号のハッシュ



58
59
60
# File 'lib/simple2ch/thread.rb', line 58

def received_anchors
  @received_anchors ||= calc_received_anchors
end

#responses(num_of_responses = nil) ⇒ Object Also known as: response

Datを解析して、レスを返す

Parameters:

  • num_of_responses (Array<Fixnum>, Fixnum) (defaults to: nil)

    取得したいレス番号



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/simple2ch/thread.rb', line 35

def responses(num_of_responses = nil)
  fetch_dat if @responses.nil?
  case num_of_responses
  when Array
    raise 'Blank array was given.' if num_of_responses.empty?
    @responses.find_all { |r| num_of_responses.index(r.res_num) }
  when Fixnum
    @responses.find { |r| r.res_num == num_of_responses }
  when NilClass
    @responses
  end
end

#titleString

タイトルを返す

Returns:

  • (String)

    スレッドの名前



85
86
87
88
# File 'lib/simple2ch/thread.rb', line 85

def title
  fetch_dat unless @title
  @title
end

#type_of_2chSymbol

2chタイプ名の取得

Returns:

  • (Symbol)

    2chタイプ名(:sc, :open)



64
65
66
# File 'lib/simple2ch/thread.rb', line 64

def type_of_2ch
  Simple2ch.type_of_2ch(@url.to_s)
end