Avatar

any optimisations on this little rack module?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
module Rack
  class SslBlock
    def initialize app, options = {}
      @app, @options = app, options
    end

    def call env
      if ssl_blocked_request? env, @options
        [403, {'Content-Type' => 'text/plain'}, "Forbidden"]
      else
        @app.call env
      end
    end

    private
      def ssl_blocked_request? env, options
        if env['HTTP_USER_AGENT'] && env['rack.url_scheme'] == 'https' && options.has_key?(:user_agents)
          options[:user_agents].each { |user_agent| return true if env['HTTP_USER_AGENT'].downcase.include? user_agent.downcase }
          false
        end
      end
  end
end

Refactorings

No refactoring yet !

Avatar

https://www.google.com/accounts/o8/id?id=AItOawkgnP7evEXOMIOnxq71Y4yEhDFN2oe3qi4

July 26, 2010, July 26, 2010 14:45, permalink

No rating. Login to rate!

maybe this would be better? thoughts?

1
options[:user_agents].any? { |user_agent| env['HTTP_USER_AGENT'].downcase.include? user_agent.downcase }
Avatar

https://www.google.com/accounts/o8/id?id=AItOawkgnP7evEXOMIOnxq71Y4yEhDFN2oe3qi4

July 27, 2010, July 27, 2010 12:54, permalink

No rating. Login to rate!

better still?

1
options[:user_agents].any? { |user_agent| env['HTTP_USER_AGENT'] =~ /#{user_agent}/i }

Your refactoring





Format Copy from initial code

or Cancel