Class: ZendeskAPI::Client
- Inherits:
-
Object
- Object
- ZendeskAPI::Client
- 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, :httpx]
Instance Attribute Summary collapse
-
#account_data ⇒ Hash
readonly
Memoized account data.
-
#callbacks ⇒ Array
readonly
Custom response callbacks.
-
#config ⇒ Configuration
readonly
Config instance.
Class Method Summary collapse
-
.check_deprecated_namespace_usage(attributes, name) ⇒ Object
show a nice warning for people using the old style api.
Instance Method Summary collapse
-
#api_token_impersonate(username) { ... } ⇒ Object
token impersonation for the scope of the block yielded value.
-
#connection ⇒ Faraday::Connection
Creates a connection if there is none, otherwise returns the existing connection.
-
#current_account(reload = false) ⇒ Hash
Returns the current account.
-
#current_locale(reload = false) ⇒ ZendeskAPI::Locale
Returns the current locale.
-
#current_user(reload = false) ⇒ ZendeskAPI::User
Returns the current user (aka me).
- #initialize {|config| ... } ⇒ Client constructor
-
#insert_callback(&block) ⇒ Object
Pushes a callback onto the stack.
-
#method_missing(method, *args) ⇒ Collection
Handles resources such as 'tickets'.
- #refresh_custom_fields_metadata ⇒ Object
- #respond_to?(method, *args) ⇒ Boolean
Constructor Details
#initialize {|config| ... } ⇒ Client
Creates a new ZendeskAPI::Client instance and yields #config.
Requires a block to be given.
Does basic configuration constraints:
- ZendeskAPI::Configuration#url must be https unless ZendeskAPI::Configuration#allow_http is set.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/zendesk_api/client.rb', line 92 def initialize raise ArgumentError, "block not given" unless block_given? @config = ZendeskAPI::Configuration.new yield config @callbacks = [] @resource_cache = {} @account_data = {} check_url check_instrumentation 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) ⇒ 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.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/zendesk_api/client.rb', line 41 def method_missing(method, *args, &) method = method.to_s = args.last.is_a?(Hash) ? args.pop : {} unless config.use_resource_cache resource_class = resource_class_for(method) raise "Resource for #{method} does not exist" unless resource_class return ZendeskAPI::Collection.new(self, resource_class, ) end @resource_cache[method] ||= {class: nil, cache: ZendeskAPI::LRUCache.new} if !.delete(:reload) && (cached = @resource_cache[method][:cache].read(.hash)) cached else @resource_cache[method][:class] ||= resource_class_for(method) raise "Resource for #{method} does not exist" unless @resource_cache[method][:class] @resource_cache[method][:cache].write(.hash, ZendeskAPI::Collection.new(self, @resource_cache[method][:class], )) end end |
Instance Attribute Details
#account_data ⇒ Hash (readonly)
Returns Memoized account data.
36 37 38 |
# File 'lib/zendesk_api/client.rb', line 36 def account_data @account_data end |
#callbacks ⇒ Array (readonly)
Returns Custom response callbacks.
34 35 36 |
# File 'lib/zendesk_api/client.rb', line 34 def callbacks @callbacks end |
#config ⇒ Configuration (readonly)
Returns Config instance.
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
146 147 148 149 150 |
# File 'lib/zendesk_api/client.rb', line 146 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
#api_token_impersonate(username) { ... } ⇒ Object
token impersonation for the scope of the block yielded value
124 125 126 127 128 129 130 |
# File 'lib/zendesk_api/client.rb', line 124 def api_token_impersonate(username) avant = Thread.current[:zendesk_thread_local_username] Thread.current[:zendesk_thread_local_username] = username yield ensure Thread.current[:zendesk_thread_local_username] = avant end |
#connection ⇒ Faraday::Connection
Creates a connection if there is none, otherwise returns the existing connection.
135 136 137 |
# File 'lib/zendesk_api/client.rb', line 135 def connection @connection ||= build_connection end |
#current_account(reload = false) ⇒ Hash
Returns the current account
74 75 76 77 |
# File 'lib/zendesk_api/client.rb', line 74 def current_account(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
81 82 83 84 |
# File 'lib/zendesk_api/client.rb', line 81 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)
67 68 69 70 |
# File 'lib/zendesk_api/client.rb', line 67 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.
141 142 143 |
# File 'lib/zendesk_api/client.rb', line 141 def insert_callback(&block) @callbacks << block end |
#refresh_custom_fields_metadata ⇒ Object
159 160 161 162 |
# File 'lib/zendesk_api/client.rb', line 159 def account_data[:custom_fields] ||= {} ticket_fields.each { |field| account_data[:custom_fields][field.title] = field.id } end |
#respond_to?(method, *args) ⇒ Boolean
60 61 62 63 |
# File 'lib/zendesk_api/client.rb', line 60 def respond_to?(method, *args) cache = @resource_cache[method] !!(cache.to_h[:class] || resource_class_for(method) || super) end |