Class: Bbs::Nichan::Thread

Inherits:
ThreadBase show all
Defined in:
lib/bbiff/bbs_reader.rb

Overview

2ちゃんスレッド

Instance Attribute Summary

Attributes inherited from ThreadBase

#board, #id, #last, #title

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ThreadBase

#dat_url

Constructor Details

#initialize(board, id, title, last = 1) ⇒ Thread

Returns a new instance of Thread.



362
363
364
# File 'lib/bbiff/bbs_reader.rb', line 362

def initialize(board, id, title, last = 1)
  super
end

Class Method Details

.from_line(line, board) ⇒ Object



353
354
355
356
357
358
359
# 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) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
# 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

Instance Method Details

#posts(range, opts = {}) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/bbiff/bbs_reader.rb', line 366

def posts(range, opts = {})
  fail ArgumentError unless range.is_a? Range
  url = URI(dat_url)
  lines = @board.send(:download_text,
                      if opts[:long_polling] then
                        url + "?long_polling=1"
                      else
                        url
                      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