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

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

oauth = Gtk2AppLib::Oauth.new(key, secret,

{ :site=>'http://sevicing_site.com'})

Parameters:

  • key (String)
  • secret (String)
  • hash (Hash)

    configuration data

  • prompt (String, Object) (defaults to: 'Pin:')

    or Object from which to get the pin number



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.

Parameters:

  • options (Hash, Symbol) (defaults to: HNIL)

    for DIALOGS.entry or method on Object @prompt

Returns:

  • tokens on success or nil on failure



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

- (Object) access!(options = HNIL)

Re-authenticates! and returns the authenticated tokens.

Parameters:

  • options (Hash, Symbol) (defaults to: HNIL)

    for DIALOGS.entry or method on Object @prompt

Returns:

  • tokens on success or nil on failure



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

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

- (Object) get_access_token(request_token, pin)

Gets authenticated tokens from servicing site

Parameters:

  • request_token
  • pin (String)

    number



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.

Parameters:

  • options (Hash, Symbol)

    for DIALOGS.entry or method on Object @prompt

Returns:



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

- (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.authorize_url)
  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