Class: DailyRep::IndexWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/dailyrep/IndexWriter.rb

Instance Method Summary collapse

Constructor Details

#initialize(index_path = Configer.index_path) ⇒ IndexWriter

Returns a new instance of IndexWriter.



4
5
6
7
# File 'lib/dailyrep/IndexWriter.rb', line 4

def initialize(index_path=Configer.index_path)
  @out_html_path = index_path.to_s
  @out_html = Nokogiri::HTML(File.open(@out_html_path))
end

Instance Method Details

#set_html(entity, params, notif = 0) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/dailyrep/IndexWriter.rb', line 14

def set_html entity, params, notif=0
  entity = entity.downcase
  params.each do |key, val|
    @out_html.css("##{entity}_#{key}")[0].content = val
  end
  @out_html.css("##{entity}_refresh")[0].content = Time.now
  @out_html.css("##{entity}_notif")[0].content = Time.now if notif == 1
end

#set_html_with_diff(entity, scope, diff, reset = false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dailyrep/IndexWriter.rb', line 23

def set_html_with_diff entity, scope, diff, reset=false
  entity = entity.downcase
    target_div = @out_html.css(scope + " .list-group .list-group-item")
    new_root_node = Nokogiri::XML::Node.new('div', @out_html)
    new_root_node['class'] = 'list-group'


    diff.each do |diff_el|
      target_div.each { |node|
        if diff_el[0] == '+' && node['href'].include?(diff_el[2].first[0]) then
          target_div.delete(node)
        end
      }
    end

    new_root_node.add_child(target_div.unlink) if not reset

    diff.each do |diff_el|
      if diff_el[0] == '-' then
        nodeel = Nokogiri::XML::Node.new('a', @out_html)
        nodeel['href'] = diff_el[2].first[0]
        nodeel['class'] = 'list-group-item'
        nodeel.content = diff_el[2].first[1]
        new_root_node.add_child(nodeel)
      end
    end

    @out_html.at_css(scope + " .list-group").replace(new_root_node)

  @out_html.css("##{entity}_refresh")[0].content = Time.now
end

#write_htmlObject



9
10
11
12
# File 'lib/dailyrep/IndexWriter.rb', line 9

def write_html
  @out_html.css("#refresh")[0].content = Time.now
  File.open(@out_html_path , 'w') { |file| file.write(@out_html.to_html) }
end