Class: Nylas::Message

Inherits:
RestfulModel show all
Includes:
ReadUnreadMethods
Defined in:
lib/message.rb

Direct Known Subclasses

Draft, ExpandedMessage

Instance Attribute Summary

Attributes inherited from RestfulModel

#raw_json

Instance Method Summary collapse

Methods included from ReadUnreadMethods

#mark_as_read!, #mark_as_unread!, #star!, #unstar!, #update_param!

Methods inherited from RestfulModel

#==, collection_name, #destroy, #initialize, #save!, #update, #url

Methods included from TimeAttrAccessor

#time_attr_accessor

Methods included from Parameters

included, #parameters

Constructor Details

This class inherits a constructor from Nylas::RestfulModel

Instance Method Details

#as_json(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/message.rb', line 48

def as_json(options = {})
  hash = {}

  # unread, starred and labels/folder are the only attribute
  # you can modify.
  if not @unread.nil?
    hash["unread"] = @unread
  end

  if not @starred.nil?
    hash["starred"] = @starred
  end

  if not @labels.nil? and @labels != []
    hash["label_ids"] = @labels.map do |label|
      label.id
    end
  end

  if not @folder.nil?
    hash["folder_id"] = @folder.id
  end

  hash
end

#expandedObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/message.rb', line 90

def expanded
  expanded_url = url(action='?view=expanded')

  RestClient.get(expanded_url){ |response,request,result|
    json = Nylas.interpret_response(result, response, :expected_class => Object)
    expanded_message = Nylas::ExpandedMessage.new(@_api)
    expanded_message.inflate(json)
    expanded_message
  }

end

#filesObject



74
75
76
# File 'lib/message.rb', line 74

def files
  @files ||= RestfulModelCollection.new(File, @_api, {:message_id=>@id})
end

#files?Boolean

Returns:

  • (Boolean)


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

def files?
  !@raw_json['files'].empty?
end

#inflate(json) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/message.rb', line 25

def inflate(json)
  super
  @to ||= []
  @cc ||= []
  @bcc ||= []
  @labels ||= []
  @folder ||= nil

  # This is a special case --- we receive label data from the API
  # as JSON but we want it to behave like an API object.
  @labels.map! do |label_json|
   label = Label.new(@_api)
   label.inflate(label_json)
   label
  end

  if not folder.nil? and folder.is_a?(Hash)
   folder = Folder.new(@_api)
   folder.inflate(@folder)
   @folder = folder
  end
end

#rawObject



82
83
84
85
86
87
88
# File 'lib/message.rb', line 82

def raw
  collection = RestfulModelCollection.new(Message, @_api, {:message_id=>@id})
  RestClient.get("#{collection.url}/#{id}/", :accept => 'message/rfc822'){ |response,request,result|
    Nylas.interpret_response(result, response, {:raw_response => true})
    response
  }
end