Class: Rango::Middlewares::Basic

Inherits:
Object
  • Object
show all
Defined in:
lib/rango/rack/middlewares/basic.rb

Constant Summary collapse

@@media_prefix =
""

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, &block) ⇒ Basic

Returns a new instance of Basic.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rango/rack/middlewares/basic.rb', line 30

def initialize(app, &block)
  @@called = false unless defined?(@@called) # I'm not entirely sure why we have to do this
  @app = app#.extend(Rango::RackDebug)

  #, key: 'rack.session', domain: 'foo.com', path: '/', expire_after: 2592000, secret: 'change_me'
  self.before = [Rango::Middlewares::Encoding, Rack::MethodOverride, [Rack::Session::Cookie, path: '/']]
  self.after = [Rack::ContentType, Rack::ContentLength, Rack::Head]

  self.static_files_serving

  block.call(self) if block_given?
  # use Rango::Middlewares::Basic do |middleware|
  #   middleware.before.push MyMiddleware
  # end
end

Instance Attribute Details

#afterObject

Returns the value of attribute after.



29
30
31
# File 'lib/rango/rack/middlewares/basic.rb', line 29

def after
  @after
end

#beforeObject

Returns the value of attribute before.



29
30
31
# File 'lib/rango/rack/middlewares/basic.rb', line 29

def before
  @before
end

Class Method Details

.media_prefixObject



19
20
21
# File 'lib/rango/rack/middlewares/basic.rb', line 19

def self.media_prefix
  @@media_prefix
end

.media_prefix=(media_prefix) ⇒ Object



23
24
25
# File 'lib/rango/rack/middlewares/basic.rb', line 23

def self.media_prefix=(media_prefix)
  @@media_prefix = media_prefix
end

Instance Method Details

#call(env) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rango/rack/middlewares/basic.rb', line 46

def call(env)
  # Matryoshka principle
  # MethodOverride.new(Encoding.new(@app))
  unless @@called
    middlewares = self.before.reverse + self.after.reverse
    @app = middlewares.inject(@app) do |app, klass|
      args = Array.new
      if klass.is_a?(Array)
        klass, args = klass
        args = [args]
        Rango.logger.debug("#{klass}.new(app, #{args.map { |arg| arg.inspect }.join(", ")})")
      else
        Rango.logger.debug("#{klass}.new(app)")
      end
      klass.new(app, *args).extend(RackDebug)
    end
  end
  @@called = true
  @app.call(env)
end

#static_files_servingObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rango/rack/middlewares/basic.rb', line 67

def static_files_serving
  if self.class.media_prefix.empty?
    Rango.logger.info("Media files are routed directly to the /")
    require "rango/rack/middlewares/static"
    self.before.unshift Rango::Middlewares::Static
  else
    # use Rack::Static, :urls => ["/media"]
    # will serve all requests beginning with /media from the "media" folder
    # located in the current directory (ie media/*).

    # use Rack::Static, :urls => ["/css", "/images"], :root => "public"
    # will serve all requests beginning with /css or /images from the folder
    # "public" in the current directory (ie public/css/* and public/images/*)
    Rango.logger.info("Media files are available on #{self.class.media_prefix}")
    options = {urls: [self.class.media_prefix]}
    self.before.unshift [Rack::Static, options]
  end
end