Sha256: e8dca1efee967922e8a2de41b73f86baa011898f75e0ae21a8d134146341274d

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

require "base64"

module Nexaas
  module Throttle
    class Guardian
      def initialize(request, configuration)
        @request = request
        @token = configuration.request_identifier.new(request).token
        @ignored_user_agents = configuration.ignored_user_agents
      end

      def throttle!
        validate { token }
      end

      def track!
        validate { true }
      end

      private

      attr_reader :request, :token, :ignored_user_agents

      def validate
        return if ignore_user_agents? || assets? || !api? || token.blank?
        request.env["nexaas.token"] = token
        yield if block_given?
      end

      def assets?
        path = request.path
        path.match(/\/assets/).present? || path.match(extensions_regexp).present?
      end

      def api?
        content_type = (request.media_type || request.env["Content-Type"]).to_s
        %w(application/json application/xml).include?(content_type)
      end

      def extensions_regexp
        @assets_extensions ||= begin
          extensions = %w(css js png jpg gif)
          /\.(#{extensions.join("|")})/
        end
      end

      def ignore_user_agents?
        ignored_user_agents && ignored_user_agents.include?(request.user_agent)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nexaas-throttle-0.2.0 lib/nexaas/throttle/guardian.rb