Class: RingCentralSdk::REST::Request::Multipart

Inherits:
Base
  • Object
show all
Defined in:
lib/ringcentral_sdk/rest/request/multipart.rb

Overview

Multipart is a base request helper class for multipart/mixed messages

Direct Known Subclasses

Fax, SMS

Constant Summary collapse

CONTENT_ID_HEADER =
'Content-Id'.freeze
DEFAULT_METHOD =
'post'.freeze
DEFAULT_ID =
'~'.freeze
DEFAULT_BASE64_ENCODE =
true
DEFAULT_CONTENT_ID_DISABLE =
true

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#headers, #params

Constructor Details

#initialize(opts = {}) ⇒ Multipart

Returns a new instance of Multipart.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 22

def initialize(opts = {})
  @mime = MIME::Multipart::Mixed.new
  @mime.headers.delete CONTENT_ID_HEADER

  @method = opts[:method] ||= DEFAULT_METHOD
  set_url(opts[:url]) if opts.key? :url

  @mime_part_params = {
    base64_encode: opts[:base64_encode] ||= DEFAULT_BASE64_ENCODE,
    content_id_disable: opts[:content_id_disable] ||= DEFAULT_CONTENT_ID_DISABLE
  }

  path_params(opts)
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



20
21
22
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 20

def 
  @account_id
end

#extension_idObject (readonly)

Returns the value of attribute extension_id.



20
21
22
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 20

def extension_id
  @extension_id
end

#methodObject

Returns the value of attribute method.



18
19
20
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 18

def method
  @method
end

#mimeObject (readonly)

Returns the value of attribute mime.



20
21
22
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 20

def mime
  @mime
end

#mime_part_paramsObject

Returns the value of attribute mime_part_params.



18
19
20
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 18

def mime_part_params
  @mime_part_params
end

Instance Method Details

#add_file(file_path_or_part, opts = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 68

def add_file(file_path_or_part, opts = {})
  if file_path_or_part.is_a? MIME::Media
    @mime.add file_path_or_part
  else
    @mime.add MIMEBuilder::Filepath.new(
      file_path_or_part,
      @mime_part_params.merge(opts).merge({is_attachment: true})
    ).mime
  end
  self
end

#add_files(files = [], opts = {}) ⇒ Object



80
81
82
83
84
85
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 80

def add_files(files = [], opts = {})
  files.each do |f|
    add_file f, opts
  end
  self
end

#add_json(data, opts = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 42

def add_json(data, opts = {})
  if data.is_a? MIME::Media
    @mime.add data
  else
    @mime.add MIMEBuilder::JSON.new(
      data,
      @mime_part_params.merge(opts)
    ).mime
  end
  self
end

#add_metadata(data, opts = {}) ⇒ Object



54
55
56
57
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 54

def (data, opts = {})
  add_json(data, opts)
  self
end

#add_part(part) ⇒ Object



87
88
89
90
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 87

def add_part(part)
  @mime.add part
  self
end

#add_text(text = nil, opts = {}) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 59

def add_text(text = nil, opts = {})
  return if text.nil? || text.to_s.empty?
  @mime.add MIMEBuilder::Text.new(
    text,
    @mime_part_params.merge(opts)
  ).mime
  self
end

#bodyObject



105
106
107
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 105

def body
  @mime.body.to_s
end

#content_typeObject



101
102
103
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 101

def content_type
  @mime.headers.get('Content-Type').to_s
end

#path_params(opts = {}) ⇒ Object



37
38
39
40
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 37

def path_params(opts = {})
  @account_id = opts[:account_id] ||= opts[:accountId] ||= DEFAULT_ID
  @extension_id = opts[:extension_id] ||= opts[:extensionId] ||= DEFAULT_ID
end

#set_url(url) ⇒ Object



96
97
98
99
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 96

def set_url(url)
  @_url = url
  self
end

#urlObject



92
93
94
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 92

def url
  @_url
end