Created
July 29, 2010 14:02
-
-
Save Dagnan/498196 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class CommandLineApp < BasicApp | |
def open_url(url) | |
if RUBY_PLATFORM =~ /darwin/ | |
`open #{url}` | |
elsif RUBY_PLATFORM =~ /linux/ && `which xdg-open` != "" | |
`xdg-open #{url}` | |
elsif RUBY_PLATFORM =~ /mswin|mingw|bccwin|wince|em/ | |
`start #{url}` | |
else | |
puts "Please open #{@authorize_url + request_token.token} in a web browser. " | |
end | |
end | |
# NB. The callback url has to be a url that your browser can access. | |
# This means it may be an 'app://'-style-url for phones like Android or iPhone. | |
def authorize!(oauth_callback = 'http://localhost:3001/', options = {}) | |
if File.exists?(ENV["HOME"] + "/.soocial.token") | |
read_token | |
else | |
request_token(:oauth_callback => oauth_callback) do |request_token| | |
open_url(request_token.authorize_url) | |
puts "Please enter the oauth verifier code" # you don't normally dsessiono this manually | |
pin = STDIN.readline.chomp | |
# if the application is authorized by the user, foo can acquire an access token. Let's try: | |
authorize(request_token.token, request_token.secret, :oauth_verifier => pin) | |
save_token | |
end | |
end | |
end | |
def save_token(path = ENV["HOME"] + "/.soocial.token") | |
File.open(path, 'w', 0600) do |f| | |
f.write(@token + "\n" + @secret) | |
end | |
end | |
def read_token(path = ENV["HOME"] + "/.soocial.token") | |
values = [] | |
begin | |
File.open(path, 'r').each { |line| values << line.chomp } | |
rescue | |
puts "Unable to process #{path}" | |
end | |
@access_token = OAuth::AccessToken.new(consumer, values[0], values[1]) | |
@token = @access_token.token | |
@secret = @access_token.secret | |
end | |
### end of OAuth process | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment