Class: ChemistryKit::Reporting::HtmlReportAssembler

Inherits:
Object
  • Object
show all
Defined in:
lib/chemistrykit/reporting/html_report_assembler.rb

Instance Method Summary collapse

Constructor Details

#initialize(results_path, output_file) ⇒ HtmlReportAssembler

TODO: Refactor to remove this cop exception rubocop:disable MethodLength, ParameterLists



10
11
12
13
14
15
16
17
18
# File 'lib/chemistrykit/reporting/html_report_assembler.rb', line 10

def initialize(results_path, output_file)
  @results_path = results_path
  @output_file = output_file
  @groups = []
  @total_reactions = 0
  @total_failures = 0
  @total_duration = 0
  @total_pending = 0
end

Instance Method Details

#assembleObject



20
21
22
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
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chemistrykit/reporting/html_report_assembler.rb', line 20

def assemble
  result_files = Dir.glob(File.join(@results_path, 'results_*.html'))

  result_files.each do |file|
    doc = Nokogiri.HTML(open(file))

    doc.css('.results').each do |element|
      @total_reactions += element['data-count'].to_i
      @total_failures += element['data-failures'].to_i
      @total_pending += element['data-pendings'].to_i
      @total_duration += element['data-duration'].to_f
    end

    doc.css('.example-group').each do |group|
      @groups << group
    end
  end

  status = @total_failures > 0 ? 'failing' : 'passing'

  File.open(@output_file, 'w') do |file|

    file.write '<!DOCTYPE html>'
    file.write '<!--[if IE 8]>         <html class="no-js lt-ie9" lang="en" > <![endif]-->'
    file.write '<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->'

      file.write get_report_head status
    file.write '<body>'
      file.write get_report_header
      file.write '<div class="report">'
        file.write get_report_summary status, @groups.count, @total_reactions, @total_failures, @total_pending, @total_duration

    @groups.each do |group|
      file.write group
    end

    file.write '</div>' # closes .report

    file.write get_report_endscripts
    file.write '</body>'
    file.write '</html>'

  end

end

#build_fragmentObject



199
200
201
202
203
204
205
# File 'lib/chemistrykit/reporting/html_report_assembler.rb', line 199

def build_fragment
  final = Nokogiri::HTML::DocumentFragment.parse ''
  Nokogiri::HTML::Builder.with(final) do |doc|
    yield doc
  end
  final.to_html
end

#get_report_endscriptsObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/chemistrykit/reporting/html_report_assembler.rb', line 164

def get_report_endscripts
  build_fragment do |doc|
    doc.script do
      doc.text load_from_file(File.join(File.dirname(File.expand_path(__FILE__)), '../../../report/', 'javascripts', 'vendor', 'jquery.js'))
    end
    doc.script do
      doc.text load_from_file(File.join(File.dirname(File.expand_path(__FILE__)), '../../../report/', 'javascripts', 'foundation', 'foundation.js'))
    end
    doc.script do
      doc.text load_from_file(File.join(File.dirname(File.expand_path(__FILE__)), '../../../report/', 'javascripts', 'foundation', 'foundation.section.js'))
    end
    doc.script { doc.text '$(document).foundation();' }
    doc.script do
      doc.text "
        function togglePassing() {
          console.log('passing')
          $('.example.passing').toggle();
          $('.example-group.passing').toggle();
        }

        function toggleFailing() {
          console.log('failing')
          $('.example.failing').toggle();
          $('.example-group.failing').toggle();
        }

        function togglePending() {
          console.log('pending')
          $('.example.pending').toggle();
          $('.example-group.pending').toggle();
        }"
    end
  end
end

#get_report_head(state) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/chemistrykit/reporting/html_report_assembler.rb', line 66

def get_report_head(state)
  build_fragment do |doc|
    doc.head do
      doc.meta(charset: 'utf-8')
      doc.meta(name: 'viewport', content: 'width=device-width')
      doc.title { doc.text "ChemistryKit - #{state.capitalize}" }
      doc.style(type: 'text/css') do
        doc.text load_from_file(File.join(File.dirname(File.expand_path(__FILE__)), '../../../report/', 'stylesheets', 'app.css'))
      end
      doc.link(href: 'http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css', rel: 'stylesheet')
      doc.script do
        doc.text load_from_file(File.join(File.dirname(File.expand_path(__FILE__)), '../../../report/', 'javascripts', 'vendor', 'custom.modernizr.js'))
      end
    end
  end
end

#get_report_headerObject



83
84
85
86
87
88
89
90
91
# File 'lib/chemistrykit/reporting/html_report_assembler.rb', line 83

def get_report_header
  build_fragment do |doc|
    doc.header(class: 'row') do
      doc.div(class: 'large-12 columns') do
        doc.h2 { doc.text 'ChemistryKit Brew Results' }
      end
    end
  end
end

#get_report_summary(status, beakers, reactions, failures, pendings, duration) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/chemistrykit/reporting/html_report_assembler.rb', line 93

def get_report_summary(status, beakers, reactions, failures, pendings, duration)
  if status == 'passing'
    icon = 'icon-ok-sign'
    message = ' Brew Passing'
  else
    icon = 'icon-warning-sign'
    message = ' Brew Failing'
  end
  build_fragment do |doc|
    doc.div(class: 'summary') do
      doc.div(class: "row status #{status}") do
        doc.div(class: 'large-9 columns') do
          doc.h1 do
            doc.i(class: icon)
            doc.text message
          end
        end
        doc.div(class: 'large-3 columns top-row') do
          %w[passing failing pending].each do | type |
            doc.div(class: 'row switch-row') do
              doc.div(class: 'large-6 columns switch-label') do
                doc.text type.capitalize
              end
              doc.div(class: 'large-6 columns') do
                doc.div(class: "#{type}-switch") do
                  opts = { onclick: "toggle#{type.capitalize}();", name: "switch-show-#{type}", type: 'radio' }
                  top = opts.merge(id: "show-#{type}")
                  bot = opts.merge(id: "show-#{type}1")
                  top.merge!(checked: 'checked') unless type == 'passing'
                  bot.merge!(checked: 'checked') if type == 'passing'
                  doc.input(top)
                  doc.label(for: "show-#{type}") { doc.text 'Hide' }
                  doc.input(bot)
                  doc.label(for: "show-#{type}1") { doc.text 'Show' }
                  doc.span
                end
              end
            end
          end
        end
      end
      doc.div(class: 'row details') do
        doc.div(class: 'large-12 columns') do
          doc.ul(class: 'large-block-grid-5') do
            doc.li do
              doc.i(class: 'icon-beaker passing-color')
              doc.p { doc.text "#{beakers.to_s} Beakers" }
            end
            doc.li do
              doc.i(class: 'icon-tint reaction-color')
              doc.p { doc.text "#{reactions.to_s} Reactions" }
            end
            doc.li do
              doc.i(class: 'icon-warning-sign failing-color')
              doc.p { doc.text "#{failures.to_s} Failures" }
            end
            doc.li do
              doc.i(class: 'icon-question-sign pending-color')
              doc.p { doc.text "#{pendings.to_s} Pending" }
            end
            doc.li do
              doc.i(class: 'icon-time')
              doc.p { doc.text "#{ sprintf("%.1i", duration / 60) }m Duration" }
            end
          end
        end
      end
    end
  end
end

#load_from_file(path) ⇒ Object



207
208
209
# File 'lib/chemistrykit/reporting/html_report_assembler.rb', line 207

def load_from_file(path)
  File.open(path, 'rb') { |file| file.read }
end