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.
- #name ⇒ Object
Methods inherited from Base
Constructor Details
#initialize ⇒ Country
Returns a new instance of Country.
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 = [: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
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.}") true end |
#name ⇒ Object
10 11 12 |
# File 'lib/rack/dedos/filters/country.rb', line 10 def name :country end |