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