Completed
Push — master ( f53a53...55639c )
by John
01:02
created

Event._symbolize_keys()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 6
rs 9.4285
1
require 'jsondoc'
2
require 'multi_json'
3
4
require 'pp'
5
6
module RingCentralSdk::REST
7
  class Event
8
    attr_accessor :doc
9
    def initialize(data={}, opts={})
10
      if data.is_a? JsonDoc::Document
11
        @doc = data
12
      elsif data.is_a? Hash
13
        data = _symbolize_keys data
14
        @doc = JsonDoc::Document.new(data,false, false, false)
15
      elsif opts.key?(:force) && opts[:force]
16
        @doc = JsonDoc::Document.new({}, false, false, false)
17
      else
18
        raise 'initialize needs JsonDoc::Document or Hash argument'
19
      end
20
    end
21
22
    def _hash_has_string_keys(hash={})
23
      hash.each do |k,v|
24
        return true if k.is_a? String
25
      end
26
      return false
27
    end
28
29
    def _symbolize_keys(hash={})
30
      if _hash_has_string_keys hash
31
        return MultiJson.decode(MultiJson.encode(hash), :symbolize_keys=>true)
32
      end
33
      return hash
34
    end
35
36
    def new_sms_count()
37
      count = 0
38
      have_sms = false
39
      changes = @doc.getAttr('body.changes')
40
41
puts '---'
42
      pp changes
43
puts '==='
44
      if changes.is_a?(Array) && changes.length > 0
45
        changes.each do |change|
46
          if change.key?(:type) && change[:type].to_s.downcase == 'sms'
47
            have_sms = true
48
            if change.key?(:newCount)
49
              count += change[:newCount]
50
            end
51
          end
52
        end
53
      end
54
      return have_sms ? count : -1
55
    end
56
57
    private :_hash_has_string_keys, :_symbolize_keys
58
  end
59
end
60