Class: Mata::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/mata/agent.rb

Instance Method Summary collapse

Instance Method Details

#insert(status, headers, body) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mata/agent.rb', line 5

def insert(status, headers, body)
  return [status, headers, body] unless html_response?(headers)

  content = extract(body)
  return [status, headers, [content]] unless content.include?("</head>")

  script_tag = '<script src="/__mata/client.js"></script>'
  modified_content = content.sub("</head>", "#{script_tag}\n</head>")

  headers["Content-Length"] = modified_content.bytesize.to_s if headers["Content-Length"]

  [status, headers, [modified_content]]
end