Class: Rack::Dedos::Filters::Country

Inherits:
Base
  • Object
show all
Defined in:
lib/rack/dedos/filters/country.rb

Constant Summary

Constants inherited from Base

Base::DEFAULT_OPTIONS

Instance Attribute Summary

Attributes inherited from Base

#app, #options

Instance Method Summary collapse

Methods inherited from Base

#call

Constructor Details

#initializeCountry

Returns a new instance of Country.

Parameters:

  • options (Hash)

    a customizable set of options



13
14
15
16
17
18
19
20
21
22
# File 'lib/rack/dedos/filters/country.rb', line 13

def initialize(*)
  super
  @maxmind_db_file = options[:maxmind_db_file] or fail "MaxMind database file not set"
  @allowed = case
    when @countries = options[:allowed_countries] then true
    when @countries = options[:denied_countries] then false
    else fail "neither allowed nor denied countries set"
  end
  maxmind_db   # hit once to fail on errors at boot
end

Instance Method Details

#allowed?(request, ip) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rack/dedos/filters/country.rb', line 24

def allowed?(request, ip)
  if country = maxmind_db.get(ip)
    country_code = country.dig('country', 'iso_code').to_sym
    @countries.include?(country_code) ? @allowed : !@allowed
  else   # not found in database
    true
  end
rescue => error
  warn("rack-dedos: request from #{ip} allowed due to error: #{error.message}")
  true
end