Module: Intercom::Traits::ApiResource

Includes:
DirtyTracking
Included in:
Admin, Company, Contact, Conversation, Count, Event, Job, Message, Note, Segment, Subscription, Intercom::Tag, 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


48
49
50
51
# File 'lib/intercom/traits/api_resource.rb', line 48

def method_missing(method_sym, *arguments, &block)
  Lib::DynamicAccessorsOnMethodMissing.new(method_sym, *arguments, self).
    define_accessors_or_call { super }
end

Instance Attribute Details

#idObject

Returns the value of attribute id


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

def id
  @id
end

Instance Method Details

#flat_store_attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)

53
54
55
# File 'lib/intercom/traits/api_resource.rb', line 53

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


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

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


19
20
21
22
23
# File 'lib/intercom/traits/api_resource.rb', line 19

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

#initialize(attributes = {}) ⇒ Object


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

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

#to_hashObject


33
34
35
36
37
38
# File 'lib/intercom/traits/api_resource.rb', line 33

def to_hash
  instance_variables_excluding_dirty_tracking_field.inject({}) do |hash, variable|
    hash[variable.to_s.delete("@")] = instance_variable_get(variable)
    hash
  end
end

#to_submittable_hashObject


40
41
42
43
44
45
46
# File 'lib/intercom/traits/api_resource.rb', line 40

def to_submittable_hash
  submittable_hash = {}
  to_hash.each do |attribute, value|
    submittable_hash[attribute] = value if submittable_attribute?(attribute, value)
  end
  submittable_hash
end