From 987eae9d9b45d0e0839b8ebab63f626da35bd87a Mon Sep 17 00:00:00 2001 From: Rogers Date: Wed, 17 May 2023 11:00:32 -0500 Subject: [PATCH 1/4] Small refactor and a bit clean up --- lib/zendesk_api/resource.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/zendesk_api/resource.rb b/lib/zendesk_api/resource.rb index 6a1bbe57..4f695699 100644 --- a/lib/zendesk_api/resource.rb +++ b/lib/zendesk_api/resource.rb @@ -39,6 +39,14 @@ def resource_path def namespace(namespace) @namespace = namespace end + + def new_from_response(client, response, includes = nil) + new(client).tap do |resource| + resource.handle_response(response) + resource.set_includes(resource, includes, response.body) if includes + resource.attributes.clear_changes + end + end end # @return [Hash] The resource's attributes @@ -123,17 +131,17 @@ def to_s # Compares resources by class and id. If id is nil, then by object_id def ==(other) - return true if other.object_id == object_id + return false unless other - if other && !(other.is_a?(Data) || other.is_a?(Integer)) - warn "Trying to compare #{other.class} to a Resource from #{caller.first}" - end + return true if other.object_id == object_id if other.is_a?(Data) other.id && other.id == id elsif other.is_a?(Integer) id == other else + warn "Trying to compare #{other.class} to a Resource from #{caller.first}" + false end end From 2f05515c9275437c0582a8b5d45b41ac4cbeed08 Mon Sep 17 00:00:00 2001 From: Rogers Date: Thu, 18 May 2023 10:32:14 -0500 Subject: [PATCH 2/4] Clean up a bit more --- lib/zendesk_api/resource.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/zendesk_api/resource.rb b/lib/zendesk_api/resource.rb index 4f695699..889922f7 100644 --- a/lib/zendesk_api/resource.rb +++ b/lib/zendesk_api/resource.rb @@ -135,15 +135,14 @@ def ==(other) return true if other.object_id == object_id - if other.is_a?(Data) - other.id && other.id == id - elsif other.is_a?(Integer) - id == other - else - warn "Trying to compare #{other.class} to a Resource from #{caller.first}" - - false + return other.id && (other.id == id) if other.is_a?(Data) + + unless other.is_a?(Integer) + return warn "Trying to compare #{other.class} to a Resource + from #{caller.first}" end + + id == other end alias :eql :== From e4dce9719cfaf4645114ebec8de7c424a2c5c9ec Mon Sep 17 00:00:00 2001 From: Rogers Date: Thu, 8 Jun 2023 20:52:37 -0500 Subject: [PATCH 3/4] Replace unless with a guard clause --- lib/zendesk_api/resource.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/zendesk_api/resource.rb b/lib/zendesk_api/resource.rb index 889922f7..b0d35282 100644 --- a/lib/zendesk_api/resource.rb +++ b/lib/zendesk_api/resource.rb @@ -137,12 +137,10 @@ def ==(other) return other.id && (other.id == id) if other.is_a?(Data) - unless other.is_a?(Integer) - return warn "Trying to compare #{other.class} to a Resource - from #{caller.first}" - end + return id == other if other.is_a?(Integer) - id == other + warn "Trying to compare #{other.class} to a Resource + from #{caller.first}" end alias :eql :== From 55b69e5ef3d29981874a2f83a9fe16ffac3c10a9 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Mon, 21 Aug 2023 11:24:58 +1000 Subject: [PATCH 4/4] Client.new_from_response testing --- spec/core/data_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 spec/core/data_spec.rb diff --git a/spec/core/data_spec.rb b/spec/core/data_spec.rb new file mode 100644 index 00000000..a6e0cff4 --- /dev/null +++ b/spec/core/data_spec.rb @@ -0,0 +1,12 @@ +require 'core/spec_helper' + +RSpec.describe ZendeskAPI::Data do + describe ".new_from_response" do + let(:response) { double(:response) } + + it "returns an instance with the response" do + expect(described_class.new_from_response(client, response)) + .to be_instance_of(described_class) + end + end +end