Skip to content

Commit d71f21b

Browse files
committed
add option to preload custom fields metadata
1 parent 300f4e4 commit d71f21b

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

lib/zendesk_api/client.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class Client
3232
attr_reader :config
3333
# @return [Array] Custom response callbacks
3434
attr_reader :callbacks
35+
# @return [Hash] Memoized account data
36+
attr_reader :account_data
3537

3638
# Handles resources such as 'tickets'. Any options are passed to the underlying collection, except reload which disregards
3739
# memoization and creates a new Collection instance.
@@ -95,6 +97,7 @@ def initialize
9597

9698
@callbacks = []
9799
@resource_cache = {}
100+
@account_data = {}
98101

99102
check_url
100103
check_instrumentation
@@ -105,6 +108,7 @@ def initialize
105108
set_token_auth
106109
set_default_logger
107110
add_warning_callback
111+
preload_custom_fields_metadata
108112
end
109113

110114
# token impersonation for the scope of the block
@@ -266,6 +270,13 @@ def add_warning_callback
266270
end
267271
end
268272

273+
def preload_custom_fields_metadata
274+
return unless @config.preload_custom_fields_metadata
275+
276+
@account_data['custom_fields'] ||= {}
277+
ticket_fields.each { |field| @account_data['custom_fields'][field.title] = field.id }
278+
end
279+
269280
# See https://lostisland.github.io/faraday/middleware/authentication
270281
def set_authentication(builder, config)
271282
if config.access_token && !config.url_based_access_token

lib/zendesk_api/configuration.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class Configuration
5757
# specify if you want instrumentation to be used
5858
attr_accessor :instrumentation
5959

60+
# set to true if you want to preload custom fields metadata
61+
attr_accessor :preload_custom_fields_metadata
62+
6063
def initialize
6164
@client_options = {}
6265
@use_resource_cache = true

lib/zendesk_api/resource.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def initialize(client, attributes = {})
7676
@attributes.clear_changes unless new_record?
7777
end
7878

79+
def account_data
80+
@client.account_data
81+
end
82+
7983
# Passes the method onto the attributes hash.
8084
# If the attributes are nested (e.g. { :tickets => { :id => 1 } }), passes the method onto the nested hash.
8185
def method_missing(*args, &)

lib/zendesk_api/resources.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,20 @@ def to_param
546546

547547
has_many :incidents, class: Ticket
548548

549+
def custom_field_by_name(name)
550+
raise "Custom fields metadata missing. Enable config.preload_custom_fields_metadata" unless account_data['custom_fields']
551+
552+
custom_field_id = account_data['custom_fields'][name]
553+
custom_fields.find { |cf| cf['id'] == custom_field_id }["value"]
554+
end
555+
556+
def set_custom_field_by_name(name, value)
557+
raise "Custom fields metadata missing. Enable config.preload_custom_fields_metadata" unless account_data['custom_fields']
558+
559+
custom_field_id = account_data['custom_fields'][name]
560+
custom_fields.find { |cf| cf['id'] == custom_field_id }["value"] = value
561+
end
562+
549563
# Gets a incremental export of tickets from the start_time until now.
550564
# @param [Client] client The {Client} object to be used
551565
# @param [Integer] start_time The start_time parameter

0 commit comments

Comments
 (0)