Sha256: 09f8410eba7cd9697f871c30bd48c6f5778c91eb35a9d7e65dedfa5654669eef

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

module DebugExtras
  class Injector
    def initialize(response)
      @response = response
      @result = @response.body
    end

    def process_response
      if response_is_html?
        inject_messages
        inject_styles
      end
      @result
    end

    private

    def response_is_html?
      return false unless @response.headers['Content-Type'].try('include?', 'html')
      tags = %w[<html <head </head> <body </body> </html>]
      tags.each do |tag|
        return false unless @response.body.include? tag
      end
      true
    end

    def inject_messages
      return if $debug_extras_messages.blank?
      $debug_extras_messages.map! { |message| DebugExtras::Dumper.new(message, 'debug-wp').render }
      injection = $debug_extras_messages.join('')
      @result = inject_content('<body') do |html|
        body = html.second.split('>')
        body.second.prepend(injection) if body.size > 1
        html[1] = body.join('>')
      end
    end

    def inject_styles
      return unless $debug_extras_add_styles
      injection = File.read(File.expand_path('../templates/styles.html', __FILE__))
      @result = inject_content('</head>') { |html| html.first << injection }
    end

    def inject_content(tag)
      html = @result.split(tag)
      yield html if block_given?
      html.join(tag)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
debug-extras-0.4.3 lib/debug_extras/injector.rb