Class: Inbox::Message

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

Direct Known Subclasses

Draft

Instance Method Summary collapse

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 Inbox::RestfulModel

Instance Method Details

#as_json(options = {}) ⇒ Object



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

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["labels"] = @labels.map do |label|
      label.id
    end
  end

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

  hash
end

#filesObject



71
72
73
# File 'lib/message.rb', line 71

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

#inflate(json) ⇒ Object



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

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?
   folder = folder.new(@_api)
   folder.inflate(@folder)
   @folder = folder
  end
end

#rawObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/message.rb', line 75

def raw
  model = nil
  collection = RestfulModelCollection.new(Message, @_api, @namespace_id, {:message_id=>@id})
  RestClient.get("#{collection.url}/#{id}/rfc2822"){ |response,request,result|
    json = Inbox.interpret_response(result, response, {:expected_class => Object})
    model = Rfc2822.new(@_api)
    model.inflate(json)
  }
  model
end