Total Complexity | 12 |
Total Lines | 52 |
Duplicated Lines | 0 % |
1 | require 'jsondoc' |
||
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 |
||
60 |