Class: RingCentralSdk::Helpers::CreateFaxRequest

Inherits:
Request
  • Object
show all
Defined in:
lib/ringcentral_sdk/helpers/fax.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_params = nil, metadata = nil, options = nil) ⇒ CreateFaxRequest

Returns a new instance of CreateFaxRequest.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ringcentral_sdk/helpers/fax.rb', line 10

def initialize(path_params=nil, =nil, options=nil)

  @msg = MIME::Multipart::Mixed.new
  @msg.headers.delete('Content-Id')

  @path_params = path_params

  if .is_a?(Hash) || .is_a?(String)
    ()
  end

  if options.is_a?(Hash)
    if options.has_key?(:file_name)
      add_file(options[:file_name], options[:file_content_type], options[:base64_encode])
    elsif options.has_key?(:text)
      add_file_text(options[:text])
    end
  end
end

Instance Attribute Details

#msgObject (readonly)

Returns the value of attribute msg.



8
9
10
# File 'lib/ringcentral_sdk/helpers/fax.rb', line 8

def msg
  @msg
end

Instance Method Details

#add_file(file_name = nil, content_type = nil, base64_encode = false) ⇒ Object



65
66
67
68
69
70
# File 'lib/ringcentral_sdk/helpers/fax.rb', line 65

def add_file(file_name=nil, content_type=nil, base64_encode=false)
  file_part = get_file_part(file_name, content_type, base64_encode)

  @msg.add(file_part)
  return true
end

#add_file_text(text = nil, charset = 'UTF-8') ⇒ Object



58
59
60
61
62
63
# File 'lib/ringcentral_sdk/helpers/fax.rb', line 58

def add_file_text(text=nil, charset='UTF-8')
  return unless text.is_a?(String)
  text_part = MIME::Text.new(text,'plain')
  text_part.headers.delete('Content-Id')
  @msg.add(text_part)
end

#add_metadata(meta = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/ringcentral_sdk/helpers/fax.rb', line 30

def (meta=nil)
  meta = (meta)
  json = MultiJson.encode(meta)
  json_part = MIME::Text.new(json)
  json_part.headers.delete('Content-Id')
  json_part.headers.set('Content-Type','application/json')
  @msg.add(json_part)
  return true
end

#bodyObject



142
143
144
# File 'lib/ringcentral_sdk/helpers/fax.rb', line 142

def body()
  return @msg.body.to_s
end

#content_typeObject



138
139
140
# File 'lib/ringcentral_sdk/helpers/fax.rb', line 138

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

#get_attachment_content_disposition(file_name = nil) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/ringcentral_sdk/helpers/fax.rb', line 106

def get_attachment_content_disposition(file_name=nil)
  base_name = File.basename(file_name.to_s)
  if base_name.is_a?(String) && base_name.length>0
    return "attachment; filename=\"#{base_name}\""
  else
    return 'attachment'
  end
end

#get_file_bytes(file_name = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ringcentral_sdk/helpers/fax.rb', line 87

def get_file_bytes(file_name=nil)

  unless File.file?(file_name.to_s)
    raise "File \"#{file_name.to_s}\" does not exist or cannot be read"
  end

  file_bytes = RUBY_VERSION < '1.9' \
    ? File.open(file_name, 'rb')        { |f| f.read } \
    : File.open(file_name, 'rb:BINARY') { |f| f.read }

  return file_bytes

end

#get_file_content_type(file_name = nil, content_type = nil) ⇒ Object



101
102
103
104
# File 'lib/ringcentral_sdk/helpers/fax.rb', line 101

def get_file_content_type(file_name=nil, content_type=nil)
  return (content_type.is_a?(String) && content_type =~ /^[^\/\s]+\/[^\/\s]+/) \
    ? content_type : MIME::Types.type_for(file_name).first.content_type || 'application/octet-stream'
end

#get_file_part(file_name = nil, content_type = nil, base64_encode = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ringcentral_sdk/helpers/fax.rb', line 72

def get_file_part(file_name=nil, content_type=nil, base64_encode=false)

  file_bytes = get_file_bytes(file_name)

  file_part  = base64_encode \
    ? MIME::Text.new(Base64.encode64(file_bytes)) \
    : MIME::Application.new(file_bytes)

  file_part.headers.delete('Content-Id')
  file_part.headers.set('Content-Type', get_file_content_type(file_name, content_type))
  file_part.headers.set('Content-Disposition', get_attachment_content_disposition(file_name))
  file_part.headers.set('Content-Transfer-Encoding', 'base64') if base64_encode
  return file_part
end

#inflate_metadata(meta = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ringcentral_sdk/helpers/fax.rb', line 40

def (meta=nil)
  if meta.is_a?(String)
    meta = MultiJson.decode(meta,:symbolize_keys=>true)
  end
  if meta.is_a?(Hash)
    inf = RingCentralSdk::Helpers::Inflator::ContactInfo.new

    if meta.has_key?(:to)
      meta[:to] = inf.inflate_to_array( meta[:to] )
    elsif meta.has_key?("to")
      meta["to"] = inf.inflate_to_array( meta["to"] )
    else
      meta[:to] = inf.inflate_to_array( nil )
    end
  end
  return meta
end

#methodObject



115
116
117
# File 'lib/ringcentral_sdk/helpers/fax.rb', line 115

def method()
  return 'post'
end

#urlObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ringcentral_sdk/helpers/fax.rb', line 119

def url()

  vals = {:account_id => '~', :extension_id => '~'}

  if @path_params.is_a?(Hash)
    vals.keys.each do |key|
      next unless @path_params.has_key?(key)
      if @path_params[key].is_a?(String) && @path_params[key].length>0
        vals[key] = @path_params[key]
      elsif @path_params[key].is_a?(Integer) && @path_params[key]>0
        vals[key] = @path_params[key].to_s
      end
    end
  end

  return "account/#{vals[:account_id].to_s}/extension/#{vals[:extension_id].to_s}/fax"

end