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.



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

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.



22
23
24
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 22

def 
  @account_id
end

#extension_idObject (readonly)

Returns the value of attribute extension_id.



23
24
25
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 23

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.



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

def mime
  @mime
end

#mime_part_paramsObject

Returns the value of attribute mime_part_params.



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

def mime_part_params
  @mime_part_params
end

Instance Method Details

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



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 71

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



83
84
85
86
87
88
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 83

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

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



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 45

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



57
58
59
60
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 57

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

#add_part(part) ⇒ Object



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

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

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



62
63
64
65
66
67
68
69
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 62

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



108
109
110
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 108

def body
  @mime.body.to_s
end

#content_typeObject



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

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

#path_params(opts = {}) ⇒ Object



40
41
42
43
# File 'lib/ringcentral_sdk/rest/request/multipart.rb', line 40

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



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

def set_url(url)
  @_url = url
  self
end

#urlObject



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

def url
  @_url
end