Class: BitBucket::Request::OAuth

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

Constant Summary collapse

AUTH_HEADER =
'Authorization'

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, parse_query, parse_query_for_param, unescape

Constructor Details

#initialize(app, *args) ⇒ OAuth

Returns a new instance of OAuth.



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

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

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bitbucket_rest_api/request/oauth.rb', line 13

def call(env)
  # Extract parameters from the query
  request = Rack::Request.new env
  env[:url] = URI.parse(request.url) if env[:url].nil?
  params = {}.update query_params(env[:url])

  if (@token && @secret) && (!@token.empty? && !@secret.empty?)
    access_token = ::OAuth::AccessToken.new(@consumer, @token, @secret)
    env[:url].query = build_query params

    puts oauth_helper.header
    puts oauth_helper.header.class
    env[:request_headers].merge!(AUTH_HEADER => oauth_helper.header)
  end

  env[:url].query = build_query params

  @app.call env
end

#query_params(url) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/bitbucket_rest_api/request/oauth.rb', line 41

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