Sha256: 45c514d148f339148c5ec1e7f84289d3e9710d4cbb9b25e5b3feb4de5a3289a6

Contents?: true

Size: 880 Bytes

Versions: 6

Compression:

Stored size: 880 Bytes

Contents

# encoding: utf-8

require 'faraday'

module Nimbu
  module Request
    class OAuth2 < Faraday::Middleware
      include Nimbu::Utils::Url

      ACCESS_TOKEN = 'access_token'.freeze
      AUTH_HEADER  = 'Authorization'.freeze

      dependency 'oauth2'

      def call(env)
        # Extract parameters from the query
        params = { ACCESS_TOKEN => @token }.update query_params(env[:url])

        if token = params[ACCESS_TOKEN] and !token.empty?
          env[:request_headers].merge!(AUTH_HEADER => "bearer #{token}")
        end

        @app.call env
      end

      def initialize(app, *args)
        super app
        @app = app
        @token = args.shift
      end

      def query_params(url)
        if url.query.nil? or url.query.empty?
          {}
        else
          parse_query url.query
        end
      end
    end # OAuth2
  end # Request
end # Nimbu

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
nimbu-api-0.5.0 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.4.4 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.4.3 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.4.2 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.4.1 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.4.0 lib/nimbu-api/request/oauth2.rb