mercredi 22 mai 2019

How can I access a constant defined in one initializer file into another initializer file in rails?

I have defined a YAML file like this throttling_request.yml

logged_in_user:
  watching_timespan: 60
  allowed_requests:  60
  blocking_timespan: 300

non_logged_in_user:
  watching_timespan: 300
  allowed_requests:  300

I load the YML file in config/intializers/throttle_config.rb

config = YAML.load_file('config/throttling_request.yml').with_indifferent_access
THROTTLE_REQ_NON_LOGGED_IN = config[:non_logged_in_user]
THROTTLE_REQ_LOGGED_IN     = config[:logged_in_user]


But I want to access this constant THROTTLE_REQ_NON_LOGGED_IN into another initializer file config/initializers/rack_attack.rb

  configs = ::THROTTLE_REQ_NON_LOGGED_IN

  # Throttle all requests by IP (20rpm)
  #
  # Key: "rack::attack:#{Time.now.to_i/:period}:req/ip:#{req.ip}"
  throttle('req/ip', limit: configs[:allowed_requests], period: configs[:watching_timespan]) do |req|
    req.ip unless req.path.start_with?('/assets')
  end

I could not able to load with or without scope resolution operator :: for constant THROTTLE_REQ_NON_LOGGED_IN. I want to use constants THROTTLE_REQ_LOGGED_IN for my other class and THROTTLE_REQ_NON_LOGGED_IN in rack attack config. So I don't want to mix up calling this constant in rack attack initializer file.

Please let me know if any other information needed from me.

Aucun commentaire:

Enregistrer un commentaire