Created
March 21, 2009 19:05
-
-
Save wr0ngway/82946 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'oauth' | |
require 'oauth/client' | |
require 'oauth/token' | |
require 'oauth/consumer' | |
require 'activeresource' | |
require 'pp' | |
module OAuth | |
module Rails | |
module ActiveResourceBaseMethods | |
def self.included receiver | |
receiver.extend ClassMethods | |
ActiveResource::Connection.send :include, OAuth::Rails::ActiveResourceConnectionMethods | |
end | |
module ClassMethods | |
def with_oauth(access_token, &block) | |
begin | |
Thread.current[:oauth_access_token] = access_token | |
block.call() | |
ensure | |
Thread.current[:oauth_access_token] = nil | |
end | |
end | |
end | |
end | |
module ActiveResourceConnectionMethods | |
def self.included receiver | |
receiver.alias_method_chain :http, :oauth | |
end | |
def http_with_oauth | |
Thread.current[:oauth_access_token] || http_without_oauth | |
end | |
end | |
end | |
end | |
ActiveResource::Base.send :include, OAuth::Rails::ActiveResourceBaseMethods | |
include OAuth | |
consumer_token_str = "" | |
consumer_secret = "" | |
access_token_str = "" | |
access_secret = "" | |
site = 'http://localhost:3000' | |
consumer = Consumer.new consumer_token_str, consumer_secret, :site => site | |
access_token = AccessToken.new consumer, access_token_str, access_secret | |
module Client | |
class User < ActiveResource::Base | |
self.site = "http://localhost:3000/" | |
end | |
end | |
u = Client::User.find(:first) | |
pp u | |
ActiveResource::Base.with_oauth(access_token) do | |
u = Client::User.find(:first) | |
pp u | |
end | |
u = Client::User.find(:first) | |
pp u |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment