Class: Pic2ch::Parsers::Index

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

Defined Under Namespace

Modules: ElementParser

Instance Method Summary collapse

Constructor Details

#initialize(html) ⇒ Index

Returns a new instance of Index.



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

def initialize(html)
  @html = html
end

Instance Method Details

#codedObject



31
32
33
# File 'lib/pic2ch/parsers/index.rb', line 31

def coded
  @coded ||= all.inject({}){|hash,obj| hash[obj.code] = obj; hash}
end

#construct(e) ⇒ Object

def get(code)

  Static.new(code)
end


43
44
45
46
47
48
49
50
# File 'lib/pic2ch/parsers/index.rb', line 43

def construct(e)
  [
   e.text,                # name
   e["href"],             # link
   ElementParser.next_class_for(e, :created){|e| e.text}, # created
   ElementParser.next_class_for(e, :star   ){|e| e.text}.to_i, # star
  ]
end

#docObject



19
20
21
# File 'lib/pic2ch/parsers/index.rb', line 19

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

#elementsObject



23
24
25
# File 'lib/pic2ch/parsers/index.rb', line 23

def elements
  @elements ||= doc.xpath("/html/body/div[2]/div[2]/a")
end

#pagesObject



35
36
37
# File 'lib/pic2ch/parsers/index.rb', line 35

def pages
  @pages ||= doc.css("div.paginate a span.enable_page").map{|span| Pages::Index.new(span.text)}
end

#threadsObject



27
28
29
# File 'lib/pic2ch/parsers/index.rb', line 27

def threads
  @threads ||= elements.map{|e| Pic2ch::Thread.new(*construct(e))}
end