Class: AuthTool::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/auth_tool/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, *args) ⇒ Client

Creates either a signet OAuth 1.0 or 2.0 client with additional params.

Parameters:

  • options (Hash)

    Configuration parameters for the client.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/auth_tool/client.rb', line 12

def initialize(options, *args)
  config = options
  credentials = args[0] if args.length == 1
  credentials ||= {}
  raise "Too many args" if args.length > 1
  @has_params = config.has_key?('params')
  self.oauth_version = config.delete('oauth_version')
  self.params = config.delete('params') if @has_params
  if @oauth_version == 1
    oauth1 config
    self.signet.token_credential_key = credentials["oauth_token"] if credentials.has_key? "oauth_token"
    self.signet.token_credential_secret = credentials["oauth_token_secret"] if credentials.has_key? "oauth_token_secret"
  elsif @oauth_version == 2
    oauth2 config
    self.signet.access_token = credentials["oauth_token"] if credentials.has_key? "oauth_token"
    self.signet.refresh_token = credentials["refresh_token"] if credentialsl.has_key? "refresh_token"
  else
    raise "Unexpected oauth_version: #{@oauth_version}"
  end

end

Class Method Details

.refreshObject

Attempts to refresh the access token of the client



47
48
49
50
51
# File 'lib/auth_tool/client.rb', line 47

def self.refresh
  raise "Incorrect OAuth Version" if @oauth_version != 2
  raise "Missing Refresh Token" if self.signet.refresh_token == nil
  self.signet.refresh!
end

Instance Method Details

#has_params?Boolean

Returns if the client has additional parameters.

Returns:

  • (Boolean)

    If the client has additional params.



98
99
100
# File 'lib/auth_tool/client.rb', line 98

def has_params?
  return @has_params
end

#oauth1(config) ⇒ Object



34
35
36
37
# File 'lib/auth_tool/client.rb', line 34

def oauth1 config
  config[:callback] = config.delete(:redirect_uri) if config.has_key?(:redirect_uri)
  self.signet = Signet::OAuth1::Client.new(config)
end

#oauth2(config) ⇒ Object



39
40
41
42
43
# File 'lib/auth_tool/client.rb', line 39

def oauth2 config
  config[:redirect_uri] = config.delete(:callback) if config.has_key?(:callback)
  self.signet = Signet::OAuth2::Client.new(config)
  @signet.additional_parameters = @params if @has_params
end

#oauth_versionInteger

Returns the OAuth version for this client.

Returns:

  • (Integer)

    The OAuth version.



57
58
59
# File 'lib/auth_tool/client.rb', line 57

def oauth_version
  return @oauth_version
end

#paramsHash

Returns the parameters hash for this client.

Returns:

  • (Hash)

    The additional parameters.



65
66
67
# File 'lib/auth_tool/client.rb', line 65

def params
  return @params
end

#signetSignet::OAuth1::Client, Signet::OAuth2::Client

Returns the signet OAuth object for this client.

Returns:

  • (Signet::OAuth1::Client, Signet::OAuth2::Client)

    The signet OAuth object.



73
74
75
# File 'lib/auth_tool/client.rb', line 73

def signet
  return @signet
end

#tokenHash

Returns the final authentication token for the client.

Returns:

  • (Hash)

    The token.



81
82
83
# File 'lib/auth_tool/client.rb', line 81

def token
  @token
end

#token=(token) ⇒ Object

Sets the final authentication token for the client.

Parameters:

  • token (Hash)

    The hash containing the token & secret or the token and refresh token.



90
91
92
# File 'lib/auth_tool/client.rb', line 90

def token= token
  @token = token
end