Class: SpecInjector

Inherits:
Object
  • Object
show all
Defined in:
lib/app/spec_injector.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ SpecInjector

Returns a new instance of SpecInjector.



2
3
4
# File 'lib/app/spec_injector.rb', line 2

def initialize app
  @app = app
end

Instance Method Details

#body_for(response) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/app/spec_injector.rb', line 34

def body_for response
  body = ''

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

  body
end

#call(env) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/app/spec_injector.rb', line 6

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

#header_specsObject



42
43
44
45
46
# File 'lib/app/spec_injector.rb', line 42

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

#jasmine_requiresObject



59
60
61
62
63
# File 'lib/app/spec_injector.rb', line 59

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

#jasmine_requires_filesObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/app/spec_injector.rb', line 48

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

#specsObject



28
29
30
31
32
# File 'lib/app/spec_injector.rb', line 28

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

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