class Jscov::Bless

Public Class Methods

new(response) click to toggle source
# File lib/jscov/bless.rb, line 3
def initialize(response)
  @response = response
end

Private Class Methods

js_code() click to toggle source
# File lib/jscov/bless.rb, line 56
      def js_code
        <<~JS
          window.addEventListener("unload", __jscov_dumpCoverage)

          function __jscov_dumpCoverage() {
            const cov = window.__coverage__
            if (!cov) { return }

            console.log('__jscov', JSON.stringify(cov))
          }
        JS
      end

Public Instance Methods

result() click to toggle source
# File lib/jscov/bless.rb, line 7
def result
  [
    @response[0],
    headers,
    blessed_body,
  ]
end

Private Instance Methods

bless(fragment) click to toggle source
# File lib/jscov/bless.rb, line 40
def bless(fragment)
  index = fragment.index(/<head>/i)
  if index
    fragment.insert(index + "<head>".size, script_tag)
  else
    fragment
  end
end
blessed_body() click to toggle source
# File lib/jscov/bless.rb, line 21
def blessed_body
  plain_body = @response[2]
  return plain_body unless html?

  blessed = []
  plain_body.each do |fragment|
    blessed << bless(fragment)
  end

  plain_body.close if plain_body.respond_to?(:close)

  blessed
end
headers() click to toggle source
# File lib/jscov/bless.rb, line 17
def headers
  @response[1]
end
html?() click to toggle source
# File lib/jscov/bless.rb, line 35
def html?
  content_type = headers["Content-Type"]
  content_type =~ /text\/html/
end
script_tag() click to toggle source
# File lib/jscov/bless.rb, line 49
def script_tag
  tag = "<script>#{self.class.js_code}</script>"
  tag = tag.html_safe if tag.respond_to?(:html_safe)
  tag
end