Class: Heathrow::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/heathrow/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Message

Returns a new instance of Message.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/heathrow/message.rb', line 20

def initialize(attrs = {})
  # Set defaults
  @timestamp = Time.now.to_i
  @received_at = Time.now.to_i
  @read = false
  @starred = false
  @archived = false
  @recipients = []
  @cc = []
  @bcc = []
  @labels = []
  @attachments = []
  @metadata = {}

  # Set attributes from hash
  attrs.each do |key, value|
    # Handle both string and symbol keys
    key = key.to_s if key.is_a?(Symbol)

    # Parse JSON strings for array/hash fields
    if ['recipients', 'cc', 'bcc', 'labels', 'attachments', 'metadata'].include?(key)
      value = parse_json_field(value)
    end

    # Handle backward compatibility
    key = 'read' if key == 'is_read'
    key = 'starred' if key == 'is_starred'

    send("#{key}=", value) if respond_to?("#{key}=")
  end
end

Instance Attribute Details

#archivedObject

Returns the value of attribute archived.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def archived
  @archived
end

#attachmentsObject

Returns the value of attribute attachments.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def attachments
  @attachments
end

#bccObject

Returns the value of attribute bcc.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def bcc
  @bcc
end

#ccObject

Returns the value of attribute cc.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def cc
  @cc
end

#contentObject

Returns the value of attribute content.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def content
  @content
end

#external_idObject

Returns the value of attribute external_id.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def external_id
  @external_id
end

#html_contentObject

Returns the value of attribute html_content.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def html_content
  @html_content
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def id
  @id
end

#labelsObject

Returns the value of attribute labels.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def labels
  @labels
end

#metadataObject

Returns the value of attribute metadata.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def 
  @metadata
end

#parent_idObject

Returns the value of attribute parent_id.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def parent_id
  @parent_id
end

#readObject Also known as: is_read

Returns the value of attribute read.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def read
  @read
end

#received_atObject

Returns the value of attribute received_at.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def received_at
  @received_at
end

#recipientsObject

Returns the value of attribute recipients.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def recipients
  @recipients
end

#senderObject

Returns the value of attribute sender.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def sender
  @sender
end

#sender_nameObject

Returns the value of attribute sender_name.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def sender_name
  @sender_name
end

#source_idObject

Returns the value of attribute source_id.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def source_id
  @source_id
end

#starredObject Also known as: is_starred

Returns the value of attribute starred.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def starred
  @starred
end

#subjectObject

Returns the value of attribute subject.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def subject
  @subject
end

#thread_idObject

Returns the value of attribute thread_id.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def thread_id
  @thread_id
end

#timestampObject

Returns the value of attribute timestamp.



6
7
8
# File 'lib/heathrow/message.rb', line 6

def timestamp
  @timestamp
end

Instance Method Details

#add_label(label) ⇒ Object



99
100
101
# File 'lib/heathrow/message.rb', line 99

def add_label(label)
  @labels << label unless @labels.include?(label)
end

#age_in_daysObject



159
160
161
# File 'lib/heathrow/message.rb', line 159

def age_in_days
  ((Time.now.to_i - @timestamp) / 86400.0).round(1)
end

#archive!Object



91
92
93
# File 'lib/heathrow/message.rb', line 91

def archive!
  @archived = true
end

#archived?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/heathrow/message.rb', line 124

def archived?
  @archived
end

#display_senderObject



151
152
153
# File 'lib/heathrow/message.rb', line 151

def display_sender
  @sender_name || @sender || 'Unknown'
end

#has_attachments?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/heathrow/message.rb', line 128

def has_attachments?
  @attachments && !@attachments.empty?
end

#has_label?(label) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/heathrow/message.rb', line 107

def has_label?(label)
  @labels.include?(label)
end

#has_thread?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/heathrow/message.rb', line 132

def has_thread?
  !@thread_id.nil?
end

#is_reply?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/heathrow/message.rb', line 136

def is_reply?
  !@parent_id.nil?
end

#mark_as_read!Object

Message state methods



79
80
81
# File 'lib/heathrow/message.rb', line 79

def mark_as_read!
  @read = true
end

#mark_as_unread!Object



83
84
85
# File 'lib/heathrow/message.rb', line 83

def mark_as_unread!
  @read = false
end

#read?Boolean

Query methods

Returns:

  • (Boolean)


112
113
114
# File 'lib/heathrow/message.rb', line 112

def read?
  @read
end

#remove_label(label) ⇒ Object



103
104
105
# File 'lib/heathrow/message.rb', line 103

def remove_label(label)
  @labels.delete(label)
end

#short_content(length = 100) ⇒ Object



146
147
148
149
# File 'lib/heathrow/message.rb', line 146

def short_content(length = 100)
  return '' unless @content
  @content.length > length ? "#{@content[0...length]}..." : @content
end

#short_subject(length = 50) ⇒ Object

Display helpers



141
142
143
144
# File 'lib/heathrow/message.rb', line 141

def short_subject(length = 50)
  return '' unless @subject
  @subject.length > length ? "#{@subject[0...length]}..." : @subject
end

#starred?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/heathrow/message.rb', line 120

def starred?
  @starred
end

#timestamp_formatted(format = '%Y-%m-%d %H:%M:%S') ⇒ Object



155
156
157
# File 'lib/heathrow/message.rb', line 155

def timestamp_formatted(format = '%Y-%m-%d %H:%M:%S')
  Time.at(@timestamp).strftime(format)
end

#to_hObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/heathrow/message.rb', line 52

def to_h
  {
    id: @id,
    source_id: @source_id,
    external_id: @external_id,
    thread_id: @thread_id,
    parent_id: @parent_id,
    sender: @sender,
    sender_name: @sender_name,
    recipients: @recipients,
    cc: @cc,
    bcc: @bcc,
    subject: @subject,
    content: @content,
    html_content: @html_content,
    timestamp: @timestamp,
    received_at: @received_at,
    read: @read,
    starred: @starred,
    archived: @archived,
    labels: @labels,
    attachments: @attachments,
    metadata: @metadata
  }
end

#toggle_star!Object



87
88
89
# File 'lib/heathrow/message.rb', line 87

def toggle_star!
  @starred = !@starred
end

#unarchive!Object



95
96
97
# File 'lib/heathrow/message.rb', line 95

def unarchive!
  @archived = false
end

#unread?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/heathrow/message.rb', line 116

def unread?
  !@read
end