Sha256: c40e7cd48217ed07ddc80147e9cd5c5658c28b8a8d8c97966ff8be6031079278

Contents?: true

Size: 939 Bytes

Versions: 39

Compression:

Stored size: 939 Bytes

Contents

# encoding: utf-8

require 'faraday'

module Github
  module Request
    class OAuth2 < Faraday::Middleware
      include Github::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[:url].query = build_query params
          env[:request_headers].merge!(AUTH_HEADER => "Token token=\"#{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 # Github

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
github_api-0.8.6 lib/github_api/request/oauth2.rb
github_api-0.8.5 lib/github_api/request/oauth2.rb
github_api-0.8.4 lib/github_api/request/oauth2.rb
github_api-0.8.3 lib/github_api/request/oauth2.rb
github_api-0.8.2 lib/github_api/request/oauth2.rb
github_api-0.8.1 lib/github_api/request/oauth2.rb
github_api-0.8.0 lib/github_api/request/oauth2.rb
github_api-0.7.2 lib/github_api/request/oauth2.rb
github_api-0.7.1 lib/github_api/request/oauth2.rb
github_api-0.7.0 lib/github_api/request/oauth2.rb
github_api-0.6.5 lib/github_api/request/oauth2.rb
github_api-0.6.4 lib/github_api/request/oauth2.rb
github_api-0.6.3 lib/github_api/request/oauth2.rb
github_api-0.6.2 lib/github_api/request/oauth2.rb
github_api-0.6.1 lib/github_api/request/oauth2.rb
github_api-0.6.0 lib/github_api/request/oauth2.rb
github_api-0.5.4 lib/github_api/request/oauth2.rb
github_api-0.5.3 lib/github_api/request/oauth2.rb
github_api-0.5.2 lib/github_api/request/oauth2.rb