Class: ZendeskAPI::Data

Inherits:
Object
  • Object
show all
Includes:
Associations, Rescue
Defined in:
lib/zendesk_api/resource.rb

Overview

Represents a resource that only holds data.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associations

#wrap_resource

Constructor Details

#initialize(client, attributes = {}) ⇒ Data

Create a new resource instance.

Parameters:

  • client (Client)

    The client to use

  • attributes (Hash) (defaults to: {})

    The optional attributes that describe the resource



47
48
49
50
51
52
53
54
55
# File 'lib/zendesk_api/resource.rb', line 47

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)
  @client = client
  @attributes = ZendeskAPI::Trackie.new(attributes)
  ZendeskAPI::Client.check_deprecated_namespace_usage @attributes, self.class.singular_resource_name

  @attributes.clear_changes unless new_record?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object

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

Raises:

  • (NoMethodError)


59
60
61
62
# File 'lib/zendesk_api/resource.rb', line 59

def method_missing(*args, &block)
  raise NoMethodError, ":save is not defined" if args.first.to_sym == :save
  @attributes.send(*args, &block)
end

Instance Attribute Details

#associationZendeskAPI::Association

Returns The association.

Returns:



42
43
44
# File 'lib/zendesk_api/resource.rb', line 42

def association
  @association
end

#attributesHash (readonly) Also known as: to_param

Returns The resource’s attributes.

Returns:

  • (Hash)

    The resource’s attributes



40
41
42
# File 'lib/zendesk_api/resource.rb', line 40

def attributes
  @attributes
end

Class Method Details

.only_send_unnested_paramsObject



30
31
32
# File 'lib/zendesk_api/resource.rb', line 30

def only_send_unnested_params
  @unnested_params = true
end

.resource_nameObject Also known as: model_key

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



19
20
21
# File 'lib/zendesk_api/resource.rb', line 19

def resource_name
  @resource_name ||= singular_resource_name.plural
end

.singular_resource_nameObject

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



14
15
16
# File 'lib/zendesk_api/resource.rb', line 14

def singular_resource_name
  @singular_resource_name ||= to_s.split("::").last.snakecase
end

.unnested_paramsObject



34
35
36
# File 'lib/zendesk_api/resource.rb', line 34

def unnested_params
  @unnested_params ||= false
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql



84
85
86
87
# File 'lib/zendesk_api/resource.rb', line 84

def ==(other)
  warn "Trying to compare #{other.class} to a Resource" if other && !other.is_a?(Data)
  other.is_a?(self.class) && ((other.id && other.id == id) || (other.object_id == self.object_id))
end

#idObject Also known as: hash

Returns the resource id of the object or nil



65
66
67
# File 'lib/zendesk_api/resource.rb', line 65

def id
  key?(:id) ? method_missing(:id) : nil
end

#new_record?Boolean

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

Returns:

  • (Boolean)


70
71
72
# File 'lib/zendesk_api/resource.rb', line 70

def new_record?
  id.nil? 
end

#path(*args) ⇒ Object

Returns the path to the resource



75
76
77
# File 'lib/zendesk_api/resource.rb', line 75

def path(*args)
  @association.generate_path(self, *args)
end

#to_sObject Also known as: inspect



79
80
81
# File 'lib/zendesk_api/resource.rb', line 79

def to_s
  "#{self.class.singular_resource_name}: #{attributes.inspect}"
end