Sha256: e960aa53858dcc68003a3ea971cff679b3feb4819ac0cb0d6939c2fb61edffab

Contents?: true

Size: 980 Bytes

Versions: 2

Compression:

Stored size: 980 Bytes

Contents

# encoding: utf-8

require 'faraday'
require 'oauth2'

require_relative '../utils/url'

module Github
  class 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}")
        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

2 entries across 2 versions & 1 rubygems

Version Path
github_api2-1.0.1 lib/github_api2/request/oauth2.rb
github_api2-1.0.0 lib/github_api2/request/oauth2.rb