Sha256: a86fcc7589cc8ea3cb198d76e76eee41e09500a1fb49144f8c0e096998381a58

Contents?: true

Size: 826 Bytes

Versions: 1

Compression:

Stored size: 826 Bytes

Contents

require 'raterr/version'
require 'raterr/store_container'
require 'raterr/period_builder'
require 'raterr/mixin'
require 'raterr/hour'
require 'raterr/day'
require 'raterr/minute'
require 'redis'

module Raterr

  InvalidStore = Class.new(StandardError)

  AVAILABLE_PERIODS = [:minute, :hour, :day, :week, :month].freeze
  DEFAULTS = {
    max: 100,
    code: 429,
    message: "Rate limit exceeded. Try again in %{time}.",
    period: :hour
  }.freeze

  class << self

    attr_accessor :store

    def enforce(request, **options)
      unless store.is_a?(Hash) || store.is_a?(::Redis)
        raise InvalidStore.new('Store is not valid, please refer to the documentation')
      end

      period = PeriodBuilder.call(request, options)
      period.allowed? ? period.proceed : period.rate_limit_exceeded
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
raterr-0.1.2 lib/raterr.rb