module Intercom::Traits::ApiResource
Attributes
client[RW]
id[RW]
Public Class Methods
new(attributes = {})
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 16 def initialize(attributes = {}) from_hash(attributes) end
Private Class Methods
included(base)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 156 def self.included(base) base.extend(ClassMethods) end
Public Instance Methods
==(other)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 20 def ==(other) self.class == other.class && to_json == other.to_json end
flat_store_attribute?(attribute)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 68 def flat_store_attribute?(attribute) respond_to?(:flat_store_attributes) && flat_store_attributes.map(&:to_s).include?(attribute.to_s) end
from_hash(hash)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 30 def from_hash(hash) hash.each do |attribute, value| initialize_property(attribute, value) end initialize_missing_flat_store_attributes if respond_to? :flat_store_attributes self end
from_response(response)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 24 def from_response(response) from_hash(response) reset_changed_fields! self end
method_missing(method_sym, *arguments, &block)
click to toggle source
Calls superclass method
# File lib/intercom/traits/api_resource.rb, line 63 def method_missing(method_sym, *arguments, &block) Lib::DynamicAccessorsOnMethodMissing.new(method_sym, *arguments, self) .define_accessors_or_call { super } end
to_hash()
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 38 def to_hash instance_variables_excluding_dirty_tracking_field.each_with_object({}) do |variable, hash| hash[variable.to_s.delete('@')] = instance_variable_get(variable) end end
to_json(*args)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 44 def to_json(*args) instance_variables_excluding_dirty_tracking_field.each_with_object({}) do |variable, hash| next if variable == :@client value = instance_variable_get(variable) hash[variable.to_s.delete('@')] = value.respond_to?(:to_json) ? value.to_json(*args) : value end end
to_submittable_hash()
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 53 def to_submittable_hash submittable_hash = {} to_hash.each do |attribute, value| next unless submittable_attribute?(attribute, value) submittable_hash[attribute] = value.respond_to?(:to_submittable_hash) ? value.to_submittable_hash : value end submittable_hash end
Private Instance Methods
accessors_already_defined?(attribute)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 87 def accessors_already_defined?(attribute) respond_to?(attribute) && respond_to?("#{attribute}=") end
addressable_list?(attribute, value)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 81 def addressable_list?(attribute, value) return false unless typed_property?(attribute, value) value['type'] == 'list' && value['url'] end
call_setter_for_attribute(attribute, value)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 130 def call_setter_for_attribute(attribute, value) setter_method = "#{attribute}=" send(setter_method, value) end
custom_attribute_field?(attribute)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 106 def custom_attribute_field?(attribute) attribute.to_s == 'custom_attributes' end
initialize_missing_flat_store_attributes()
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 135 def initialize_missing_flat_store_attributes flat_store_attributes.each do |attribute| unless instance_variables_excluding_dirty_tracking_field.map(&:to_s).include? "@#{attribute}" initialize_property(attribute, {}) end end end
initialize_property(attribute, value)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 74 def initialize_property(attribute, value) return if addressable_list?(attribute, value) Lib::DynamicAccessors.define_accessors(attribute, value, self) unless accessors_already_defined?(attribute) set_property(attribute, value) end
message_from_field?(attribute, value)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 110 def message_from_field?(attribute, value) attribute.to_s == 'from' && value.is_a?(Hash) && value['type'] end
message_to_field?(attribute, value)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 114 def message_to_field?(attribute, value) attribute.to_s == 'to' && value.is_a?(Hash) && value['type'] end
parsed_value_for_attribute(attribute, value)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 96 def parsed_value_for_attribute(attribute, value) if typed_property?(attribute, value) Intercom::Lib::TypedJsonDeserializer.new(value, client).deserialize elsif flat_store_attribute?(attribute) Intercom::Lib::FlatStore.new(value) else value end end
set_property(attribute, value)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 91 def set_property(attribute, value) value_to_set = parsed_value_for_attribute(attribute, value) call_setter_for_attribute(attribute, value_to_set) end
submittable_attribute?(attribute, value)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 143 def submittable_attribute?(attribute, value) # FlatStores always submitted, even if not changed, as we don't track their dirtyness value.is_a?(Intercom::Lib::FlatStore) || field_changed?(attribute) end
typed_property?(attribute, value)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 118 def typed_property?(attribute, value) typed_value?(value) && !custom_attribute_field?(attribute) && !message_from_field?(attribute, value) && !message_to_field?(attribute, value) && attribute.to_s != 'metadata' end
typed_value?(value)
click to toggle source
# File lib/intercom/traits/api_resource.rb, line 126 def typed_value?(value) value.is_a?(Hash) && !!value['type'] end