lib/hubspot/resource.rb in ruby_hubspot_api-0.3.0 vs lib/hubspot/resource.rb in ruby_hubspot_api-0.3.1
- old
+ new
@@ -261,10 +261,12 @@
# - `_neq`: Not equal to comparison.
# - `_in`: Matches any of the values in the given array.
#
# If no suffix is provided, the default comparison is equality (`EQ`).
#
+ # If no value is provided, or is empty the NOT_HAS_PROPERTY operator will be used
+ #
# query - [String, Hash] The query for searching. This can be either:
# - A String: for full-text search.
# - A Hash: where each key represents a property and may have suffixes for the comparison
# (e.g., `{ email_contains: 'example.org', age_gt: 30 }`).
# properties - An optional array of property names to return in the search results.
@@ -354,21 +356,23 @@
# Convert simple filters to HubSpot's filterGroups format
def build_filter_groups(filters)
filter_groups = [{ filters: [] }]
filters.each do |key, value|
- filter = extract_property_and_operator(key)
+ filter = extract_property_and_operator(key, value)
value_key = value.is_a?(Array) ? :values : :value
- filter[value_key] = value
+ filter[value_key] = value unless value.blank?
filter_groups.first[:filters] << filter
end
filter_groups
end
# Extract property name and operator from the key
- def extract_property_and_operator(key)
+ def extract_property_and_operator(key, value)
+ return { propertyName: key.to_s, operator: 'NOT_HAS_PROPERTY' } if value.blank?
+
OPERATOR_MAP.each do |suffix, hubspot_operator|
if key.to_s.end_with?(suffix)
return {
propertyName: key.to_s.sub(suffix, ''),
operator: hubspot_operator
@@ -452,10 +456,16 @@
else
create_new
end
end
+ def save!
+ raise NothingToDoError, 'Nothing to save' unless changes?
+
+ save
+ end
+
# If the resource exists in Hubspot
#
# Returns Boolean
def persisted?
@id ? true : false
@@ -620,9 +630,11 @@
# Create a new resource
def create_new
created_resource = self.class.create(@changes)
@id = created_resource.id
+ @properties.merge!(@changes)
+ @changes = {}
@id ? true : false
end
end
# rubocop:enable Metrics/ClassLength
end