Skip to content

Instantly share code, notes, and snippets.

@uhhuhyeah
Created June 24, 2011 15:31
Show Gist options
  • Save uhhuhyeah/1045031 to your computer and use it in GitHub Desktop.
Save uhhuhyeah/1045031 to your computer and use it in GitHub Desktop.
token_request method in oauth gem
# in ~/.gem/ruby/1.8/gems/oauth-0.4.4/lib/oauth/consumer.rb:196
def token_request(http_method, path, token = nil, request_options = {}, *arguments)
response = request(http_method, path, token, request_options, *arguments)
case response.code.to_i
when (200..299)
if block_given?
yield response.body
else
# symbolize keys
# TODO this could be considered unexpected behavior; symbols or not?
# TODO this also drops subsequent values from multi-valued keys
CGI.parse(response.body).inject({}) do |h,(k,v)|
h[k.strip.to_sym] = v.first
h[k.strip] = v.first
h
end
end
when (300..399)
# this is a redirect
response.error!
when (400..499)
raise OAuth::Unauthorized, response
else
response.error!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment