Class: Readiness::Zendesk::OAuthTokens

Inherits:
Client
  • Object
show all
Defined in:
lib/support_readiness/zendesk/oauth_tokens.rb

Overview

Defines the class OAuthTokens within the module Readiness::Zendesk.

Author:

  • Jason Colyer

Since:

  • 1.0.125

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Client

auth_error, bad_request_error, convert_actions, convert_conditions, convert_standard_names_to_ids, convert_ticket_form_agent_conditions, convert_ticket_form_brands, convert_ticket_form_end_user_conditions, convert_ticket_form_names_to_ids, convert_view_names_to_ids, convert_view_restrictions, covert_ticket_form_field_ids, create_package!, erb_renderer, handle_request_error, not_found_error, not_processible_error, put_into_archive, recursively_deflate_directory, timestamp_filename, to_clean_json, to_clean_json_with_key, to_hash, to_nearly_clean_json, to_nearly_clean_json_with_key, to_param_string, write_entries

Constructor Details

#initialize(object = {}) ⇒ OAuthTokens

Creates a new Readiness::Zendesk::OAuthTokens instance

Examples:

require 'support_readiness'
Readiness::Zendesk::OAuthTokens.new

Parameters:

Author:

  • Jason Colyer

Since:

  • 1.0.125



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 24

def initialize(object = {})
  @client_id = object['client_id']
  @created_at = object['created_at']
  @expires_at = object['expires_at']
  @full_token = object['full_token']
  @id = object['id']
  @refresh_token = object['refresh_token']
  @refresh_token_expires_at = object['refresh_token_expires_at']
  @scopes = object['scopes']
  @token = object['token']
  @used_at = object['used_at']
  @user_id = object['user_id']
end

Instance Attribute Details

#client_idObject

Since:

  • 1.0.125



13
14
15
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 13

def client_id
  @client_id
end

#created_atObject

Since:

  • 1.0.125



13
14
15
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 13

def created_at
  @created_at
end

#expires_atObject

Since:

  • 1.0.125



13
14
15
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 13

def expires_at
  @expires_at
end

#full_tokenObject

Since:

  • 1.0.125



13
14
15
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 13

def full_token
  @full_token
end

#idObject

Since:

  • 1.0.125



13
14
15
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 13

def id
  @id
end

#refresh_tokenObject

Since:

  • 1.0.125



13
14
15
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 13

def refresh_token
  @refresh_token
end

#refresh_token_expires_atObject

Since:

  • 1.0.125



13
14
15
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 13

def refresh_token_expires_at
  @refresh_token_expires_at
end

#scopesObject

Since:

  • 1.0.125



13
14
15
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 13

def scopes
  @scopes
end

#tokenObject

Since:

  • 1.0.125



13
14
15
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 13

def token
  @token
end

#used_atObject

Since:

  • 1.0.125



13
14
15
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 13

def used_at
  @used_at
end

#user_idObject

Since:

  • 1.0.125



13
14
15
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 13

def user_id
  @user_id
end

Class Method Details

.create!(client, token) ⇒ Object

Creates an Oauth token. Will exit if unsuccessful.

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
token = Readiness::Zendesk::OAuthTokens.new
token.client_id = 1234
token.scopes = [
  'impersonate',
  'write'
]
create = Readiness::Zendesk::OAuthTokens.create!(client, token)
pp create.full_token
# => "4v8AR9vXCM7dV4murXlIaBAAzDrAw9lIbaHF7X9VbVPtOABAyy3dnTtHE3A6AdNC"

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.125



148
149
150
151
152
153
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 148

def self.create!(client, token)
  pp to_clean_json_with_key(token, 'token')
  response = client.connection.post 'oauth/tokens', to_clean_json_with_key(token, 'token')
  handle_request_error(1, 'Zendesk', response.status, { action: 'Create Oauth token', message: Oj.load(response.body)}) unless response.status == 201
  OAuthTokens.new(Oj.load(response.body)['token'])
end

.find(client, tid) ⇒ Object

Locates an OAuth token within Zendesk. This will not exit on error (except Authentication errors)

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
oauth_token = Readiness::Zendesk::OAuthTokens.find(client, 223443)
pp oauth_token.client_id
# => 1234

Parameters:

  • client (Object)

    An instance of Client

  • tid (Integer[ The OAuth token ID to find)

    id [Integer[ The OAuth token ID to find

  • return (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.132



90
91
92
93
94
95
96
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 90

def self.find(client, tid)
  response = client.connection.get("oauth/tokens/#{tid}")
  handle_request_error(0, 'Zendesk', response.status,  { action: 'get', id: cid }) unless response.status == 200
  return OAuthTokens.new(Oj.load(response.body)['token']) if response.status == 200

  Oj.load(response.body)
end

.find!(client, tid) ⇒ Object

Locates an OAuth token within Zendesk. This will exit on error

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
oauth_token = Readiness::Zendesk::OAuthTokens.find!(client, 223443)
pp oauth_token.client_id
# => 1234

Parameters:

  • client (Object)

    An instance of Client

  • tid (Integer[ The OAuth token ID to find)

    id [Integer[ The OAuth token ID to find

  • return (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.132



117
118
119
120
121
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 117

def self.find!(client, tid)
  response = client.connection.get("oauth/tokens/#{tid}")
  handle_request_error(1, 'Zendesk', response.status, { action: 'Find OAuth token', id: tid }) unless response.status == 200
  OAuthTokens.new(Oj.load(response.body)['token'])
end

.list(client) ⇒ Array

Lists all OAuth tokens.

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
oauth_tokens = Readiness::Zendesk::OAuthTokens.list(client)
pp oauth_tokens.count
# => 24

Parameters:

  • client (Object)

    An instance of Client

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.132



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 56

def self.list(client)
  array = []
  opts = 'page[size]=100'
  loop do
    response = client.connection.get("oauth/tokens?#{opts}")
    handle_request_error(0, 'Zendesk', response.status) unless response.status == 200
    body = Oj.load(response.body)
    array += body['tokens'].map { |t| OAuthTokens.new(t) }
    break unless body['meta']['has_more']

    opts = body['links']['next'].split('?').last
  end
  array
end

.revoke!(client, token) ⇒ Boolean

Revkokes an OAuth token. Will exit if unsuccessful

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
oauth_token = Readiness::Zendesk::OAuthTokens.find!(client, 223443)
revoke = Readiness::Zendesk::OAuthTokens.revoke!(client, oauth_token)
pp revoke
# => true

Parameters:

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.132



175
176
177
178
179
# File 'lib/support_readiness/zendesk/oauth_tokens.rb', line 175

def self.revoke!(client, token)
  response = client.connection.delete "oauth/tokens/#{token.id}"
  handle_request_error(1, 'Zendesk', response.status, { action: 'Revoke a token', id: token.id, message: Oj.load(response.body)}) unless response.status == 204
  true
end