Class: ContextIO::Message
- Defined in:
- lib/context-io/message.rb
Instance Attribute Summary collapse
-
#account_id ⇒ Object
Returns the value of attribute account_id.
-
#cc ⇒ Object
Returns the value of attribute cc.
-
#date ⇒ Object
Returns the value of attribute date.
-
#files ⇒ Object
Returns the value of attribute files.
-
#folders ⇒ Object
Returns the value of attribute folders.
-
#from ⇒ Object
Returns the value of attribute from.
-
#message_id ⇒ Object
Returns the value of attribute message_id.
-
#raw_data ⇒ Object
readonly
Returns the value of attribute raw_data.
-
#sources ⇒ Object
Returns the value of attribute sources.
-
#subject ⇒ Object
Returns the value of attribute subject.
-
#to ⇒ Object
Returns the value of attribute to.
Class Method Summary collapse
-
.all(account, query = {}) ⇒ Object
Public: Get all messages for given account.
- .find(account, message_id) ⇒ Object
-
.from_json(account_id, json_msg) ⇒ Object
Internal: Create an Message instance from the JSON returned by the Context.IO server.
Instance Method Summary collapse
- #answered! ⇒ Object
-
#body(format = :plain) ⇒ Object
Public: Returns message body.
- #copy(folder_name, destination_source = nil) ⇒ Object
- #delete! ⇒ Object
- #draft! ⇒ Object
- #flagged! ⇒ Object
-
#flags ⇒ Object
Public: Returns message flags.
-
#headers ⇒ Object
Public: Returns message headers.
-
#initialize(account_id, raw_data) ⇒ Message
constructor
Internal: Returns ContextIO::Message object.
- #move(folder_name, destination_source = nil) ⇒ Object
- #read! ⇒ Object
-
#thread ⇒ Object
Public: Returns array of messages of the thread a given message is in.
- #unanswered! ⇒ Object
- #undelete! ⇒ Object
- #undraft! ⇒ Object
- #unflagged! ⇒ Object
- #unread! ⇒ Object
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(account_id, raw_data) @account_id = account_id @raw_data = raw_data @body = {} end |
Instance Attribute Details
#account_id ⇒ Object
Returns the value of attribute account_id.
5 6 7 |
# File 'lib/context-io/message.rb', line 5 def account_id @account_id end |
#cc ⇒ Object
Returns the value of attribute cc.
5 6 7 |
# File 'lib/context-io/message.rb', line 5 def cc @cc end |
#date ⇒ Object
Returns the value of attribute date.
5 6 7 |
# File 'lib/context-io/message.rb', line 5 def date @date end |
#files ⇒ Object
Returns the value of attribute files.
5 6 7 |
# File 'lib/context-io/message.rb', line 5 def files @files end |
#folders ⇒ Object
Returns the value of attribute folders.
5 6 7 |
# File 'lib/context-io/message.rb', line 5 def folders @folders end |
#from ⇒ Object
Returns the value of attribute from.
5 6 7 |
# File 'lib/context-io/message.rb', line 5 def from @from end |
#message_id ⇒ Object
Returns the value of attribute message_id.
5 6 7 |
# File 'lib/context-io/message.rb', line 5 def @message_id end |
#raw_data ⇒ Object (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 |
#sources ⇒ Object
Returns the value of attribute sources.
5 6 7 |
# File 'lib/context-io/message.rb', line 5 def sources @sources end |
#subject ⇒ Object
Returns the value of attribute subject.
5 6 7 |
# File 'lib/context-io/message.rb', line 5 def subject @subject end |
#to ⇒ Object
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(account, query = {}) return [] if account.nil? account_id = account.is_a?(Account) ? account.id : account.to_s get("/2.0/accounts/#{account_id}/messages", query).map do |msg| Message.from_json(account_id, 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(account, ) return nil if account.nil? or .nil? account_id = account.is_a?(Account) ? account.id : account.to_s Message.from_json(account_id, get("/2.0/accounts/#{account_id}/messages/#{}")) 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(account_id, json_msg) = new(account_id, json_msg) . = json_msg["message_id"] .subject = json_msg["subject"] .date = Time.at json_msg["date"] .sources = json_msg["sources"] .from = json_msg["addresses"]["from"] .to = json_msg["addresses"]["to"] .cc = json_msg["addresses"]["cc"] .folders = json_msg["folders"] .files = json_msg["files"] 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 |
#flags ⇒ Object
Public: Returns message flags.
82 83 84 |
# File 'lib/context-io/message.rb', line 82 def flags get("#{url}/flags") end |
#headers ⇒ Object
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 |
#thread ⇒ Object
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(account_id, 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 |