class Rack::Geofilter

Constants

VERSION

Public Class Methods

new(app) click to toggle source
# File lib/rack/geofilter.rb, line 4
def initialize app
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/geofilter.rb, line 8
def call env
  if should_block?(env)
    return error_message
  end

  @app.call(env)
end

Private Instance Methods

countries_blocked() click to toggle source
# File lib/rack/geofilter.rb, line 22
def countries_blocked
  ENV['BLOCKED_COUNTRIES'].split(',')
end
error_message() click to toggle source
# File lib/rack/geofilter.rb, line 40
def error_message
  [451, {'Content-Type' => 'plain/text'}, ["Service not available in country of origin\n"]]
end
filter_enabled?() click to toggle source
# File lib/rack/geofilter.rb, line 18
def filter_enabled?
  ENV.has_key?('BLOCKED_COUNTRIES')
end
origin_country(env) click to toggle source
# File lib/rack/geofilter.rb, line 26
def origin_country(env)
  env['HTTP_CF_IPCOUNTRY']
end
request_tagged?(env) click to toggle source
# File lib/rack/geofilter.rb, line 30
def request_tagged?(env)
  env.has_key?('HTTP_CF_IPCOUNTRY')
end
should_block?(env) click to toggle source
# File lib/rack/geofilter.rb, line 34
def should_block?(env)
  if filter_enabled? && request_tagged?(env)
    countries_blocked.include? origin_country(env)
  end
end