Sha256: 216e66c32cfe706605fd43a748d69227e72572461ef8de9a77d217ab802f7bfd

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

module Xcflushd
  Authorization = Struct.new(:allowed, :reason) do
    def initialize(authorized, reason = nil)
      super(authorized, authorized ? nil : reason)
    end

    def authorized?
      allowed
    end
  end

  class Authorization
    # This is inevitably tied to the 3scale backend code
    LIMITS_EXCEEDED_CODE = 'limits_exceeded'.freeze
    private_constant :LIMITS_EXCEEDED_CODE

    ALLOWED = new(true).freeze
    private_constant :ALLOWED
    DENIED = new(false).freeze
    private_constant :DENIED
    LIMITS_EXCEEDED = new(false, LIMITS_EXCEEDED_CODE).freeze
    private_constant :LIMITS_EXCEEDED

    private_class_method :new

    def limits_exceeded?
      reason == LIMITS_EXCEEDED_CODE
    end

    def self.allow
      ALLOWED
    end

    def self.deny_over_limits
      LIMITS_EXCEEDED
    end

    def self.deny(reason = nil)
      if reason.nil?
        DENIED
      # this test has to be done in case the code changes
      elsif reason == LIMITS_EXCEEDED_CODE
        LIMITS_EXCEEDED
      else
        new(false, reason)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
xcflushd-1.2.0 lib/xcflushd/authorization.rb
xcflushd-1.1.0 lib/xcflushd/authorization.rb
xcflushd-1.0.0 lib/xcflushd/authorization.rb
xcflushd-1.0.0.rc2 lib/xcflushd/authorization.rb