Class: ZendeskAPI::Client

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

Overview

The top-level class that handles configuration and connection to the Zendesk API. Can also be used as an accessor to resource collections.

Constant Summary collapse

GZIP_EXCEPTIONS =
[:em_http, :httpclient]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|config| ... } ⇒ Client

Creates a new ZendeskAPI::Client instance and yields #config.

Requires a block to be given.

Does basic configuration constraints:

Yields:

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/zendesk_api/client.rb', line 89

def initialize
  raise ArgumentError, "block not given" unless block_given?

  @config = ZendeskAPI::Configuration.new
  yield config

  @callbacks = []
  @resource_cache = {}

  check_url

  config.retry = !!config.retry # nil -> false

  set_raise_error_when_rated_limited

  set_token_auth

  set_default_logger
  add_warning_callback
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Collection

Handles resources such as ‘tickets’. Any options are passed to the underlying collection, except reload which disregards memoization and creates a new Collection instance.

Returns:

  • (Collection)

    Collection instance for resource



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/zendesk_api/client.rb', line 39

def method_missing(method, *args, &block)
  method = method.to_s
  options = args.last.is_a?(Hash) ? args.pop : {}

  unless config.use_resource_cache
    raise "Resource for #{method} does not exist" unless method_as_class(method)
    return ZendeskAPI::Collection.new(self, method_as_class(method), options)
  end

  @resource_cache[method] ||= { :class => nil, :cache => ZendeskAPI::LRUCache.new }
  if !options.delete(:reload) && (cached = @resource_cache[method][:cache].read(options.hash))
    cached
  else
    @resource_cache[method][:class] ||= method_as_class(method)
    raise "Resource for #{method} does not exist" unless @resource_cache[method][:class]
    @resource_cache[method][:cache].write(options.hash, ZendeskAPI::Collection.new(self, @resource_cache[method][:class], options))
  end
end

Instance Attribute Details

#callbacksArray (readonly)

Returns Custom response callbacks.

Returns:

  • (Array)

    Custom response callbacks



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

def callbacks
  @callbacks
end

#configConfiguration (readonly)

Returns Config instance.

Returns:



32
33
34
# File 'lib/zendesk_api/client.rb', line 32

def config
  @config
end

Class Method Details

.check_deprecated_namespace_usage(attributes, name) ⇒ Object

show a nice warning for people using the old style api



125
126
127
128
129
# File 'lib/zendesk_api/client.rb', line 125

def self.check_deprecated_namespace_usage(attributes, name)
  if attributes[name].is_a?(Hash)
    raise "un-nest '#{name}' from the attributes"
  end
end

Instance Method Details

#connectionFaraday::Connection

Creates a connection if there is none, otherwise returns the existing connection.

Returns:

  • (Faraday::Connection)

    Faraday connection for the client



113
114
115
116
# File 'lib/zendesk_api/client.rb', line 113

def connection
  @connection ||= build_connection
  return @connection
end

#current_account(reload = false) ⇒ Hash

Returns the current account

Returns:

  • (Hash)

    The attributes of the current account or nil



71
72
73
74
# File 'lib/zendesk_api/client.rb', line 71

def (reload = false)
  return @current_account if @current_account && !reload
  @current_account = SilentMash.new(connection.get('account/resolve').body)
end

#current_locale(reload = false) ⇒ ZendeskAPI::Locale

Returns the current locale

Returns:



78
79
80
81
# File 'lib/zendesk_api/client.rb', line 78

def current_locale(reload = false)
  return @locale if @locale && !reload
  @locale = locales.find(:id => 'current')
end

#current_user(reload = false) ⇒ ZendeskAPI::User

Returns the current user (aka me)

Returns:



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

def current_user(reload = false)
  return @current_user if @current_user && !reload
  @current_user = users.find(:id => 'me')
end

#insert_callback(&block) ⇒ Object

Pushes a callback onto the stack. Callbacks are executed on responses, last in the Faraday middleware stack.

Parameters:

  • block (Proc)

    The block to execute. Takes one parameter, env.



120
121
122
# File 'lib/zendesk_api/client.rb', line 120

def insert_callback(&block)
  @callbacks << block
end

#respond_to?(method, *args) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/zendesk_api/client.rb', line 58

def respond_to?(method, *args)
  ((cache = @resource_cache[method]) && cache[:class]) || !method_as_class(method).nil? || super
end