Class: Teabag::Instrumentation

Inherits:
Object
  • Object
show all
Extended by:
Utility
Defined in:
lib/teabag/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.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/teabag/instrumentation.rb', line 27

def initialize(response)
  status, headers, @asset = response
  headers, @asset = [headers.clone, @asset.clone]
  result = process_and_instrument
  length = result.bytesize.to_s

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

  response.replace([status, headers, @asset])
end

Class Method Details

.add?(response, env) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.add?(response, env)
  (
    executable.present? &&                                          # we have an executable
    env["QUERY_STRING"].to_s =~ /instrument=(1|true)/ &&            # the instrument param was provided
    response[0] == 200 &&                                           # the status is 200
    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



21
22
23
24
25
# File 'lib/teabag/instrumentation.rb', line 21

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

.executableObject



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

def self.executable
  @executable ||= which("istanbul")
end