Class: FreshdeskAPI::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/freshdesk_api/resource.rb

Overview

Represents a resource that only holds data.

Direct Known Subclasses

Resource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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



28
29
30
31
32
# File 'lib/freshdesk_api/resource.rb', line 28

def initialize(client, attributes = {})
  raise "Expected a Hash for attributes, got #{attributes.inspect}" unless attributes.is_a?(Hash)
  @client = client
  @attributes = attributes
end

Instance Attribute Details

#attributesHash (readonly)

Returns The resource’s attributes.

Returns:

  • (Hash)

    The resource’s attributes



7
8
9
# File 'lib/freshdesk_api/resource.rb', line 7

def attributes
  @attributes
end

#errorsArray

Returns The last received errors.

Returns:

  • (Array)

    The last received errors



10
11
12
# File 'lib/freshdesk_api/resource.rb', line 10

def errors
  @errors
end

Class Method Details

.resource_nameObject

The resource name taken from the class name (e.g. FreshdeskAPI::SolutionCatogory -> solution_categories)



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

def resource_name
  @resource_name ||= singular_resource_name.pluralize
end

.singular_resource_nameObject

The singular resource name taken from the class name (e.g. FreshdeskAPI::SoulutionCategory -> solution_category)



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

def singular_resource_name
  @singular_respurce_name ||= to_s.split('::').last.underscore

end

Instance Method Details

#==(other) ⇒ Object

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



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/freshdesk_api/resource.rb', line 56

def ==(other)
  return true if other.object_id == self.object_id

  if other && !other.is_a?(Data)
    warn "Trying to compare #{other.class} to a Resource from #{caller.first}"
  end

  if other.is_a?(Data)
    other.id && other.id == id
  else
    false
  end
end

#idObject

Returns the resource id of the object or nil



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

def id
  attributes.key?(:id) ? attributes[:id] : nil
end

#new_record?Boolean

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

Returns:

  • (Boolean)


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

def new_record?
  id.nil?
end

#to_sObject Also known as: inspect



45
46
47
# File 'lib/freshdesk_api/resource.rb', line 45

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