Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/zendesk_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Client
attr_reader :config
# @return [Array] Custom response callbacks
attr_reader :callbacks
# @return [Hash] Memoized account data
attr_reader :account_data

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

@callbacks = []
@resource_cache = {}
@account_data = {}

check_url
check_instrumentation
Expand All @@ -105,6 +108,7 @@ def initialize
set_token_auth
set_default_logger
add_warning_callback
preload_custom_fields_metadata
end

# token impersonation for the scope of the block
Expand Down Expand Up @@ -266,6 +270,13 @@ def add_warning_callback
end
end

def preload_custom_fields_metadata
return unless @config.preload_custom_fields_metadata

@account_data["custom_fields"] ||= {}
ticket_fields.each { |field| @account_data["custom_fields"][field.title] = field.id }
end

# See https://lostisland.github.io/faraday/middleware/authentication
def set_authentication(builder, config)
if config.access_token && !config.url_based_access_token
Expand Down
3 changes: 3 additions & 0 deletions lib/zendesk_api/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class Configuration
# specify if you want instrumentation to be used
attr_accessor :instrumentation

# set to true if you want to preload custom fields metadata
attr_accessor :preload_custom_fields_metadata

def initialize
@client_options = {}
@use_resource_cache = true
Expand Down
4 changes: 4 additions & 0 deletions lib/zendesk_api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def initialize(client, attributes = {})
@attributes.clear_changes unless new_record?
end

def account_data
@client.account_data
end

# Passes the method onto the attributes hash.
# If the attributes are nested (e.g. { :tickets => { :id => 1 } }), passes the method onto the nested hash.
def method_missing(*args, &)
Expand Down
14 changes: 14 additions & 0 deletions lib/zendesk_api/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,20 @@ def to_param

has_many :incidents, class: Ticket

def custom_field_by_name(name)
raise "Custom fields metadata missing. Enable config.preload_custom_fields_metadata" unless account_data["custom_fields"]

custom_field_id = account_data["custom_fields"][name]
custom_fields.find { |cf| cf["id"] == custom_field_id }["value"]
end

def set_custom_field_by_name(name, value)
raise "Custom fields metadata missing. Enable config.preload_custom_fields_metadata" unless account_data["custom_fields"]

custom_field_id = account_data["custom_fields"][name]
custom_fields.find { |cf| cf["id"] == custom_field_id }["value"] = value
end

# Gets a incremental export of tickets from the start_time until now.
# @param [Client] client The {Client} object to be used
# @param [Integer] start_time The start_time parameter
Expand Down
Loading