Total Complexity | 15 |
Total Lines | 47 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | require 'atom' |
||
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 |
||
55 |