class Mona::Thread

Thread of 2ch.net

Attributes

board[R]
dat_size[R]
id[R]
last_accessed_at[R]
res_num[R]
title[R]

Public Class Methods

from_url(url) click to toggle source
# File lib/mona/thread.rb, line 31
def self.from_url(url)
  matched = url.match %r{http://(.+)/test/read.cgi/(.+)/(.+)/}
  raise "Invalid Url" unless matched
  board = Mona::Board.new(matched[1], matched[2])
  new(:board => board, :id => matched[3].to_i)
end
new(args = {}) click to toggle source

initialize method

required options

:board

instance of Board

:id

thread id

optionals

:title

title of thread

:res_num

number of res

:last_accessed_at

timestamp of last accessed time

:dat_size

size of known dat file

# File lib/mona/thread.rb, line 22
def initialize(args = {})
  @board = args[:board]
  @title = args[:title]
  @id = args[:id]
  @res_num = args[:res_num]
  @last_accessed_at = Time.at(args[:last_accessed_at] || 0)
  @dat_size = args[:dat_size] || 0
end

Public Instance Methods

parse_body(body) click to toggle source
# File lib/mona/thread.rb, line 38
def parse_body(body)
  first = body.lines.first.strip.split(/<>/)
  @title = first[4] if first[4]
  body.lines.map{ |line| Mona::Response.parse_line(line) }
end
reload() click to toggle source
# File lib/mona/thread.rb, line 44
def reload
  headers = {'If-Modified-Since' => @last_accessed_at.rfc2822, 'Range' => @dat_size}
  res = Mona::Client.new.get(dat_url, :header => headers)

  case res.status
  when 200
    @dat_size += res.body.bytesize
    @last_accessed_at = Time.rfc2822(res.header["Last-Modified"].first)
    body = res.body.toutf8
    parse_body(body)
  end
end

Private Instance Methods

dat_url() click to toggle source

url for thread dat

# File lib/mona/thread.rb, line 61
def dat_url
  "http://#{board.host}/#{board.board}/dat/#{id}.dat"
end