Class: Hubspot::Resource
- Inherits:
-
Object
show all
- Defined in:
- lib/hubspot/resource.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(id_or_properties = nil) ⇒ Resource
Returns a new instance of Resource.
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/hubspot/resource.rb', line 56
def initialize(id_or_properties = nil)
@changes = HashWithIndifferentAccess.new
@properties = HashWithIndifferentAccess.new
case id_or_properties
when Integer, NilClass
@id = id_or_properties
when String
@id = Integer id_or_properties
when Hash
@id = id_or_properties.delete(id_field) || id_or_properties.delete(:id)
add_accessors(id_or_properties.keys)
id_or_properties.each do |k, v|
send "#{k}=", v
end
else
raise InvalidParams.new("#{self.class.name} must be initialized with an ID, hash, or nil")
end
@persisted = @id.present?
@deleted = false
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
# File 'lib/hubspot/resource.rb', line 249
def method_missing(method_name, *arguments, &block)
if method_name.to_s.end_with?("=")
attr = method_name.to_s[0...-1].to_sym
add_accessors([attr])
send(method_name, arguments[0])
elsif @properties.key?(method_name)
self[method_name]
else
super
end
end
|
Instance Attribute Details
#changes ⇒ Object
Returns the value of attribute changes.
86
87
88
|
# File 'lib/hubspot/resource.rb', line 86
def changes
@changes
end
|
#id ⇒ Object
Returns the value of attribute id.
80
81
82
|
# File 'lib/hubspot/resource.rb', line 80
def id
@id
end
|
Returns the value of attribute metadata.
87
88
89
|
# File 'lib/hubspot/resource.rb', line 87
def metadata
@metadata
end
|
#properties ⇒ Object
Returns the value of attribute properties.
88
89
90
|
# File 'lib/hubspot/resource.rb', line 88
def properties
@properties
end
|
Class Method Details
.create(properties = {}) ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/hubspot/resource.rb', line 25
def create(properties = {})
request = {
properties: Hubspot::Utils.hash_to_properties(properties.stringify_keys, key_name: property_name_field)
}
response = Hubspot::Connection.post_json(create_path, params: {}, body: request)
from_result(response)
end
|
.create_path ⇒ Object
167
168
169
170
171
172
173
|
# File 'lib/hubspot/resource.rb', line 167
def self.create_path
begin
self::CREATE_PATH
rescue NameError
raise "CREATE_PATH not defined for #{self.class.name}"
end
end
|
.delete_path ⇒ Object
203
204
205
206
207
208
209
|
# File 'lib/hubspot/resource.rb', line 203
def self.delete_path
begin
self::DELETE_PATH
rescue NameError
raise "CREATE_PATH not defined for #{self.class.name}"
end
end
|
.find(id) ⇒ Object
20
21
22
23
|
# File 'lib/hubspot/resource.rb', line 20
def find(id)
instance = new(id)
instance.reload
end
|
.find_path ⇒ Object
179
180
181
182
183
184
185
|
# File 'lib/hubspot/resource.rb', line 179
def self.find_path
begin
self::FIND_PATH
rescue NameError
raise "FIND_PATH not defined for #{self.class.name}"
end
end
|
.from_result(result, id_field: self.id_field) ⇒ Object
14
15
16
17
18
|
# File 'lib/hubspot/resource.rb', line 14
def from_result(result, id_field: self.id_field)
resource = new(result[id_field])
resource.send(:initialize_from, result.with_indifferent_access)
resource
end
|
.update(id, properties = {}) ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/hubspot/resource.rb', line 33
def update(id, properties = {})
begin
update!(id, properties)
rescue Hubspot::RequestError => e
false
end
end
|
.update!(id, properties = {}) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/hubspot/resource.rb', line 41
def update!(id, properties = {})
request = {
properties: Hubspot::Utils.hash_to_properties(properties.stringify_keys, key_name: property_name_field)
}
if update_method == "put"
response = Hubspot::Connection.put_json(update_path, params: { id: id, no_parse: true }, body: request)
else
response = Hubspot::Connection.post_json(update_path, params: { id: id, no_parse: true }, body: request)
end
response.success?
end
|
.update_path ⇒ Object
191
192
193
194
195
196
197
|
# File 'lib/hubspot/resource.rb', line 191
def self.update_path
begin
self::UPDATE_PATH
rescue NameError
raise "UPDATE_PATH not defined for #{self.class.name}"
end
end
|
Instance Method Details
#[](name) ⇒ Object
94
95
96
|
# File 'lib/hubspot/resource.rb', line 94
def [](name)
@changes[name] || @properties.dig(name, 'value')
end
|
#[]=(name, value) ⇒ Object
98
99
100
|
# File 'lib/hubspot/resource.rb', line 98
def []=(name, value)
@changes[name] = value unless @changes[name] == value
end
|
#changed? ⇒ Boolean
90
91
92
|
# File 'lib/hubspot/resource.rb', line 90
def changed?
!@changes.empty?
end
|
#delete ⇒ Object
151
152
153
154
155
156
157
158
159
|
# File 'lib/hubspot/resource.rb', line 151
def delete
raise(Hubspot::InvalidParams.new("Resource must have an ID")) if @id.nil?
Hubspot::Connection.delete_json(delete_path, id: @id)
@deleted = true
@changes = HashWithIndifferentAccess.new
true
end
|
#deleted? ⇒ Boolean
161
162
163
|
# File 'lib/hubspot/resource.rb', line 161
def deleted?
@deleted
end
|
#persisted? ⇒ Boolean
111
112
113
|
# File 'lib/hubspot/resource.rb', line 111
def persisted?
@persisted
end
|
#reload ⇒ Object
102
103
104
105
106
107
108
109
|
# File 'lib/hubspot/resource.rb', line 102
def reload
raise(Hubspot::InvalidParams.new("Resource must have an ID")) if @id.nil?
response = Hubspot::Connection.get_json(find_path, id: @id)
initialize_from(response.with_indifferent_access)
self
end
|
#save ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/hubspot/resource.rb', line 115
def save
request = {
properties: Hubspot::Utils.hash_to_properties(@changes.stringify_keys, key_name: property_name_field)
}
if persisted?
if update_method == "put"
response = Hubspot::Connection.put_json(update_path, params: { id: @id }, body: request)
else
response = Hubspot::Connection.post_json(update_path, params: { id: @id }, body: request)
end
update_from_changes
else
response = Hubspot::Connection.post_json(create_path, params: {}, body: request)
@id = response[id_field]
initialize_from(response.with_indifferent_access)
end
@persisted = true
true
end
|
#to_i ⇒ Object
82
83
84
|
# File 'lib/hubspot/resource.rb', line 82
def to_i
@id
end
|
#update(properties) ⇒ Object
142
143
144
145
146
147
148
149
|
# File 'lib/hubspot/resource.rb', line 142
def update(properties)
if properties && !properties.is_a?(Hash)
raise ArgumentError, "When assigning properties, you must pass a hash as an argument."
end
@changes = @changes.merge(properties)
save
end
|