Sha256: f0638cc7d30c73012e348a5ab47de106a514200719eb58207bbce5e39521db1f

Contents?: true

Size: 1.21 KB

Versions: 8

Compression:

Stored size: 1.21 KB

Contents

module Rack
  class API
    module Middleware
      class Limit
        attr_reader :options, :env

        def initialize(app, options = {})
          @app = app
          @options = {
            :limit  => 60,
            :key    => "REMOTE_ADDR",
            :with   => Redis.new
          }.merge(options)
        end

        def call(env)
          @env = env

          if authorized?
            @app.call(env)
          else
            [503, {"Content-Type" => "text/plain"}, ["Over Rate Limit."]]
          end
        rescue Exception => e
          @app.call(env)
        end

        private
        def authorized? # :nodoc:
          count = redis.incr(key)
          redis.expire(key, 3600)

          count <= options[:limit] || redis.sismember("api:whitelist", identifier)
        end

        def redis # :nodoc:
          options[:with]
        end

        def identifier # :nodoc:
          @identifier ||= begin
            options[:key].respond_to?(:call) ? options[:key].call(env).to_s : env[options[:key].to_s]
          end
        end

        def key # :nodoc:
          @key ||= begin
            "api:#{identifier}:#{Time.now.strftime("%Y%m%d%H")}"
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rack-api-1.1.0 lib/rack/api/middleware/limit.rb
rack-api-1.0.2 lib/rack/api/middleware/limit.rb
rack-api-1.0.1 lib/rack/api/middleware/limit.rb
rack-api-1.0.0 lib/rack/api/middleware/limit.rb
rack-api-0.3.1 lib/rack/api/middleware/limit.rb
rack-api-0.3.0 lib/rack/api/middleware/limit.rb
rack-api-0.2.2 lib/rack/api/middleware/limit.rb
rack-api-0.2.1 lib/rack/api/middleware/limit.rb