Class: Gtk2AppLib::Oauth
- Inherits:
-
Object
- Object
- Gtk2AppLib::Oauth
- Defined in:
- lib/gtk2applib/oauth.rb
Overview
Gkt2AppLib::Oauth uses gem oauth, OAuth Core Ruby implementation. It maintains the file, access_token, in the user's application hidden directory. It provides a dialog to enter the autorizing pin.
Instance Method Summary (collapse)
-
- (Object) access(options = HNIL)
Get previously authenticated tokens for a service, or re-authenticates and get new authenticated tokens.
-
- (Object) access!(options = HNIL)
Re-authenticates! and returns the authenticated tokens.
-
- (Object) get_access_token(request_token, pin)
Gets authenticated tokens from servicing site.
-
- (String) get_pin(options)
Calls DIALOGS.entry if @prompt is a String, else calls prompt.method(options).call in which case it's expected to return the String pin number.
-
- (Object) get_request_token
Get request tokens from servicing site.
-
- (Object) gets_file_tokens
Reads previously authenticated tokens.
-
- (Oauth) initialize(key, secret, hash, prompt = 'Pin:')
constructor
oauth = Gtk2AppLib::Oauth.new(key, secret,
{ :site=>'http://sevicing_site.com'}). -
- (Object) puts_file_tokens
Saves the authenticated tokens.
Constructor Details
- (Oauth) initialize(key, secret, hash, prompt = 'Pin:')
oauth = Gtk2AppLib::Oauth.new(key, secret,
{ :site=>'http://sevicing_site.com'})
14 15 16 17 18 19 |
# File 'lib/gtk2applib/oauth.rb', line 14 def initialize(key, secret, hash, prompt='Pin:') @prompt = prompt @consumer = OAuth::Consumer.new(key, secret, hash) @tokens_file = File.join(Constants::USERDIR, 'access_token') (File.exist?(@tokens_file))? self.gets_file_tokens: (@tokens = nil) end |
Instance Method Details
- (Object) access(options = HNIL)
Get previously authenticated tokens for a service, or re-authenticates and get new authenticated tokens.
86 87 88 89 90 91 92 93 94 |
# File 'lib/gtk2applib/oauth.rb', line 86 def access(=HNIL) return @tokens if @tokens request_token = get_request_token if pin = get_pin() then get_access_token(request_token, pin) return @tokens end return nil end |
- (Object) access!(options = HNIL)
Re-authenticates! and returns the authenticated tokens.
100 101 102 103 |
# File 'lib/gtk2applib/oauth.rb', line 100 def access!(=HNIL) @tokens = nil access() end |
- (Object) get_access_token(request_token, pin)
Gets authenticated tokens from servicing site
47 48 49 50 51 |
# File 'lib/gtk2applib/oauth.rb', line 47 def get_access_token(request_token, pin) access = request_token.get_access_token(:oauth_verifier => pin) @tokens = [access.token, access.secret] puts_file_tokens end |
- (String) get_pin(options)
Calls DIALOGS.entry if @prompt is a String, else calls prompt.method(options).call in which case it's expected to return the String pin number.
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/gtk2applib/oauth.rb', line 67 def get_pin() case @prompt when String begin DIALOGS.entry(@prompt, ) rescue NameError print @prompt $stdin.gets.strip end else @prompt.method().call end end |
- (Object) get_request_token
Get request tokens from servicing site
55 56 57 58 59 |
# File 'lib/gtk2applib/oauth.rb', line 55 def get_request_token request_token = @consumer.get_request_token System.run(request_token.) return request_token end |
- (Object) gets_file_tokens
Reads previously authenticated tokens
33 34 35 36 37 38 39 40 41 |
# File 'lib/gtk2applib/oauth.rb', line 33 def gets_file_tokens @tokens = [] fh = File.open(@tokens_file, 'r') while token = fh.gets do @tokens.push(token.strip) end $stderr.puts "Read #{@tokens_file}" if $trace fh.close end |
- (Object) puts_file_tokens
Saves the authenticated tokens
23 24 25 26 27 28 29 |
# File 'lib/gtk2applib/oauth.rb', line 23 def puts_file_tokens fh = File.open(@tokens_file, 'w') @tokens.each { |token| fh.puts token } $stderr.puts "Wrote #{@tokens_file}" if $trace fh.close File.chmod(0600, @tokens_file) end |