Class: RogerThemes::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/roger_themes/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/roger_themes/middleware.rb', line 5

def initialize(app, options = {})
  @app = app

  defaults = {
    default_theme: "default",
    shared_folders: ["images", "fonts"]
  }

  @options = defaults.update(options)
  @shared_folders = SharedFolders.new(@options[:shared_folders])
end

Instance Method Details

#call(env) ⇒ Object



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
43
44
45
46
47
48
49
# File 'lib/roger_themes/middleware.rb', line 17

def call(env)
  path = ::Rack::Utils.unescape(env["PATH_INFO"])
  r = /\A\/themes\/([^\/]+)\/theme\//
  if theme = path[r,1]
    orig_path = env["PATH_INFO"].dup
    env["SITE_THEME"] = theme
    env["PATH_INFO"].sub!(r,"")
  else
    # Set default theme
    env["SITE_THEME"] = @options[:default_theme]
  end

  ret = @app.call(env)

  # Fallback for shared images
  unless ret[0] == 200

    shared_path = @shared_folders.local_to_shared_path(path)

    if shared_path
      # Store so we can restore later
      orig_path = env["PATH_INFO"].dup
      env["PATH_INFO"] = shared_path
    end

    ret = @app.call(env)

    # Restore path so we are nice with everybody else
    env["PATH_INFO"] = orig_path
  end

  ret
end