Class: ContextIO::Message

Inherits:
Resource show all
Defined in:
lib/context-io/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Request

#delete, #get, #post, #put, #request

Constructor Details

#initialize(account_id, raw_data) ⇒ Message

Internal: Returns ContextIO::Message object

raw_data - The parse JSON returned by the Context.IO server.



55
56
57
58
59
# File 'lib/context-io/message.rb', line 55

def initialize(, raw_data)
  @account_id = 
  @raw_data = raw_data
  @body = {}
end

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



5
6
7
# File 'lib/context-io/message.rb', line 5

def 
  @account_id
end

#ccObject

Returns the value of attribute cc.



5
6
7
# File 'lib/context-io/message.rb', line 5

def cc
  @cc
end

#dateObject

Returns the value of attribute date.



5
6
7
# File 'lib/context-io/message.rb', line 5

def date
  @date
end

#filesObject

Returns the value of attribute files.



5
6
7
# File 'lib/context-io/message.rb', line 5

def files
  @files
end

#foldersObject

Returns the value of attribute folders.



5
6
7
# File 'lib/context-io/message.rb', line 5

def folders
  @folders
end

#fromObject

Returns the value of attribute from.



5
6
7
# File 'lib/context-io/message.rb', line 5

def from
  @from
end

#message_idObject

Returns the value of attribute message_id.



5
6
7
# File 'lib/context-io/message.rb', line 5

def message_id
  @message_id
end

#raw_dataObject (readonly)

Returns the value of attribute raw_data.



6
7
8
# File 'lib/context-io/message.rb', line 6

def raw_data
  @raw_data
end

#sourcesObject

Returns the value of attribute sources.



5
6
7
# File 'lib/context-io/message.rb', line 5

def sources
  @sources
end

#subjectObject

Returns the value of attribute subject.



5
6
7
# File 'lib/context-io/message.rb', line 5

def subject
  @subject
end

#toObject

Returns the value of attribute to.



5
6
7
# File 'lib/context-io/message.rb', line 5

def to
  @to
end

Class Method Details

.all(account, query = {}) ⇒ Object

Public: Get all messages for given account.

query - An optional Hash (default: {}) containing a query to filter the

responses. For possible values see Context.IO API documentation.

Returns an Array of Message objects.

account - Account object or ID



15
16
17
18
19
20
21
22
# File 'lib/context-io/message.rb', line 15

def self.all(, query = {})
  return [] if .nil?

   = .is_a?(Account) ? .id : .to_s
  get("/2.0/accounts/#{}/messages", query).map do |msg|
    Message.from_json(, msg)
  end
end

.find(account, message_id) ⇒ Object



24
25
26
27
28
29
# File 'lib/context-io/message.rb', line 24

def self.find(, message_id)
  return nil if .nil? or message_id.nil?
   = .is_a?(Account) ? .id : .to_s

  Message.from_json(, get("/2.0/accounts/#{}/messages/#{message_id}"))
end

.from_json(account_id, json_msg) ⇒ Object

Internal: Create an Message instance from the JSON returned by the Context.IO server.

json - The parsed JSON returned by the Context.IO server. See their

documentation for what keys are possible.

Returns a ContextIO::Message instance.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/context-io/message.rb', line 38

def self.from_json(, json_msg)
  message = new(, json_msg)
  message.message_id = json_msg["message_id"]
  message.subject = json_msg["subject"]
  message.date = Time.at json_msg["date"]
  message.sources = json_msg["sources"]
  message.from = json_msg["addresses"]["from"]
  message.to = json_msg["addresses"]["to"]
  message.cc = json_msg["addresses"]["cc"]
  message.folders = json_msg["folders"]
  message.files = json_msg["files"]
  message
end

Instance Method Details

#answered!Object



102
103
104
# File 'lib/context-io/message.rb', line 102

def answered!
  flag("answered" => true)
end

#body(format = :plain) ⇒ Object

Public: Returns message body. Data is lazy loaded. Message body fetched from Context.IO server contain plain text and html format and both formats are stored.

format - String determining required format of message body. Allowed values are :plain and :html. Default value is :plain.



67
68
69
70
71
72
73
74
# File 'lib/context-io/message.rb', line 67

def body(format = :plain)
  if @body.empty?
    get("#{url}/body").each do |b|
      @body[b["type"]] = b["content"]
    end
  end
  @body["text/#{format}"]
end

#copy(folder_name, destination_source = nil) ⇒ Object



133
134
135
# File 'lib/context-io/message.rb', line 133

def copy(folder_name, destination_source = nil)
  copy_move(folder_name, false, destination_source)
end

#delete!Object



118
119
120
# File 'lib/context-io/message.rb', line 118

def delete!
  flag("deleted" => true)
end

#draft!Object



110
111
112
# File 'lib/context-io/message.rb', line 110

def draft!
  flag("draft" => true)
end

#flagged!Object



94
95
96
# File 'lib/context-io/message.rb', line 94

def flagged!
  flag("flagged" => true)
end

#flagsObject

Public: Returns message flags.



82
83
84
# File 'lib/context-io/message.rb', line 82

def flags
  get("#{url}/flags")
end

#headersObject

Public: Returns message headers. Data is lazy loaded.



77
78
79
# File 'lib/context-io/message.rb', line 77

def headers
  @headers ||= get("#{url}/headers")
end

#move(folder_name, destination_source = nil) ⇒ Object



137
138
139
# File 'lib/context-io/message.rb', line 137

def move(folder_name, destination_source = nil)
  copy_move(folder_name, true, destination_source)
end

#read!Object



86
87
88
# File 'lib/context-io/message.rb', line 86

def read!
  flag("seen" => true)
end

#threadObject

Public: Returns array of messages of the thread a given message is in.



127
128
129
130
131
# File 'lib/context-io/message.rb', line 127

def thread
  get("#{url}/thread")["messages"].map do |m|
    Message.from_json(, m)
  end
end

#unanswered!Object



106
107
108
# File 'lib/context-io/message.rb', line 106

def unanswered!
  flag("answered" => false)
end

#undelete!Object



122
123
124
# File 'lib/context-io/message.rb', line 122

def undelete!
  flag("deleted" => false)
end

#undraft!Object



114
115
116
# File 'lib/context-io/message.rb', line 114

def undraft!
  flag("draft" => false)
end

#unflagged!Object



98
99
100
# File 'lib/context-io/message.rb', line 98

def unflagged!
  flag("flagged" => false)
end

#unread!Object



90
91
92
# File 'lib/context-io/message.rb', line 90

def unread!
  flag("seen" => false)
end