Class: Gtk2AppLib::Oauth

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(key, secret, hash, prompt = 'Pin:') ⇒ Oauth

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

#access(options = HNIL) ⇒ Object

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(options=HNIL)
  return @tokens if @tokens
  request_token = get_request_token
  if pin = get_pin(options) then
    get_access_token(request_token, pin)
    return @tokens
  end
  return nil
end

#access!(options = HNIL) ⇒ Object

Re-authenticates! and returns the authenticated tokens.



100
101
102
103
# File 'lib/gtk2applib/oauth.rb', line 100

def access!(options=HNIL)
  @tokens = nil
  access(options)
end

#get_access_token(request_token, pin) ⇒ Object

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

#get_pin(options) ⇒ String

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(options)
  case @prompt
  when String
    begin
      DIALOGS.entry(@prompt, options)
    rescue NameError
      print @prompt
      $stdin.gets.strip
    end
  else
    @prompt.method(options).call
  end
end

#get_request_tokenObject

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.authorize_url)
  return request_token
end

#gets_file_tokensObject

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

#puts_file_tokensObject

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