Completed
Push — master ( 2dec5e...ae318f )
by John
01:39 queued 47s
created

AtomEntry.from_phone_number()   A

Complexity

Conditions 3

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 3
1
require 'atom'
2
3
module RingCentral
4
  module RSS
5
    # Represents an Atom Entry for a feed
6
    class AtomEntry
7
      attr_accessor :entry
8
9
      def initialize(data = nil)
10
        load_message(data) unless data.nil?
11
      end
12
13
      def load_message(data)
14
        raise 'Data is not a hash' unless data.is_a? Hash
15
16
        @entry = Atom::Entry.new do |e|
17
          e.title = build_title data
18
          e.links << Atom::Link.new(href: data['uri'])
19
          e.id = data['id']
20
          e.updated = Time.parse(data['lastModifiedTime'])
21
          e.summary = data['subject']
22
        end
23
      end
24
25
      def build_title(data)
26
        raise 'Data is not a hash' unless data.is_a? Hash
27
28
        parts = []
29
30
        to = to_phone_number data
31
        parts << "To: #{to}" unless to.empty?
32
33
        from = from_phone_number data
34
        parts << "From: #{from}" unless from.nil?
35
36
        "[#{data['direction']} #{data['type']}] " + parts.join('; ')
37
      end
38
39
      def to_phone_number(data, index = 0)
40
        if data.key?('to') && !data['to'].empty? && data['to'][index]['phoneNumber']
41
          return data['to'][0]['phoneNumber'].to_s
42
        end
43
        nil
44
      end
45
46
      def from_phone_number(data)
47
        if data.key?('from') && !data['from']['phoneNumber'].empty?
48
          return data['from']['phoneNumber'].to_s
49
        end
50
        nil
51
      end
52
    end
53
  end
54
end
55