Class: Teaspoon::Instrumentation

Inherits:
Object
  • Object
show all
Extended by:
Utility
Defined in:
lib/teaspoon/instrumentation.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utility

which

Constructor Details

#initialize(response) ⇒ Instrumentation

Returns a new instance of Instrumentation.



26
27
28
# File 'lib/teaspoon/instrumentation.rb', line 26

def initialize(response)
  @response = response
end

Class Method Details

.add?(response, env) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'lib/teaspoon/instrumentation.rb', line 12

def self.add?(response, env)
  executable &&                                                   # we have an executable
  env["QUERY_STRING"].to_s =~ /instrument=(1|true)/ &&            # the instrument param was provided
  response[0] == 200 &&                                           # the status is 200 (304 might be needed here too)
  response[1]["Content-Type"].to_s == "application/javascript" && # the format is something that we care about
  response[2].respond_to?(:source)                                # it looks like an asset
end

.add_to(response, env) ⇒ Object



7
8
9
10
# File 'lib/teaspoon/instrumentation.rb', line 7

def self.add_to(response, env)
  return response unless add?(response, env)
  Teaspoon::Instrumentation.new(response).instrumented_response
end

.executableObject



20
21
22
23
24
# File 'lib/teaspoon/instrumentation.rb', line 20

def self.executable
  return @executable if @executable_checked
  @executable_checked = true
  @executable = which("istanbul")
end

Instance Method Details

#instrumented_responseObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/teaspoon/instrumentation.rb', line 30

def instrumented_response
  status, headers, asset = @response
  headers, asset = [headers.clone, asset.clone]

  result = add_instrumentation(asset)

  asset.instance_variable_set(:@source, result)
  asset.instance_variable_set(:@length, headers["Content-Length"] = result.bytesize.to_s)

  [status, headers, asset]
end