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.



29
30
31
32
33
# File 'lib/github_api/request/oauth2.rb', line 29

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

Instance Method Details

#call(env) ⇒ Object



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

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



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

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