Class: Zm::Client::ContactJsnsInitializer

Inherits:
Object
  • Object
show all
Defined in:
lib/zm/client/contact/contact_jsns_initializer.rb

Overview

class for account contact

Class Method Summary collapse

Class Method Details

.create(parent, json) ⇒ Object



8
9
10
11
# File 'lib/zm/client/contact/contact_jsns_initializer.rb', line 8

def create(parent, json)
  item = Contact.new(parent)
  update(item, json)
end

.init_by_attrs(attrs, item) ⇒ Object



41
42
43
44
45
46
# File 'lib/zm/client/contact/contact_jsns_initializer.rb', line 41

def init_by_attrs(attrs, item)
  attrs.each do |k, v|
    m = Utils.equals_name(k)
    item.send(m, v) if item.respond_to?(m)
  end
end

.init_members_from_json(m, item) ⇒ Object



48
49
50
51
52
# File 'lib/zm/client/contact/contact_jsns_initializer.rb', line 48

def init_members_from_json(m, item)
  return if m.nil?

  item.members.all = m.map { |m| Zm::Client::ConcatMember.new(m[:type], m[:value]) }
end

.make_custom_keys(attrs, item) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/zm/client/contact/contact_jsns_initializer.rb', line 32

def make_custom_keys(attrs, item)
  custom_keys = attrs.keys - item.all_instance_variable_keys
  return if custom_keys.empty?

  item.class.attr_accessor(*custom_keys)
rescue StandardError => _e
  nil
end

.update(item, json) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/zm/client/contact/contact_jsns_initializer.rb', line 13

def update(item, json)
  item.id = json.delete(:id)
  item.l = json.delete(:l)
  item.name = json.delete(:fileAsStr)
  item.tn   = json.delete(:tn)

  if json[:_attrs].is_a?(Hash)
    make_custom_keys(json[:_attrs], item)
    init_by_attrs(json[:_attrs], item)
  end

  if item.group?
    item.extend(GroupContact)
    init_members_from_json(json[:m], item)
  end

  item
end