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.
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/hubspot/resource.rb', line 54
def initialize(id_or_properties = nil)
@changes = HashWithIndifferentAccess.new
@properties = HashWithIndifferentAccess.new
if id_or_properties.is_a?(Integer) || id_or_properties.nil?
@id = id_or_properties
elsif id_or_properties.is_a?(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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
# File 'lib/hubspot/resource.rb', line 240
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])
return send(method_name, arguments[0])
elsif @properties.key?(method_name)
return @properties[method_name]['value']
else
super
end
end
|
Instance Attribute Details
#changes ⇒ Object
Returns the value of attribute changes.
81
82
83
|
# File 'lib/hubspot/resource.rb', line 81
def changes
@changes
end
|
#id ⇒ Object
Returns the value of attribute id.
75
76
77
|
# File 'lib/hubspot/resource.rb', line 75
def id
@id
end
|
Returns the value of attribute metadata.
82
83
84
|
# File 'lib/hubspot/resource.rb', line 82
def metadata
@metadata
end
|
#properties ⇒ Object
Returns the value of attribute properties.
83
84
85
|
# File 'lib/hubspot/resource.rb', line 83
def properties
@properties
end
|
Class Method Details
.create(properties = {}) ⇒ Object
23
24
25
26
27
28
29
|
# File 'lib/hubspot/resource.rb', line 23
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
|
.find(id) ⇒ Object
18
19
20
21
|
# File 'lib/hubspot/resource.rb', line 18
def find(id)
instance = new(id)
instance.reload
end
|
.from_result(result) ⇒ Object
12
13
14
15
16
|
# File 'lib/hubspot/resource.rb', line 12
def from_result(result)
resource = new(result[id_field])
resource.send(:initialize_from, result.with_indifferent_access)
resource
end
|
.update(id, properties = {}) ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/hubspot/resource.rb', line 31
def update(id, properties = {})
begin
update!(id, properties)
rescue Hubspot::RequestError => e
false
end
end
|
.update!(id, properties = {}) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/hubspot/resource.rb', line 39
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
|
Instance Method Details
#[](name) ⇒ Object
89
90
91
|
# File 'lib/hubspot/resource.rb', line 89
def [](name)
@changes[name] || @properties.dig(name, 'value')
end
|
#changed? ⇒ Boolean
85
86
87
|
# File 'lib/hubspot/resource.rb', line 85
def changed?
!@changes.empty?
end
|
#delete ⇒ Object
142
143
144
145
146
147
148
149
150
|
# File 'lib/hubspot/resource.rb', line 142
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
152
153
154
|
# File 'lib/hubspot/resource.rb', line 152
def deleted?
@deleted
end
|
#persisted? ⇒ Boolean
102
103
104
|
# File 'lib/hubspot/resource.rb', line 102
def persisted?
@persisted
end
|
#reload ⇒ Object
93
94
95
96
97
98
99
100
|
# File 'lib/hubspot/resource.rb', line 93
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/hubspot/resource.rb', line 106
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
77
78
79
|
# File 'lib/hubspot/resource.rb', line 77
def to_i
@id
end
|
#update(properties) ⇒ Object
133
134
135
136
137
138
139
140
|
# File 'lib/hubspot/resource.rb', line 133
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
|