Class: Rack::EnvNotifier::BodyInjector
- Inherits:
-
Object
- Object
- Rack::EnvNotifier::BodyInjector
- Defined in:
- lib/rack/env_notifier/body_injector.rb
Constant Summary collapse
- BODY_TAG_REGEX =
Lookup for <body> tag and inject notification after
/<body>|<body[^(er)][^<]*>/
Instance Attribute Summary collapse
-
#content_length ⇒ Object
readonly
Returns the value of attribute content_length.
-
#new_body ⇒ Object
readonly
Returns the value of attribute new_body.
-
#notification_added ⇒ Object
readonly
Returns the value of attribute notification_added.
Instance Method Summary collapse
-
#initialize(body, text_to_be_injected) ⇒ BodyInjector
constructor
A new instance of BodyInjector.
- #inject!(env) ⇒ Object
Constructor Details
#initialize(body, text_to_be_injected) ⇒ BodyInjector
Returns a new instance of BodyInjector.
10 11 12 13 |
# File 'lib/rack/env_notifier/body_injector.rb', line 10 def initialize(body, text_to_be_injected) @body = body @text_to_be_injected = text_to_be_injected end |
Instance Attribute Details
#content_length ⇒ Object (readonly)
Returns the value of attribute content_length.
8 9 10 |
# File 'lib/rack/env_notifier/body_injector.rb', line 8 def content_length @content_length end |
#new_body ⇒ Object (readonly)
Returns the value of attribute new_body.
8 9 10 |
# File 'lib/rack/env_notifier/body_injector.rb', line 8 def new_body @new_body end |
#notification_added ⇒ Object (readonly)
Returns the value of attribute notification_added.
8 9 10 |
# File 'lib/rack/env_notifier/body_injector.rb', line 8 def notification_added @notification_added end |
Instance Method Details
#inject!(env) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rack/env_notifier/body_injector.rb', line 15 def inject!(env) @env = env @body.close if @body.respond_to?(:close) # Convert String body to Array so it can respond to each method # In test environment body may be a String object @body = [@body] if @body.is_a? String @new_body = [] @body.each { |line| @new_body << line.to_s } @content_length = 0 @notification_added = false @new_body.each do |line| if !@notification_added && line['<body'] line.gsub! (BODY_TAG_REGEX) {|match| %{#{match}\n#{@text_to_be_injected}} } @notification_added = true end # Keep track of content_length @content_length += line.bytesize end @new_body = @body end |