Class: Ungarbled::Middleware

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

Constant Summary collapse

DISPOSITION_KEY =
'Content-Disposition'
DISPOSITION_REGEX =
/^(?:attachment|inline); filename="(.+)"$/

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Middleware.



8
9
10
11
# File 'lib/ungarbled/middleware.rb', line 8

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

Instance Method Details

#call(env) ⇒ Object



13
14
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/ungarbled/middleware.rb', line 13

def call(env)
  req = Rack::Request.new(env)
  # Argument changes depending on version of browser
  browser =
    ##  Browser.method(:new).parameters
    ##  => [[:rest]]
    if Browser.method(:new).parameters.flatten.count == 1
      browser = Browser.new(
        ua: req.user_agent,
        accept_language: req.env['HTTP_ACCEPT_LANGUAGE']
      )
    else
      ## Browser.method(:new).parameters.flatten
      ## => [:req, :user_agent, :keyrest, :kwargs]
      browser = Browser.new(
        req.user_agent,
        accept_language: req.env['HTTP_ACCEPT_LANGUAGE']
      )
    end
  encoder = Ungarbled::Encoder.new(browser, @options)
  status, headers, res = @app.call(env)
  disposition = headers[DISPOSITION_KEY]

  if disposition
    headers[DISPOSITION_KEY] =
      encode_disposition(encoder, disposition)
  end

  [status, headers, res]
end

#encode_disposition(encoder, disposition) ⇒ Object



44
45
46
47
48
# File 'lib/ungarbled/middleware.rb', line 44

def encode_disposition(encoder, disposition)
  replacing = DISPOSITION_REGEX.match(disposition)[1]
  replaced = encoder.encode(replacing)
  disposition.gsub(replacing, replaced)
end