Created
April 10, 2011 01:52
-
-
Save sosedoff/911970 to your computer and use it in GitHub Desktop.
Rake middleware to bounce off mobile devices and bots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Rack | |
class Restrict | |
REGEX_MOBILE = /(blackberry|motorokr|motorola|sony|windows ce|240x320|176x220|palm|mobile|iphone|ipod|symbian|nokia|samsung|midp)/i | |
REGEX_BOTS = /(google|yahoo|baidu|bot|webalta|ia_archiver)/ | |
attr_reader :options | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
str = env['HTTP_USER_AGENT'] | |
restrict = !str.match(REGEX_MOBILE).nil? || !str.match(REGEX_BOTS).nil? | |
restrict ? forbidden! : @app.call(env) | |
end | |
def forbidden! | |
[403, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, []] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment