Sha256: 8c88c80833e32c74da68c1390f1d2b7a77659381878eca91d15e2a2fb04affc8

Contents?: true

Size: 1.54 KB

Versions: 56

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module Faraday
  class Request
    # Request middleware for the Authorization HTTP header
    class Authorization < Faraday::Middleware
      unless defined?(::Faraday::Request::Authorization::KEY)
        KEY = 'Authorization'
      end

      # @param type [String, Symbol]
      # @param token [String, Symbol, Hash]
      # @return [String] a header value
      def self.header(type, token)
        case token
        when String, Symbol
          "#{type} #{token}"
        when Hash
          build_hash(type.to_s, token)
        else
          raise ArgumentError,
                "Can't build an Authorization #{type}" \
                  "header from #{token.inspect}"
        end
      end

      # @param type [String]
      # @param hash [Hash]
      # @return [String] type followed by comma-separated key=value pairs
      # @api private
      def self.build_hash(type, hash)
        comma = ', '
        values = []
        hash.each do |key, value|
          values << "#{key}=#{value.to_s.inspect}"
        end
        "#{type} #{values * comma}"
      end

      # @param app [#call]
      # @param type [String, Symbol] Type of Authorization
      # @param token [String, Symbol, Hash] Token value for the Authorization
      def initialize(app, type, token)
        @header_value = self.class.header(type, token)
        super(app)
      end

      # @param env [Faraday::Env]
      def call(env)
        env.request_headers[KEY] = @header_value unless env.request_headers[KEY]
        @app.call(env)
      end
    end
  end
end

Version data entries

56 entries across 45 versions & 12 rubygems

Version Path
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/2.7.0/gems/faraday-1.3.0/lib/faraday/request/authorization.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/faraday-1.1.0/lib/faraday/request/authorization.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/faraday-1.1.0/lib/faraday/request/authorization.rb
faraday-1.4.2 lib/faraday/request/authorization.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/faraday-1.4.1/lib/faraday/request/authorization.rb
faraday-1.4.1 lib/faraday/request/authorization.rb
faraday-1.4.0 lib/faraday/request/authorization.rb
faraday-1.3.1 lib/faraday/request/authorization.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/faraday-1.2.0/lib/faraday/request/authorization.rb
tdiary-5.1.5 vendor/bundle/ruby/2.7.0/gems/faraday-1.3.0/lib/faraday/request/authorization.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/faraday-1.1.0/lib/faraday/request/authorization.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/faraday-1.1.0/lib/faraday/request/authorization.rb
logstash-output-newrelic-1.2.0 vendor/bundle/jruby/2.5.0/gems/faraday-1.3.0/lib/faraday/request/authorization.rb
faraday-1.3.0 lib/faraday/request/authorization.rb
faraday-1.2.0 lib/faraday/request/authorization.rb
tdiary-5.1.4 vendor/bundle/ruby/2.7.0/gems/faraday-1.1.0/lib/faraday/request/authorization.rb
passbase-1.2.0 vendor/bundle/ruby/2.7.0/gems/faraday-1.1.0/lib/faraday/request/authorization.rb
passbase-1.1.0 vendor/bundle/ruby/2.7.0/gems/faraday-1.1.0/lib/faraday/request/authorization.rb
passbase-1.0.3 vendor/bundle/ruby/2.7.0/gems/faraday-1.1.0/lib/faraday/request/authorization.rb
faraday-1.1.0 lib/faraday/request/authorization.rb