Sha256: b642f5be8241b128fe75ab5c3d06e0293697821c9dadac0fd1ca26ab82189fc3
Contents?: true
Size: 1.42 KB
Versions: 1
Compression:
Stored size: 1.42 KB
Contents
module Raterr module Mixin attr_reader :request, :options def initialize(request, options) @request = request @options = options end def rate_limit_exceeded { status: options[:code] || Raterr::DEFAULTS[:code], text: Raterr::DEFAULTS[:message] % { time: try_after } } end def allowed? reset_cache if Time.now > rate_period fetch_cache['attempts'] <= max_per_period end def proceed attempts = fetch_cache['attempts'] + 1 set_cache(attempts) { status: 200, attempts: attempts } end private def identifier # TODO: extend with other options from the request request.ip.to_s end def fetch_cache container.resolve(:get) end def set_cache(value) cache_attributes = {}.tap do |cache| cache['attempts'] = value cache['start_time'] = start_time end container.resolve(:set, cache_attributes) end def reset_cache container.resolve(:delete, identifier) end def container @container ||= StoreContainer.new(store: Raterr.store, identifier: identifier) end def start_time start_time = fetch_cache['start_time'] # Depending on the storage option start_time can be # either a Time object or a string return Time.parse(start_time) if start_time.is_a?(String) start_time end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
raterr-0.1.2 | lib/raterr/mixin.rb |