Class: GSheets::Oauth::Offline

Inherits:
Object
  • Object
show all
Defined in:
lib/g_sheets/oauth/offline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, scope: default_scopes, signet_client: nil) ⇒ Offline

Returns a new instance of Offline.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/g_sheets/oauth/offline.rb', line 8

def initialize(
  client_id,
  client_secret,
  scope: default_scopes,
  signet_client: nil
)
  @client_id = client_id
  @client_secret = client_secret
  @scope = scope
  @signet_client = signet_client || get_signet_client
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



6
7
8
# File 'lib/g_sheets/oauth/offline.rb', line 6

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



6
7
8
# File 'lib/g_sheets/oauth/offline.rb', line 6

def client_secret
  @client_secret
end

#scopeObject (readonly)

Returns the value of attribute scope.



6
7
8
# File 'lib/g_sheets/oauth/offline.rb', line 6

def scope
  @scope
end

#signet_clientObject (readonly)

Returns the value of attribute signet_client.



6
7
8
# File 'lib/g_sheets/oauth/offline.rb', line 6

def signet_client
  @signet_client
end

Instance Method Details

#get_access_token(authentication_code: nil, refresh_token: nil) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
# File 'lib/g_sheets/oauth/offline.rb', line 24

def get_access_token(authentication_code: nil, refresh_token: nil)
  error_message = "you must provide either an authentication_code or refresh_token".freeze
  raise ArgumentError, error_message if authentication_code.nil? && refresh_token.nil?
  @signet_client.code = authentication_code if authentication_code
  @signet_client.refresh_token = refresh_token if refresh_token
  @signet_client.fetch_access_token!
  @signet_client.access_token
end

#get_authentication_uriObject



20
21
22
# File 'lib/g_sheets/oauth/offline.rb', line 20

def get_authentication_uri
  @signet_client.authorization_uri
end

#get_refresh_token(authentication_code:) ⇒ Object



33
34
35
36
37
# File 'lib/g_sheets/oauth/offline.rb', line 33

def get_refresh_token(authentication_code:)
  @signet_client.code = authentication_code
  @signet_client.fetch_access_token!
  @signet_client.refresh_token
end