lib/hubspot/resource.rb in ruby_hubspot_api-0.1.2.1 vs lib/hubspot/resource.rb in ruby_hubspot_api-0.2
- old
+ new
@@ -1,8 +1,10 @@
# frozen_string_literal: true
require_relative './api_client'
+require_relative './paged_collection'
+require_relative './paged_batch'
module Hubspot
# rubocop:disable Metrics/ClassLength
# Hubspot::Resource class
class Resource < ApiClient
@@ -51,10 +53,25 @@
params: params,
resource_class: self
)
end
+ def batch_read(object_ids = [], id_property: 'id')
+ params = id_property == 'id' ? {} : { idProperty: id_property }
+
+ PagedBatch.new(
+ url: "/crm/v3/objects/#{resource_name}/batch/read",
+ params: params,
+ object_ids: object_ids,
+ resource_class: self
+ )
+ end
+
+ def batch_read_all(object_ids = [], id_property: 'id')
+ Hubspot::Batch.read(self, object_ids, id_property: id_property)
+ end
+
# Get the complete list of fields (properties) for the object
def properties
@properties ||= begin
response = get("/crm/v3/properties/#{resource_name}")
handle_response(response)['results'].map { |hash| Property.new(hash) }
@@ -109,22 +126,22 @@
)
end
# rubocop:enable Metrics/MethodLength
- private
-
# Define the resource name based on the class
def resource_name
name = self.name.split('::').last.downcase
if name.end_with?('y')
name.gsub(/y$/, 'ies') # Company -> companies
else
"#{name}s" # Contact -> contacts, Deal -> deals
end
end
+ private
+
# Instantiate a single resource object from the response
def instantiate_from_response(response)
data = handle_response(response)
new(data) # Passing full response data to initialize
end
@@ -159,22 +176,26 @@
end
end
# rubocop:disable Ling/MissingSuper
def initialize(data = {})
+ data.transform_keys!(&:to_s)
@id = extract_id(data)
@properties = {}
@metadata = {}
-
if @id
initialize_from_api(data)
else
initialize_new_object(data)
end
end
# rubocop:enable Ling/MissingSuper
+ def changes?
+ !@changes.empty?
+ end
+
# Instance methods for update (or save)
def save
if persisted?
self.class.update(@id, @changes).tap do |result|
return false unless result
@@ -205,10 +226,14 @@
def delete
self.class.archive(id)
end
alias archive delete
+ def resource_name
+ self.class.resource_name
+ end
+
# rubocop:disable Metrics/MethodLength
# Handle dynamic getter and setter methods with method_missing
def method_missing(method, *args)
method_name = method.to_s
@@ -230,22 +255,18 @@
return @changes[method_name] if @changes.key?(method_name)
return @properties[method_name] if @properties.key?(method_name)
end
# Fallback if the method or attribute is not found
- # :nocov:
super
- # :nocov:
end
# rubocop:enable Metrics/MethodLength
# Ensure respond_to_missing? is properly overridden
- # :nocov:
def respond_to_missing?(method_name, include_private = false)
property_name = method_name.to_s.chomp('=')
@properties.key?(property_name) || @changes.key?(property_name) || super
end
- # :nocov:
private
# Extract ID from data and convert to integer
def extract_id(data)