Skip to content

Commit

Permalink
Added basic twitter oauth support and an example on how to use it. Ve…
Browse files Browse the repository at this point in the history
…ry rough right now and no integration with other methods.
  • Loading branch information
jnunemaker committed Mar 29, 2009
1 parent af2065f commit 4397254
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Echoe.new(ProjectName, Twitter::Version) do |p|
p.url = "http://#{ProjectName}.rubyforge.org"
p.author = "John Nunemaker"
p.email = "[email protected]"
p.extra_deps = [['hpricot', '>= 0.6'], ['activesupport', '>= 2.1'], ['httparty', '>= 0.2.4']]
p.extra_deps = [['oauth', '>= 0.3.2'], ['hpricot', '>= 0.6'], ['activesupport', '>= 2.1'], ['httparty', '>= 0.2.4']]
p.need_tar_gz = false
p.docs_host = WebsitePath
end
Expand Down
60 changes: 60 additions & 0 deletions examples/oauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require File.join(File.dirname(__FILE__), '..', 'lib', 'twitter')
require 'pp'

class ConfigStore
attr_reader :file

def initialize(file)
@file = file
end

def load
@config ||= YAML::load(open(file))
self
end

def [](key)
load
@config[key]
end

def []=(key, value)
@config[key] = value
end

def update(c={})
@config.merge!(c)
save
end

def save
File.open(file, 'w') { |f| f.write(YAML.dump(@config)) }
end
end

config = ConfigStore.new("#{ENV['HOME']}/.twitter")
oauth = Twitter::OAuth.new(config['token'], config['secret'])

if config['atoken'] && config['asecret']
oauth.authorize_from_access(config['atoken'], config['asecret'])
puts oauth.access_token.get("/statuses/friends_timeline.json")

elsif config['rtoken'] && config['rsecret']
oauth.authorize_from_request(config['rtoken'], config['rsecret'])
puts oauth.access_token.get("/statuses/friends_timeline.json")

config.update({
'atoken' => oauth.access_token.token,
'asecret' => oauth.access_token.secret,
'rtoken' => nil,
'rsecret' => nil,
})
else
config.update({
'rtoken' => oauth.request_token.token,
'rsecret' => oauth.request_token.secret,
})

# authorize in browser
%x(open #{oauth.request_token.authorize_url})
end
6 changes: 6 additions & 0 deletions lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
require 'yaml'
require 'time'
require 'rubygems'

gem 'hpricot'
require 'hpricot'

gem 'oauth'
require 'oauth'

$:.unshift(File.dirname(__FILE__))
require 'twitter/version'
require 'twitter/easy_class_maker'
require 'twitter/oauth'
require 'twitter/base'
require 'twitter/user'
require 'twitter/search'
Expand Down
32 changes: 32 additions & 0 deletions lib/twitter/oauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Twitter
class OAuth
attr_reader :token, :secret

def initialize(ctoken, csecret)
@ctoken, @csecret = ctoken, csecret
end

def request_token
@request_token ||= consumer.get_request_token
end

def authorize_from_request(rtoken, rsecret)
request_token = ::OAuth::RequestToken.new(consumer, rtoken, rsecret)
access_token = request_token.get_access_token
@atoken, @asecret = access_token.token, access_token.secret
end

def access_token
@access_token ||= ::OAuth::AccessToken.new(consumer, @atoken, @asecret)
end

def authorize_from_access(atoken, asecret)
@atoken, @asecret = atoken, asecret
end

private
def consumer
@consumer ||= ::OAuth::Consumer.new(@ctoken, @csecret, {:site => 'http://twitter.com'})
end
end
end

0 comments on commit 4397254

Please sign in to comment.