Class: Close::APIResource
- Inherits:
-
CloseObject
- Object
- CloseObject
- Close::APIResource
- Extended by:
- APIOperations
- Defined in:
- lib/close/api_resource.rb
Direct Known Subclasses
Activity, Contact, CustomActivity, CustomActivityType, Lead, Opportunity, Organization, Task, User
Class Method Summary collapse
-
.available_subclasses ⇒ Array
Available subclasses of API resource that can be instanced.
-
.class_name ⇒ Object
Easy to grok resource name.
-
.create(values = {}) ⇒ Close::APIResource
Creates a resource with the given values.
-
.list(opts = {}) ⇒ Array
Retrieves a list of resources.
-
.resource_url ⇒ String
Define a resource’s URL.
-
.retrieve(id) ⇒ Close::APIResource
Retrieve a singular resource.
Instance Method Summary collapse
-
#destroy ⇒ Boolean
Destroy the resource on the server.
-
#dirty? ⇒ Boolean
If there are unsaved/unpersisted changes to the object.
-
#initialize(values = {}, existing_values = {}) ⇒ Close::APIResource
constructor
Initialize a new resource with the given values.
-
#inspect ⇒ String
Override the default inspect method to show the object’s values.
-
#save ⇒ Boolean
Persists the values to the server.
-
#set(key, value) ⇒ String
Escape hatch for new values.
-
#update ⇒ Boolean
Updates the resource on the server.
-
#values ⇒ Hash
Easily access the mixed existing and new values.
Methods included from APIOperations
Constructor Details
#initialize(values = {}, existing_values = {}) ⇒ Close::APIResource
Initialize a new resource with the given values.
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_subclasses ⇒ Array
Available subclasses of API resource that can be instanced. Listed from their files in ./resource
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_name ⇒ Object
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.
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.
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_url ⇒ String
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.
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.
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
#destroy ⇒ Boolean
Destroy the resource on the server.
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.
109 110 111 |
# File 'lib/close/api_resource.rb', line 109 def dirty? !@new_values.empty? end |
#inspect ⇒ String
Override the default inspect method to show 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 |
#save ⇒ Boolean
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.
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.
72 73 74 |
# File 'lib/close/api_resource.rb', line 72 def set(key, value) @new_values[key] = value end |
#update ⇒ Boolean
Updates the resource on the server. Will not make a request if there are no unsaved changes.
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 |
#values ⇒ Hash
Easily access the mixed existing and new values.
63 64 65 |
# File 'lib/close/api_resource.rb', line 63 def values @values.merge(@new_values) end |