class ZendeskAPI::Data

Represents a resource that only holds data.

Attributes

association[RW]

@return [ZendeskAPI::Association] The association

attributes[R]

@return [Hash] The resource’s attributes

errors[RW]

@return [Array] The last received errors

response[RW]

Place to dump the last response

to_param[R]

@return [Hash] The resource’s attributes

Public Class Methods

inherited(klass) click to toggle source
# File lib/zendesk_api/resource.rb, line 14
def inherited(klass)
  subclasses.push(klass)
end
model_key()
Alias for: resource_name
namespace(namespace) click to toggle source
# File lib/zendesk_api/resource.rb, line 38
def namespace(namespace)
  @namespace = namespace
end
new(client, attributes = {}) click to toggle source

Create a new resource instance. @param [Client] client The client to use @param [Hash] attributes The optional attributes that describe the resource

# File lib/zendesk_api/resource.rb, line 55
def initialize(client, attributes = {})
  raise "Expected a Hash for attributes, got #{attributes.inspect}" unless attributes.is_a?(Hash)
  @association = attributes.delete(:association) || Association.new(:class => self.class)
  @global_params = attributes.delete(:global) || {}
  @client = client
  @attributes = ZendeskAPI::Trackie.new(attributes)

  if self.class.associations.none? { |a| a[:name] == self.class.singular_resource_name }
    ZendeskAPI::Client.check_deprecated_namespace_usage @attributes, self.class.singular_resource_name
  end

  @attributes.clear_changes unless new_record?
end
new_from_response(client, response, includes = nil) click to toggle source
# File lib/zendesk_api/resource.rb, line 69
def self.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
resource_name() click to toggle source

The resource name taken from the class name (e.g. ZendeskAPI::Ticket -> tickets)

# File lib/zendesk_api/resource.rb, line 28
def resource_name
  @resource_name ||= Inflection.plural(singular_resource_name)
end
Also aliased as: model_key
resource_path() click to toggle source
# File lib/zendesk_api/resource.rb, line 32
def resource_path
  [@namespace, resource_name].compact.join("/")
end
singular_resource_name() click to toggle source

The singular resource name taken from the class name (e.g. ZendeskAPI::Ticket -> ticket)

# File lib/zendesk_api/resource.rb, line 23
def singular_resource_name
  @singular_resource_name ||= ZendeskAPI::Helpers.snakecase_string(to_s.split("::").last)
end
subclasses() click to toggle source
# File lib/zendesk_api/resource.rb, line 18
def subclasses
  @subclasses ||= []
end

Public Instance Methods

==(other) click to toggle source

Compares resources by class and id. If id is nil, then by object_id

# File lib/zendesk_api/resource.rb, line 123
def ==(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

  if other.is_a?(Data)
    other.id && other.id == id
  elsif other.is_a?(Integer)
    id == other
  else
    false
  end
end
Also aliased as: eql
eql(other)
Alias for: ==
id() click to toggle source

Returns the resource id of the object or nil

# File lib/zendesk_api/resource.rb, line 89
def id
  key?(:id) ? method_missing(:id) : nil
end
inspect()
Alias for: to_s
loaded_associations() click to toggle source

@private

# File lib/zendesk_api/resource.rb, line 99
def loaded_associations
  self.class.associations.select do |association|
    loaded = @attributes.method_missing(association[:name])
    loaded && !(loaded.respond_to?(:empty?) && loaded.empty?)
  end
end
method_missing(*args, &block) click to toggle source

Passes the method onto the attributes hash. If the attributes are nested (e.g. { :tickets => { :id => 1 } }), passes the method onto the nested hash.

# File lib/zendesk_api/resource.rb, line 79
def method_missing(*args, &block)
  raise NoMethodError, ":save is not defined" if args.first.to_sym == :save
  @attributes.send(*args, &block)
end
new_record?() click to toggle source

Has this been object been created server-side? Does this by checking for an id.

# File lib/zendesk_api/resource.rb, line 94
def new_record?
  id.nil?
end
path(options = {}) click to toggle source

Returns the path to the resource

# File lib/zendesk_api/resource.rb, line 107
def path(options = {})
  @association.generate_path(self, options)
end
respond_to_missing?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/zendesk_api/resource.rb, line 84
def respond_to_missing?(method, include_private = false)
  @attributes.respond_to?(method) || super
end
to_json(*args) click to toggle source

Passes to_json to the underlying attributes hash

# File lib/zendesk_api/resource.rb, line 112
def to_json(*args)
  method_missing(:to_json, *args)
end
to_s() click to toggle source

@private

# File lib/zendesk_api/resource.rb, line 117
def to_s
  "#{self.class.singular_resource_name}: #{attributes.inspect}"
end
Also aliased as: inspect

Private Instance Methods

attributes_for_save() click to toggle source
# File lib/zendesk_api/resource.rb, line 149
def attributes_for_save
  { self.class.singular_resource_name.to_sym => attributes.changes }
end