Module: Intercom::Traits::ApiResource

Includes:
DirtyTracking
Included in:
Admin, Article, Collection, Company, Contact, Conversation, Count, DataAttribute, Event, Job, Lead, Message, Note, Section, Segment, Subscription, Intercom::Tag, Intercom::Team, User, Visitor
Defined in:
lib/intercom/traits/api_resource.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DirtyTracking

#field_changed?, #instance_variables_excluding_dirty_tracking_field, #mark_field_as_changed!, #mark_fields_as_changed!, #reset_changed_fields!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object


63
64
65
66
# 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

Instance Attribute Details

#clientObject

Returns the value of attribute client


14
15
16
# File 'lib/intercom/traits/api_resource.rb', line 14

def client
  @client
end

#idObject

Returns the value of attribute id


14
15
16
# File 'lib/intercom/traits/api_resource.rb', line 14

def id
  @id
end

Instance Method Details

#==(other) ⇒ Object


20
21
22
# 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) ⇒ Boolean

Returns:

  • (Boolean)

68
69
70
# 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) ⇒ Object


30
31
32
33
34
35
36
# 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) ⇒ Object


24
25
26
27
28
# File 'lib/intercom/traits/api_resource.rb', line 24

def from_response(response)
  from_hash(response)
  reset_changed_fields!
  self
end

#initialize(attributes = {}) ⇒ Object


16
17
18
# File 'lib/intercom/traits/api_resource.rb', line 16

def initialize(attributes = {})
  from_hash(attributes)
end

#to_hashObject


38
39
40
41
42
# 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) ⇒ Object


44
45
46
47
48
49
50
51
# 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_hashObject


53
54
55
56
57
58
59
60
61
# 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