Class: FixMicrosoftLinks::Rack::Response

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

Constant Summary collapse

USER_AGENTS_REGEX =
/[^\w](Word|Excel|PowerPoint|ms-office)([^\w]|\z)/
EXCLUDE_USER_AGENTS_REGEX =
/Microsoft Outlook/

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Response

Returns a new instance of Response.



7
8
9
# File 'lib/fix_microsoft_links.rb', line 7

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/fix_microsoft_links.rb', line 11

def call(env)
  # Clicking a URL from Microsoft apps will redirect you to login to already authenticated sites otherwise :(
  # http://support.microsoft.com/kb/899927
  if matching_user_agent?(env["HTTP_USER_AGENT"])
    body = StringIO.new("<html><head><meta http-equiv='refresh' content='0'/></head><body></body></html>")
    return [200, {"Content-Type" => "text/html"}, body]
  end
  @app.call(env)
end

#matching_user_agent?(user_agent) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/fix_microsoft_links.rb', line 21

def matching_user_agent?(user_agent)
  (user_agent =~ USER_AGENTS_REGEX) && !(user_agent =~ EXCLUDE_USER_AGENTS_REGEX)
end