Class: Rack::Mockjax

Inherits:
Object
  • Object
show all
Defined in:
lib/mockjax/rack.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Mockjax

Returns a new instance of Mockjax.



4
5
6
7
# File 'lib/mockjax/rack.rb', line 4

def initialize(app, options={})
  @app     = app
  @options = options
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
# File 'lib/mockjax/rack.rb', line 9

def call(env)
  @status, @headers, @body = @app.call(env)
  @body = @body.to_a.join
  insert!
  [@status, @headers, [@body]]
end

#insert!Object



16
17
18
19
20
21
22
# File 'lib/mockjax/rack.rb', line 16

def insert!
  mocks = ''.tap do |m|
    Mockjax.mocks.each { |mock| m << "$.mockjax(#{mock.to_json});\n" }
  end
  @body.gsub!(/(<\/head>)/, "<script src='#{Mockjax.path_to_js}' type='text/javascript'></script>\n<script>#{mocks}</script>\\1")
  @headers['Content-Length'] = Rack::Utils.bytesize(@body).to_s
end