class Bbs::Nichan::Thread

2ちゃんスレッド

Public Class Methods

from_line(line, board) click to toggle source
# File lib/bbiff/bbs_reader.rb, line 353
def from_line(line, board)
  unless line =~ /^(\d+)\.dat<>(.+?) \((\d+)\)$/
    fail 'スレ一覧のフォーマットが変です' 
  end
  id, title, last = $1.to_i, $2, $3.to_i
  Thread.send(:new, board, id, title, last)
end
from_url(url) click to toggle source
# File lib/bbiff/bbs_reader.rb, line 340
def from_url(url)
  if url.to_s =~ NICHAN_THREAD_URL_PATTERN
    board_name, thread_num = $2, $3.to_i
    uri = URI(url)
    board = Board.send(:new, uri.hostname, uri.port, board_name)
    thread = board.thread(thread_num)
    raise NotFoundError, 'no such thread' if thread.nil?
    return thread
  else
    return nil
  end
end
new(board, id, title, last = 1) click to toggle source
Calls superclass method Bbs::ThreadBase::new
# File lib/bbiff/bbs_reader.rb, line 362
def initialize(board, id, title, last = 1)
  super
end

Public Instance Methods

posts(range, opts = {}) click to toggle source
# File lib/bbiff/bbs_reader.rb, line 366
def posts(range, opts = {})
  fail ArgumentError unless range.is_a? Range
  url = URI(dat_url)
  begin
    lines = @board.send(:download_text,
                        if opts[:long_polling] then
                          url + "?long_polling=1"
                        else
                          url
                        end)
  rescue Net::ReadTimeout => e
    if opts[:long_polling]
      retry
    else
      raise e
    end
  end
  ary = []
  lines.each_line.with_index(1) do |line, res_no|
    next unless range.include?(res_no)

    fields = line.chomp.split('<>', 5)
    if fields.size != 5
      raise FormatError, "invalid line #{line.inspect}"
    end
    name, mail, date, body, title = fields
    post = Post.new(res_no.to_s, name, mail, date, body)
    ary << post
    @last = [post.no, last].max
  end
  return ary
end