class Hubspot::Resource
Public Class Methods
create(properties = {})
click to toggle source
# File lib/hubspot/resource.rb, line 24 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)
click to toggle source
# File lib/hubspot/resource.rb, line 19 def find(id) instance = new(id) instance.reload end
from_result(result)
click to toggle source
# File lib/hubspot/resource.rb, line 13 def from_result(result) resource = new(result[id_field]) resource.send(:initialize_from, result.with_indifferent_access) resource end
new(id_or_properties = nil)
click to toggle source
# File lib/hubspot/resource.rb, line 55 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
update(id, properties = {})
click to toggle source
# File lib/hubspot/resource.rb, line 32 def update(id, properties = {}) begin update!(id, properties) rescue Hubspot::RequestError => e false end end
update!(id, properties = {})
click to toggle source
# File lib/hubspot/resource.rb, line 40 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
Protected Class Methods
create_path()
click to toggle source
# File lib/hubspot/resource.rb, line 169 def self.create_path begin self::CREATE_PATH rescue NameError raise "CREATE_PATH not defined for #{self.class.name}" end end
delete_path()
click to toggle source
# File lib/hubspot/resource.rb, line 205 def self.delete_path begin self::DELETE_PATH rescue NameError raise "CREATE_PATH not defined for #{self.class.name}" end end
find_path()
click to toggle source
# File lib/hubspot/resource.rb, line 181 def self.find_path begin self::FIND_PATH rescue NameError raise "FIND_PATH not defined for #{self.class.name}" end end
update_path()
click to toggle source
# File lib/hubspot/resource.rb, line 193 def self.update_path begin self::UPDATE_PATH rescue NameError raise "UPDATE_PATH not defined for #{self.class.name}" end end
Public Instance Methods
[](name)
click to toggle source
# File lib/hubspot/resource.rb, line 100 def [](name) @changes[name] || @properties.dig(name, 'value') end
changed?()
click to toggle source
# File lib/hubspot/resource.rb, line 96 def changed? !@changes.empty? end
changes()
click to toggle source
# File lib/hubspot/resource.rb, line 92 def changes @changes end
delete()
click to toggle source
# File lib/hubspot/resource.rb, line 153 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?()
click to toggle source
# File lib/hubspot/resource.rb, line 163 def deleted? @deleted end
id()
click to toggle source
# File lib/hubspot/resource.rb, line 76 def id @id end
id=(id)
click to toggle source
# File lib/hubspot/resource.rb, line 80 def id=(id) @id = id end
metadata()
click to toggle source
# File lib/hubspot/resource.rb, line 88 def metadata @metadata end
persisted?()
click to toggle source
# File lib/hubspot/resource.rb, line 113 def persisted? @persisted end
reload()
click to toggle source
# File lib/hubspot/resource.rb, line 104 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()
click to toggle source
# File lib/hubspot/resource.rb, line 117 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) # Grab the new ID from the response @id = response[id_field] # Update the fields with the response initialize_from(response.with_indifferent_access) end @persisted = true true end
to_i()
click to toggle source
# File lib/hubspot/resource.rb, line 84 def to_i @id end
update(properties)
click to toggle source
# File lib/hubspot/resource.rb, line 144 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
Protected Instance Methods
add_accessors(keys)
click to toggle source
# File lib/hubspot/resource.rb, line 237 def add_accessors(keys) singleton_class.instance_eval do keys.each do |k| # Define a getter define_method(k) { @changes[k.to_sym] || @properties.dig(k, 'value') } # Define a setter define_method("#{k}=") do |v| @changes[k.to_sym] = v end end end end
create_path()
click to toggle source
# File lib/hubspot/resource.rb, line 177 def create_path self.class.create_path end
delete_path()
click to toggle source
# File lib/hubspot/resource.rb, line 213 def delete_path self.class.delete_path end
find_path()
click to toggle source
# File lib/hubspot/resource.rb, line 189 def find_path self.class.find_path end
initialize_from(response)
click to toggle source
# File lib/hubspot/resource.rb, line 217 def initialize_from(response) @properties = response["properties"] || HashWithIndifferentAccess.new @metadata = response.except "properties" add_accessors(@properties.keys) # Clear any changes @changes = HashWithIndifferentAccess.new end
method_missing(method_name, *arguments, &block)
click to toggle source
Calls superclass method
# File lib/hubspot/resource.rb, line 251 def method_missing(method_name, *arguments, &block) # When assigning a missing attribute define the accessors and set the value if method_name.to_s.end_with?("=") attr = method_name.to_s[0...-1].to_sym add_accessors([attr]) # Call the new setter return send(method_name, arguments[0]) elsif @properties.key?(method_name) return @properties[method_name]['value'] else super end end
respond_to_missing?(method_name, include_private = false)
click to toggle source
Calls superclass method
# File lib/hubspot/resource.rb, line 266 def respond_to_missing?(method_name, include_private = false) (@properties && @properties.key?(method_name)) || super end
update_from_changes()
click to toggle source
# File lib/hubspot/resource.rb, line 227 def update_from_changes @changes.each do |k, v| @properties[k] ||= {} @properties[k]["value"] = v end # Clear any changes @changes = HashWithIndifferentAccess.new end
update_path()
click to toggle source
# File lib/hubspot/resource.rb, line 201 def update_path self.class.update_path end