Class: ZendeskAPI::Data

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

Overview

Represents an abstract resource that only holds data.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associations

included, #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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/zendesk_api/resource.rb', line 64

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

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)


89
90
91
92
# File 'lib/zendesk_api/resource.rb', line 89

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:



55
56
57
# File 'lib/zendesk_api/resource.rb', line 55

def association
  @association
end

#attributesHash (readonly) Also known as: to_param

Returns The resource’s attributes.

Returns:

  • (Hash)

    The resource’s attributes



53
54
55
# File 'lib/zendesk_api/resource.rb', line 53

def attributes
  @attributes
end

#errorsArray

Returns The last received errors.

Returns:

  • (Array)

    The last received errors



57
58
59
# File 'lib/zendesk_api/resource.rb', line 57

def errors
  @errors
end

#responseObject

Place to dump the last response



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

def response
  @response
end

Class Method Details

.inherited(klass) ⇒ Object



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

def inherited(klass)
  subclasses.push(klass)
end

.namespace(namespace) ⇒ Object



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

def namespace(namespace)
  @namespace = namespace
end

.new_from_response(client, response, includes = nil) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/zendesk_api/resource.rb', line 43

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

.resource_nameObject Also known as: model_key

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



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

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

.resource_pathObject



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

def resource_path
  [@namespace, resource_name].compact.join("/")
end

.singular_resource_nameObject

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



24
25
26
# File 'lib/zendesk_api/resource.rb', line 24

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

.subclassesObject



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

def subclasses
  @subclasses ||= []
end

Instance Method Details

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

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



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/zendesk_api/resource.rb', line 133

def ==(other)
  return false unless other

  return true if other.object_id == object_id

  return other.id && (other.id == id) if other.is_a?(Data)

  return id == other if other.is_a?(Integer)

  warn "Trying to compare #{other.class} to a Resource
    from #{caller.first}"
end

#attributes_for_saveObject



154
155
156
# File 'lib/zendesk_api/resource.rb', line 154

def attributes_for_save
  { self.class.singular_resource_name.to_sym => attribute_changes }
end

#idObject

Returns the resource id of the object or nil



99
100
101
# File 'lib/zendesk_api/resource.rb', line 99

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

#loaded_associationsObject



109
110
111
112
113
114
# File 'lib/zendesk_api/resource.rb', line 109

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)


104
105
106
# File 'lib/zendesk_api/resource.rb', line 104

def new_record?
  id.nil?
end

#path(options = {}) ⇒ Object

Returns the path to the resource



117
118
119
# File 'lib/zendesk_api/resource.rb', line 117

def path(options = {})
  @association.generate_path(self, options)
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(method, include_private = false)
  @attributes.respond_to?(method) || super
end

#to_json(*args) ⇒ Object

Passes #to_json to the underlying attributes hash



122
123
124
# File 'lib/zendesk_api/resource.rb', line 122

def to_json(*args)
  method_missing(:to_json, *args)
end

#to_sObject Also known as: inspect



127
128
129
# File 'lib/zendesk_api/resource.rb', line 127

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