Class: Multigiri

Inherits:
Object
  • Object
show all
Defined in:
lib/multigiri.rb,
lib/multigiri/html5.rb,
lib/multigiri/minify.rb,
lib/multigiri/version.rb,
lib/multigiri/link_checker.rb,
lib/multigiri/email_obfuscator.rb,
lib/multigiri/google_analytics.rb,
lib/multigiri/default_attributes.rb

Overview

Examples:

use Multigiri do
  use GoogleAnalytics, :my_tracking_code if Rango.production?
end

Defined Under Namespace

Modules: HTML5 Classes: DefaultAttributes, EmailObfuscator, GoogleAnalytics, LinkChecker, Minify

Constant Summary collapse

VERSION =
"0.0.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = Hash.new, &block) ⇒ Multigiri

Returns a new instance of Multigiri.



15
16
17
18
# File 'lib/multigiri.rb', line 15

def initialize(app, options = Hash.new, &block)
  @app, @options, @block = app, options, block
  @options[:indentation] ||= 2
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



14
15
16
# File 'lib/multigiri.rb', line 14

def block
  @block
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/multigiri.rb', line 14

def options
  @options
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/multigiri.rb', line 20

def call(env)
  # convert to a Nokogiri document
  status, headers, chunks = @app.call(env)
  # the only what we know about body is that it has to respond to #each
  body = String.new
  chunks.each { |chunk| body += chunk }
  document = Nokogiri::HTML(body) # before
  @stack = lambda { |env| [status, headers, document] }

  # get middlewares
  self.instance_eval(&@block) if @block

  # convert back to a [String]
  status, headers, document = @stack.call(env)
  body = document.to_html(indentation: options[:indentation]) # TODO
  headers["Content-Length"] = body.bytesize.to_s
  [status, headers, [body]]
end

#use(klass, *args, &block) ⇒ Object

GoogleAnalytics.new(Other.new(self), tracking_code)



40
41
42
# File 'lib/multigiri.rb', line 40

def use(klass, *args, &block)
  @stack = klass.new(@stack, *args, &block)
end