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



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

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)


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

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:



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

def association
  @association
end

#attributesHash (readonly) Also known as: to_param

Returns The resource’s attributes.

Returns:

  • (Hash)

    The resource’s attributes



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

def attributes
  @attributes
end

Class Method Details

.only_send_unnested_paramsObject



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

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)



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

def resource_name
  @resource_name ||= Inflection.plural(singular_resource_name)
end

.singular_resource_nameObject

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



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

def singular_resource_name
  @singular_resource_name ||= ZendeskAPI::Helpers.snakecase_string(to_s.split("::").last)
end

.unnested_paramsObject



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

def unnested_params
  @unnested_params ||= false
end

Instance Method Details

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



92
93
94
95
# File 'lib/zendesk_api/resource.rb', line 92

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



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

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

#loaded_associationsObject



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

def loaded_associations
  self.class.associations.select do |association|
    loaded = @attributes.method_missing(association[:name])
    loaded && !(loaded.respond_to?(:empty?) && loaded.empty?)
  end
end

#new_record?Boolean

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

Returns:

  • (Boolean)


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

def new_record?
  id.nil?
end

#path(*args) ⇒ Object

Returns the path to the resource



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

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

#to_sObject Also known as: inspect



87
88
89
# File 'lib/zendesk_api/resource.rb', line 87

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