-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic twitter oauth support and an example on how to use it. Ve…
…ry rough right now and no integration with other methods.
- Loading branch information
1 parent
af2065f
commit 4397254
Showing
4 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
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
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 |
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
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
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 |