Class: Mona::Thread

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

Overview

Thread of 2ch.net

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Thread

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



22
23
24
25
26
27
28
29
# 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

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



9
10
11
# File 'lib/mona/thread.rb', line 9

def board
  @board
end

#dat_sizeObject (readonly)

Returns the value of attribute dat_size.



9
10
11
# File 'lib/mona/thread.rb', line 9

def dat_size
  @dat_size
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/mona/thread.rb', line 9

def id
  @id
end

#last_accessed_atObject (readonly)

Returns the value of attribute last_accessed_at.



9
10
11
# File 'lib/mona/thread.rb', line 9

def last_accessed_at
  @last_accessed_at
end

#res_numObject (readonly)

Returns the value of attribute res_num.



9
10
11
# File 'lib/mona/thread.rb', line 9

def res_num
  @res_num
end

#titleObject (readonly)

Returns the value of attribute title.



9
10
11
# File 'lib/mona/thread.rb', line 9

def title
  @title
end

Class Method Details

.from_url(url) ⇒ Object



31
32
33
34
35
36
# 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

Instance Method Details

#parse_body(body) ⇒ Object



38
39
40
41
42
# 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

#reloadObject



44
45
46
47
48
49
50
51
52
53
54
55
# 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