Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sosedoff
Created April 10, 2011 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sosedoff/911970 to your computer and use it in GitHub Desktop.
Save sosedoff/911970 to your computer and use it in GitHub Desktop.
Rake middleware to bounce off mobile devices and bots
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