Sha256: ae14c6dacd765f3f73eb05716e56f612786a782818fc890fae27384fb333da9a

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

class SpecInjector
  def initialize app
    @app = app
  end

  def call env
    @env = env
    
    status, headers, response = @app.call( env )

    if status != 200
      puts red( "capybara-jasmine HTTP request error: #{ status } #{ env[ 'PATH_INFO' ] }")
    else
      # puts green( "capybara-jasmine HTTP request success: #{ status } #{ env[ 'PATH_INFO' ] }")
    end

    if headers[ 'Content-Type' ] && headers[ 'Content-Type' ].include?( 'text/html' )
      body = body_for( response )
      
      headers[ 'Content-Length' ] = body.length.to_s

      [ status, headers, [ body ]]
    else  
      [ status, headers, response ]
    end  
  end

  def specs
    return [] if @env[ 'HTTP_X_SPECS' ].nil?

    @env[ 'HTTP_X_SPECS' ].split( ',' ).map( &:strip )
  end

  def body_for response
    body = ''

    response.each{| p | body += p.gsub( '<head>', "<head>#{ jasmine_requires }#{ header_specs }" )}

    body
  end

  def header_specs
    "\n" + specs.map do |spec|
      "<script src='/jasmine/js/#{ spec }.js'></script>"
    end.join( "\n" ) + "\n"
  end

  def jasmine_requires_files
    [ 
      'vendor/jquery.2.1.3.min.js',
      'jasmine_lib/jasmine.js' ,
      'jasmine_lib/jasmine-html.js',
      'jasmine_lib/boot.js',
      'jasmine_lib/mock-ajax.js',
      'js/SharedHelpers.js'
    ]
  end

  def jasmine_requires
    "\n" + jasmine_requires_files.map do |f|
      "<script src='/capybara-jasmine-files/#{ f }'></script>"
    end.join( "\n" )
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
capybara-jasmine-0.1.4 lib/app/spec_injector.rb
capybara-jasmine-0.1.3 lib/app/spec_injector.rb
capybara-jasmine-0.1.2 lib/app/spec_injector.rb
capybara-jasmine-0.1.1 lib/app/spec_injector.rb