Class: Github::Request::OAuth2

Inherits:
Faraday::Middleware
  • Object
show all
Includes:
Utils::Url
Defined in:
lib/github_api/request/oauth2.rb

Constant Summary collapse

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

Constants included from Utils::Url

Utils::Url::DEFAULT_QUERY_SEP, Utils::Url::KEY_VALUE_SEP

Instance Method Summary collapse

Methods included from Utils::Url

#build_query, #escape, #escape_uri, #normalize, #parse_query, #parse_query_for_param, #unescape

Constructor Details

#initialize(app, *args) ⇒ OAuth2

Returns a new instance of OAuth2.


27
28
29
30
31
# File 'lib/github_api/request/oauth2.rb', line 27

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

Instance Method Details

#call(env) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/github_api/request/oauth2.rb', line 15

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

#query_params(url) ⇒ Object


33
34
35
36
37
38
39
# File 'lib/github_api/request/oauth2.rb', line 33

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