Class: Inbox::Draft

Inherits:
Message show all
Defined in:
lib/draft.rb

Instance Attribute Summary

Attributes inherited from RestfulModel

#raw_json

Instance Method Summary collapse

Methods inherited from Message

#expanded, #files, #files?, #inflate, #raw

Methods included from ReadUnreadMethods

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

Methods inherited from RestfulModel

#==, collection_name, #destroy, #inflate, #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



17
18
19
20
21
22
23
24
# File 'lib/draft.rb', line 17

def as_json(options = {})
  # FIXME @karim: this is a bit of a hack --- Draft inherits Message
  # was okay until we overrode Message#as_json to allow updating folders/labels.
  # This broke draft sending, which relies on RestfulModel::as_json to work.
  grandparent = self.class.superclass.superclass
  meth = grandparent.instance_method(:as_json)
  meth.bind(self).call
end

#attach(file) ⇒ Object



11
12
13
14
15
# File 'lib/draft.rb', line 11

def attach(file)
  file.save! unless file.id
  @file_ids ||= []
  @file_ids.push(file.id)
end

#send!Object



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
51
52
53
# File 'lib/draft.rb', line 26

def send!
  url = @_api.url_for_path("/send")
  if @id
    data = {:draft_id => @id, :version => @version}
  else
    data = as_json()
  end

  ::RestClient.post(url, data.to_json, :content_type => :json) do |response, request, result|

    # This is mostly lifted from Inbox#interpret_response. We're not using the original function
    # because we need to pass an additional error message to the Exception constructor.
    Inbox.interpret_http_status(result)
    json = JSON.parse(response)
    if json.is_a?(Hash) && (json['type'] == 'api_error' or json['type'] == 'invalid_request_error')
      exc = Inbox.http_code_to_exception(result.code.to_i)
      exc_type = json['type']
      exc_message = json['message']
      exc_server_error = json['server_error']
      raise exc.new(exc_type, exc_message, server_error=exc_server_error)
    end
    raise UnexpectedResponse.new(result.msg) if result.is_a?(Net::HTTPClientError)

    self.inflate(json)
  end

  self
end