Class: Close::APIResource

Inherits:
CloseObject show all
Extended by:
APIOperations
Defined in:
lib/close/api_resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from APIOperations

request

Constructor Details

#initialize(values = {}, existing_values = {}) ⇒ Close::APIResource

Initialize a new resource with the given values.

Parameters:

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

    the values to initialize the resource with.

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

    the values that already exist on the server.



55
56
57
58
59
# File 'lib/close/api_resource.rb', line 55

def initialize(values = {}, existing_values = {})
  @values = {}
  @new_values = values
  define_accessors(existing_values)
end

Class Method Details

.available_subclassesArray

Available subclasses of API resource that can be instanced. Listed from their files in ./resource

Returns:

  • (Array)

    the available subclasses.



13
14
15
16
17
# File 'lib/close/api_resource.rb', line 13

def self.available_subclasses
  Dir.glob(File.join(File.dirname(__FILE__), 'resource', '*.rb')).map do |file|
    File.basename(file, '.rb').split('_').map(&:capitalize).join
  end
end

.class_nameObject

Easy to grok resource name.



6
7
8
# File 'lib/close/api_resource.rb', line 6

def self.class_name
  name.split('::')[-1]
end

.create(values = {}) ⇒ Close::APIResource

Creates a resource with the given values.

Parameters:

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

    the values to create the resource with.

Returns:



47
48
49
# File 'lib/close/api_resource.rb', line 47

def self.create(values = {})
  new({}, request(:post, resource_url, values))
end

.list(opts = {}) ⇒ Array

Retrieves a list of resources.

Parameters:

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

    options to pass to the request.

Returns:

  • (Array)

    an array of resources.



32
33
34
35
# File 'lib/close/api_resource.rb', line 32

def self.list(opts = {})
  items = request(:get, resource_url, opts)
  items['data'].map { |item| new({}, item) }
end

.resource_urlString

Define a resource’s URL. We could probably do some magic here to infer the URL from the class name, but it can always be overridden.

Returns:

  • (String)

    the resource’s URL.

Raises:

  • (NotImplementedError)

    if the method is not overridden.



23
24
25
26
27
# File 'lib/close/api_resource.rb', line 23

def self.resource_url
  if self == APIResource
    raise NotImplementedError.new('APIResource is an abstract class.  You should perform actions on its subclasses (Lead, Opportunity, etc.)')
  end
end

.retrieve(id) ⇒ Close::APIResource

Retrieve a singular resource.

Parameters:

  • id (String)

    the resource’s ID.

Returns:



40
41
42
# File 'lib/close/api_resource.rb', line 40

def self.retrieve(id)
  new({}, request(:get, "#{resource_url}#{id}/"))
end

Instance Method Details

#destroyBoolean

Destroy the resource on the server.

Returns:

  • (Boolean)

    true if the destroy succeeded, false otherwise.



101
102
103
104
105
# File 'lib/close/api_resource.rb', line 101

def destroy
  return false if self.id.empty?
  self.class.request(:delete, "#{self.class.resource_url}#{self.id}/")
  true
end

#dirty?Boolean

If there are unsaved/unpersisted changes to the object.

Returns:

  • (Boolean)

    true if there are unsaved changes, false otherwise.



109
110
111
# File 'lib/close/api_resource.rb', line 109

def dirty?
  !@new_values.empty?
end

#inspectString

Override the default inspect method to show the object’s values.

Returns:

  • (String)

    the object’s values.



115
116
117
118
119
# File 'lib/close/api_resource.rb', line 115

def inspect
  str = "#<#{self.class.name}:#{self.object_id} "
  fields = @values.keys.map{|field| "#{field}: #{self.send(field)}"}
  str << fields.join(", ") << ">"
end

#saveBoolean

Persists the values to the server. If the resource already exists, it will update the resource. If the resource does not exist, it will create the resource.

Returns:

  • (Boolean)

    true if the save succeeded, false otherwise.



91
92
93
94
95
96
97
# File 'lib/close/api_resource.rb', line 91

def save
  if defined?(id) && !id.empty?
    update()
  else
    @values = self.class.request(:post, self.class.resource_url, values)
  end
end

#set(key, value) ⇒ String

Escape hatch for new values. Instead of using the defined accessors, you can use this method to set a value.

Parameters:

  • key (String)

    the key to set.

  • value (String)

    the value to set.

Returns:

  • (String)

    the value.



72
73
74
# File 'lib/close/api_resource.rb', line 72

def set(key, value)
  @new_values[key] = value
end

#updateBoolean

Updates the resource on the server. Will not make a request if there are no unsaved changes.

Returns:

  • (Boolean)

    true if the update succeeded, false otherwise.



79
80
81
82
83
84
85
# File 'lib/close/api_resource.rb', line 79

def update
  return true unless dirty?
  resp = self.class.request(:put, "#{self.class.resource_url}#{self.id}/", values)
  @new_values = {}
  @values = resp
  true
end

#valuesHash

Easily access the mixed existing and new values.

Returns:

  • (Hash)

    the values.



63
64
65
# File 'lib/close/api_resource.rb', line 63

def values
  @values.merge(@new_values)
end