Class: Rack::Dedos::Filters::Country
- Defined in:
- lib/rack/dedos/filters/country.rb
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #allowed?(request, ip) ⇒ Boolean
-
#initialize ⇒ Country
constructor
A new instance of Country.
Methods inherited from Base
Constructor Details
#initialize ⇒ Country
Returns a new instance of Country.
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 = [:maxmind_db_file] or fail "MaxMind database file not set" @allowed = case when @countries = [:allowed_countries] then true when @countries = [: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
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 |