diff --git a/lib/zendesk_api/resource.rb b/lib/zendesk_api/resource.rb index 6a1bbe57..b0d35282 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 :==