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, #details, #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



17
18
19
20
21
22
23
24
25
26
# File 'lib/rack/dedos/filters/country.rb', line 17

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)


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rack/dedos/filters/country.rb', line 28

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

#nameObject



10
11
12
# File 'lib/rack/dedos/filters/country.rb', line 10

def name
  :country
end