Class: Pic2ch::Parsers::Thread

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

Instance Method Summary collapse

Constructor Details

#initialize(thread) ⇒ Thread

Returns a new instance of Thread.



7
8
9
# File 'lib/pic2ch/parsers/thread.rb', line 7

def initialize(thread)
  @thread = thread
end

Instance Method Details

#countObject



26
27
28
29
30
31
32
33
34
# File 'lib/pic2ch/parsers/thread.rb', line 26

def count
  unless @count
    check_deleted
    count = doc.css('h2').text.scan(/\((\d+)\)$/).flatten.first.to_s
    raise NotFound, "count" if count.empty?
    @count = count.to_i
  end
  return @count
end

#deleted?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
# File 'lib/pic2ch/parsers/thread.rb', line 19

def deleted?
  check_deleted
  return false
rescue DeletedPage
  return true
end

#docObject



15
16
17
# File 'lib/pic2ch/parsers/thread.rb', line 15

def doc
  @doc ||= Nokogiri::HTML(html)
end

#htmlObject



11
12
13
# File 'lib/pic2ch/parsers/thread.rb', line 11

def html
  @html ||= open(url).read{}
end

#imagesObject



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

def images
  @images ||= jpgs.map{|jpg| image_url + "/pics3/#{time}/#{jpg}" }
end

#images_with_thumbsObject



62
63
64
# File 'lib/pic2ch/parsers/thread.rb', line 62

def images_with_thumbs
  @images_with_thumbs ||= [images, thumbs].transpose
end

#jpgsObject



36
37
38
39
40
41
42
43
44
# File 'lib/pic2ch/parsers/thread.rb', line 36

def jpgs
  unless @jpgs
    check_deleted
    urls = extract(:urls).split(/:/)
    raise ParseError, "expected #{count} urls exist, but got #{urls.size}" unless urls.size == count
    @jpgs = urls
  end
  return @jpgs
end

#thumbsObject



54
55
56
# File 'lib/pic2ch/parsers/thread.rb', line 54

def thumbs
  @thumbs ||= jpgs.map{|jpg| image_url + "/thumbs/#{time}/#{jpg}" }
end

#timeObject



46
47
48
# File 'lib/pic2ch/parsers/thread.rb', line 46

def time
  @time ||= extract(:id).to_i
end

#titleObject



50
51
52
# File 'lib/pic2ch/parsers/thread.rb', line 50

def title
  @title ||= doc.search("title").text
end