class ZendeskSupportAPI::Client

Client class - developer.zendesk.com/rest_api/docs/support/introduction#security-and-authentication

Public Class Methods

new(user, token, url) click to toggle source

Create a new instance of Client

@param user [String] - The API username to use @param token [String] - The API token to use @param url [String] - The API URL to use

@example

ZendeskSupportAPI::Client.new('user', '123', 'zendesk.com/api')
#=> #<ZendeskSupportAPI::Client:0x00007f88779cb330 @user="user",
#=>    @token="123", @url="zendesk.com/api">
# File lib/zendesk_support_api/client.rb, line 24
def initialize(user, token, url)
  @user = user
  @token = token
  @url = url
end

Public Instance Methods

handle_job(job) click to toggle source

Handles responses that create jobs

@param job [Hash] - The output from a request that created a job @return [Hash]

# File lib/zendesk_support_api/client.rb, line 69
def handle_job(job)
  print 'Checking job'
  while job['job_status']['status'] != 'completed'
    print '.'
    job = ZendeskSupportAPI::Jobs.show(self, job['job_status']['id'])
  end
  puts 'completed'
  job['job_status']['results']
end
request(http_method, endpoint, params = {}) click to toggle source

Make a request to the Zendesk Support API

@param http_method [Symbol] The HTTP method to utilize @param endpoint [String] The endpoint to hit @param params [Hash] Parameters for the request @return [Hash]

@example

client = ZendeskSupportAPI::Client.new('user', '123', 'zendesk.com/api')
client.response(:get, 'users.json')
#=> {users:[{user1},{user2}...{user100}]}
# File lib/zendesk_support_api/client.rb, line 42
def request(http_method, endpoint, params = {})
  response = client.public_send(http_method, endpoint, params)
  Oj.load(response.body)
end
spinner(string, num) click to toggle source

Outputs a spinner symbol

@param string [String] The string to output at the beginning @param num [Integer] The index of the iteration @return [String]

@example

ZendeskSupportAPI::Client.spinner('users', 1) #=> Grabbing users... \
ZendeskSupportAPI::Client.spinner('groups', 3) #=> /
# File lib/zendesk_support_api/client.rb, line 57
def spinner(string, num)
  print "Grabbing #{string}... " if num.to_i == 1
  symbols = ['-', '\\', '|', '/']
  print symbols[num.to_i % 4]
  print "\b"
end

Private Instance Methods

client() click to toggle source

Creates a new Faraday instance

@return [ZendeskSupportAPI::Client]

# File lib/zendesk_support_api/client.rb, line 85
def client
  @client ||= Faraday.new(@url) do |c|
    c.request :url_encoded
    c.adapter Faraday.default_adapter
    c.basic_auth "#{@user}/token", @token
  end
end