diff --git a/lib/zendesk_api/resource.rb b/lib/zendesk_api/resource.rb index c796da2b..7f64c749 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,19 +131,16 @@ def to_s # Compares resources by class and id. If id is nil, then by object_id def ==(other) + return false unless other + return true if other.object_id == object_id - if other && !(other.is_a?(Data) || other.is_a?(Integer)) - warn "Trying to compare #{other.class} to a Resource from #{caller.first}" - end + return other.id && (other.id == id) if other.is_a?(Data) - if other.is_a?(Data) - other.id && other.id == id - elsif other.is_a?(Integer) - id == other - else - false - end + return id == other if other.is_a?(Integer) + + warn "Trying to compare #{other.class} to a Resource + from #{caller.first}" end alias :eql :== 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