Skip to content

Commit 4eb2647

Browse files
committed
fetch ticket field data after first custom field call
1 parent 4558c72 commit 4eb2647

File tree

6 files changed

+31
-48
lines changed

6 files changed

+31
-48
lines changed

lib/zendesk_api/client.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ def initialize
108108
set_token_auth
109109
set_default_logger
110110
add_warning_callback
111-
load_custom_fields_metadata
112111
end
113112

114113
# token impersonation for the scope of the block
@@ -157,11 +156,9 @@ def self.check_deprecated_namespace_usage(attributes, name)
157156
end
158157
end
159158

160-
def load_custom_fields_metadata
161-
return unless @config.preload_custom_fields_metadata
162-
163-
@account_data["custom_fields"] ||= {}
164-
ticket_fields.each { |field| @account_data["custom_fields"][field.title] = field.id }
159+
def refresh_custom_fields_metadata
160+
account_data[:custom_fields] ||= {}
161+
ticket_fields.each { |field| account_data[:custom_fields][field.title] = field.id }
165162
end
166163

167164
protected

lib/zendesk_api/error.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,5 @@ def generate_error_msg(response_body)
4040
class NetworkError < ClientError; end
4141
class RecordNotFound < ClientError; end
4242
class RateLimited < ClientError; end
43-
44-
class CustomFieldsMetadataConfigurationError < StandardError
45-
def to_s
46-
"Custom fields metadata missing. Enable config.preload_custom_fields_metadata"
47-
end
48-
end
4943
end
5044
end

lib/zendesk_api/resource.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ def account_data
8080
@client.account_data
8181
end
8282

83+
def refresh_custom_fields_metadata
84+
@client.refresh_custom_fields_metadata
85+
end
86+
8387
# Passes the method onto the attributes hash.
8488
# If the attributes are nested (e.g. { :tickets => { :id => 1 } }), passes the method onto the nested hash.
8589
def method_missing(*args, &)

lib/zendesk_api/resources.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -551,21 +551,24 @@ def initialize(ticket)
551551
@ticket = ticket
552552
end
553553

554-
def [](name)
555-
find_by_name(name)["value"]
554+
def [](field_name)
555+
find_by_name(field_name)["value"]
556556
end
557557

558-
def []=(name, value)
559-
find_by_name(name)["value"] = value
558+
def []=(field_name, value)
559+
find_by_name(field_name)["value"] = value
560560
end
561561

562562
private
563563

564-
def find_by_name(name)
565-
raise ZendeskAPI::Error::CustomFieldsMetadataConfigurationError unless @ticket.account_data["custom_fields"]
564+
def find_by_name(field_name)
565+
@ticket.refresh_custom_fields_metadata unless @ticket.account_data.has_key?(:custom_fields)
566566

567-
custom_field_id = @ticket.account_data["custom_fields"][name]
568-
@ticket.custom_fields.find { |cf| cf["id"] == custom_field_id }
567+
custom_field_id = @ticket.account_data[:custom_fields][field_name]
568+
custom_field = @ticket.custom_fields.find { |cf| cf["id"] == custom_field_id }
569+
raise ArgumentError if custom_field.nil?
570+
571+
custom_field
569572
end
570573
end
571574

spec/core/client_spec.rb

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -394,25 +394,4 @@ def url.to_str
394394
expect(Thread.current[:zendesk_thread_local_username]).to eq(original)
395395
end
396396
end
397-
398-
context "custom fields metadata preloading" do
399-
it "doesn’t preload metadata by default" do
400-
expect(subject.account_data["custom_fields"]).to be_nil
401-
end
402-
403-
it "preloads metadata if configured" do
404-
fields = {ticket_fields: [{title: "field one", id: 12}]}.to_json
405-
client = ZendeskAPI::Client.new do |config|
406-
config.preload_custom_fields_metadata = true
407-
config.url = "https://example.zendesk.com/api/v2"
408-
config.adapter = :test
409-
config.adapter_proc = proc do |stub|
410-
stub.get "/api/v2/ticket_fields" do |env|
411-
[200, {"content-type": "application/json", Authorization: env.request_headers["Authorization"]}, fields]
412-
end
413-
end
414-
end
415-
expect(client.account_data["custom_fields"]).to eq({"field one" => 12})
416-
end
417-
end
418397
end

spec/live/ticket_spec.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,10 @@ def valid_attributes
113113
describe "custom fields" do
114114
before do
115115
client.config.preload_custom_fields_metadata = true
116-
client.load_custom_fields_metadata
117-
VCR.use_cassette("ticket_create_custom_fields") do
118-
@ticket = ZendeskAPI::Ticket.create(client, {
119-
subject: "live spec subject for custom fields test",
120-
description: "live spec description for custom fields test"
121-
})
122-
end
116+
@ticket = ZendeskAPI::Ticket.create(client, {
117+
subject: "live spec subject for custom fields test",
118+
description: "live spec description for custom fields test"
119+
})
123120
end
124121

125122
after do
@@ -134,6 +131,15 @@ def valid_attributes
134131
@ticket.reload!
135132
expect(@ticket.custom_field["Custom field name"]).to eq("Custom field value")
136133
end
134+
135+
it "raises ArgumentError for non-existent fields" do
136+
expect { @ticket.custom_field["This field does not exist"] }.to raise_error(ArgumentError)
137+
end
138+
139+
it "stores custom fields metadata on a client" do
140+
_custom_field_value = @ticket.custom_field["Custom field name"]
141+
expect(@ticket.account_data[:custom_fields]["Custom field name"]).to eq(9961714922394)
142+
end
137143
end
138144

139145
describe ".incremental_export" do

0 commit comments

Comments
 (0)