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



28
29
30
# File 'lib/teaspoon/instrumentation.rb', line 28

def initialize(response)
  @response = response
end

Class Method Details

.add?(response, env) ⇒ Boolean



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

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 maybe?)
    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
    !ignored?(response[2])                                          # it is not ignored
end

.add_to(response, env) ⇒ Object



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

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

.executableObject



22
23
24
25
26
# File 'lib/teaspoon/instrumentation.rb', line 22

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

Instance Method Details

#instrumented_responseObject



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

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