From 24877a271c1b342314b7312767138f1ff2381090 Mon Sep 17 00:00:00 2001 From: ur5us Date: Mon, 18 May 2026 14:13:12 +1200 Subject: [PATCH 1/4] Refactor to DRY property access MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …instead of littering the same logic in multiple places. The bigger intention behind this change is going to implemented in the next commit specifically addressing `Contact`` property which is broken for the `Contact.all` due to different API response shapes. --- lib/hubspot/resource.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/hubspot/resource.rb b/lib/hubspot/resource.rb index 75480d36..a8bda184 100644 --- a/lib/hubspot/resource.rb +++ b/lib/hubspot/resource.rb @@ -230,7 +230,7 @@ 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_method(k) { self[k] } # Define a setter define_method("#{k}=") do |v| @@ -247,9 +247,9 @@ def method_missing(method_name, *arguments, &block) add_accessors([attr]) # Call the new setter - return send(method_name, arguments[0]) + send(method_name, arguments[0]) elsif @properties.key?(method_name) - return @properties[method_name]['value'] + self[method_name] else super end From 3f0e479629712d4980b4f4dc341e0fd5f3ea920f Mon Sep 17 00:00:00 2001 From: ur5us Date: Mon, 18 May 2026 14:55:20 +1200 Subject: [PATCH 2/4] Fix Contact property accessors Contact APIs v1 and v3+ return different shapes of JSON. For the latter, both dynamic (Hash) and meta-programmed property accessors fail with `'Hash#dig': String does not have #dig method (TypeError)` due to the aforementioned data shape differences. Thus, overwrite the hash access operator to account for both shapes. v3+ simply returns a key/value shape whereas v1 returns a hash for each key which the current value as well as the history. --- lib/hubspot/contact.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/hubspot/contact.rb b/lib/hubspot/contact.rb index 31b1611f..9789681f 100644 --- a/lib/hubspot/contact.rb +++ b/lib/hubspot/contact.rb @@ -106,6 +106,16 @@ def batch_update(contacts, opts = {}) end end + def [](name) + if changes.key? name + @changes[name] + else + name_property = @properties[name] + + name_property.is_a?(Hash) ? name_property["value"] : name_property + end + end + def name [firstname, lastname].compact.join(' ') end From b5c233aa5e3d667a34705b62d8c987f18622a576 Mon Sep 17 00:00:00 2001 From: ur5us Date: Tue, 19 May 2026 11:07:46 +1200 Subject: [PATCH 3/4] Refactor to use guard clause Address PR feedback; this is cleaner. --- lib/hubspot/contact.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/hubspot/contact.rb b/lib/hubspot/contact.rb index 9789681f..e198345e 100644 --- a/lib/hubspot/contact.rb +++ b/lib/hubspot/contact.rb @@ -107,13 +107,11 @@ def batch_update(contacts, opts = {}) end def [](name) - if changes.key? name - @changes[name] - else - name_property = @properties[name] + return @changes[name] if changes.key? name - name_property.is_a?(Hash) ? name_property["value"] : name_property - end + name_property = @properties[name] + + name_property.is_a?(Hash) ? name_property['value'] : name_property end def name From c2d642b39187673c70d48f555a461e20b3b14f58 Mon Sep 17 00:00:00 2001 From: ur5us Date: Tue, 19 May 2026 11:11:09 +1200 Subject: [PATCH 4/4] Remove unnecessary symbol conversion `@changes` is a `Hash` with indifferent access. --- lib/hubspot/resource.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/hubspot/resource.rb b/lib/hubspot/resource.rb index a8bda184..d762a33f 100644 --- a/lib/hubspot/resource.rb +++ b/lib/hubspot/resource.rb @@ -234,7 +234,7 @@ def add_accessors(keys) # Define a setter define_method("#{k}=") do |v| - @changes[k.to_sym] = v + @changes[k] = v end end end