Class: Tumble::Request::TumblrOAuth

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/tumble/request/oauth.rb

Constant Summary collapse

AUTH_HEADER =
'Authorization'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ TumblrOAuth

Returns a new instance of TumblrOAuth.



11
12
13
14
# File 'lib/tumble/request/oauth.rb', line 11

def initialize(app, options)
  super(app)
  @options = options
end

Instance Method Details

#body_params(env) ⇒ Object



40
41
42
# File 'lib/tumble/request/oauth.rb', line 40

def body_params(env)
  env[:body] || {}
end

#call(env) ⇒ Object



16
17
18
19
# File 'lib/tumble/request/oauth.rb', line 16

def call(env)
  env[:request_headers][AUTH_HEADER] ||= oauth_header(env).to_s if sign_request?(env)
  @app.call(env)
end

#oauth_header(env) ⇒ Object



21
22
23
24
25
26
# File 'lib/tumble/request/oauth.rb', line 21

def oauth_header(env)
  SimpleOAuth::Header.new env[:method],
                          env[:url].to_s,
                          signature_params(body_params(env)),
                          oauth_options(env)
end

#oauth_options(env) ⇒ Object



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

def oauth_options(env)
  if extra = env[:request][:oauth] and extra.is_a? Hash and !extra.empty?
    @options.merge extra
  else
    @options
  end
end

#sign_request?(env) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/tumble/request/oauth.rb', line 28

def sign_request?(env)
  !!env[:request].fetch(:oauth, true)
end

#signature_params(params) ⇒ Object



44
45
46
47
# File 'lib/tumble/request/oauth.rb', line 44

def signature_params(params)
  params.empty? ? params :
    params.reject {|k,v| v.respond_to?(:content_type) }
end