Class: Zm::Client::Base::BaseJsnsInitializer

Inherits:
Object
  • Object
show all
Defined in:
lib/zm/client/base/base_jsns_initializer.rb

Overview

class for account object jsns initializer

Class Method Summary collapse

Class Method Details

.convert_json_string_value(value) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/zm/client/base/base_jsns_initializer.rb', line 43

def convert_json_string_value(value)
  return value unless value.is_a?(String)
  return 0 if value == '0'

  c = value.to_i
  c.to_s == value ? c : value
end

.formatted_json(json) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/zm/client/base/base_jsns_initializer.rb', line 20

def formatted_json(json)
  return [] if json[:a].nil?

  json[:a].reject! { |n| n[:n].nil? }
  json_map = json[:a].map { |n| [n[:n], n[:_content]] }.freeze

  hash_multivalued = json_map.each_with_object({}) do |(k, v), h|
    h[k] ||= []
    h[k].push(v)
  end

  hash_multivalued.transform_values do |v|
    v.length == 1 ? v.first : v
  end
end

.update(item, json) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/zm/client/base/base_jsns_initializer.rb', line 9

def update(item, json)
  item.id = json[:id]
  item.name = json[:name]

  formatted_json(json).each do |k, v|
    valorise(item, k, v)
  end

  item
end

.valorise(item, k, v) ⇒ Object



36
37
38
39
40
41
# File 'lib/zm/client/base/base_jsns_initializer.rb', line 36

def valorise(item, k, v)
  setter_method_name = "#{k}="
  return unless item.respond_to?(setter_method_name)

  item.send(setter_method_name, convert_json_string_value(v))
end