Class: Weary::Middleware::OAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/weary/middleware/oauth.rb

Constant Summary

AUTH_HEADER =
"HTTP_AUTHORIZATION"

Instance Method Summary (collapse)

Constructor Details

- (OAuth) initialize(app, oauth_options)

Returns a new instance of OAuth



11
12
13
14
# File 'lib/weary/middleware/oauth.rb', line 11

def initialize(app, oauth_options)
  @app = app
  @oauth = oauth_options
end

Instance Method Details

- (Object) call(env)



16
17
18
19
# File 'lib/weary/middleware/oauth.rb', line 16

def call(env)
  env.update AUTH_HEADER => sign(env).to_s
  @app.call(env)
end

- (Object) request_body_to_hash(qs, d = nil)

Stolen from Rack::Utils



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/weary/middleware/oauth.rb', line 32

def request_body_to_hash(qs, d = nil)
  params = Rack::Utils::KeySpaceConstrainedParams.new
  default_sep = /[&;] */n

  (qs || '').split(d ? /[#{d}] */n : default_sep).each do |p|
    k, v = p.split('=', 2).map { |s| Rack::Utils.unescape(s, defined?(::Encoding) ? Encoding::BINARY : nil) }

    Rack::Utils.normalize_params(params, k, v)
  end

  return params.to_params_hash
end

- (Object) sign(env)



21
22
23
24
25
26
27
28
29
# File 'lib/weary/middleware/oauth.rb', line 21

def sign(env)
  req = Rack::Request.new(env)
  post_body = req.body.read
  req.body.rewind
  SimpleOAuth::Header.new req.request_method,
                          req.url,
                          request_body_to_hash(post_body),
                          @oauth
end