lib/ringcentral_sdk/helpers/fax.rb in ringcentral_sdk-0.0.4 vs lib/ringcentral_sdk/helpers/fax.rb in ringcentral_sdk-0.1.0

- old
+ new

@@ -5,11 +5,11 @@ module RingCentralSdk::Helpers class CreateFaxRequest < RingCentralSdk::Helpers::Request attr_reader :msg - def initialize(path_params=nil,metadata=nil,options=nil) + def initialize(path_params=nil, metadata=nil, options=nil) @msg = MIME::Multipart::Mixed.new @msg.headers.delete('Content-Id') @path_params = path_params @@ -18,116 +18,110 @@ add_metadata(metadata) end if options.is_a?(Hash) if options.has_key?(:file_name) - if options.has_key?(:base64_encode) && options[:base64_encode] - add_file_base64(options[:file_name],options[:file_content_type]) - else - add_file_octet_stream(options[:file_name]) - end + 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 - def add_metadata(json=nil) - if json.is_a?(Hash) - json = MultiJson.encode(json) + def add_metadata(meta=nil) + meta = inflate_metadata(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 + + def inflate_metadata(meta=nil) + if meta.is_a?(String) + meta = MultiJson.decode(meta,:symbolize_keys=>true) end - if json.is_a?(String) - 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 + 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 false + return meta end - def add_file_text(text=nil,charset='UTF-8') + 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 - def add_file_base64(file_name=nil,content_type=nil) - unless file_name.is_a?(String) && File.file?(file_name) - return false - end + def add_file(file_name=nil, content_type=nil, base64_encode=false) + file_part = get_file_part(file_name, content_type, base64_encode) - content_type = (content_type.is_a?(String) && content_type =~ /^[^\/\s]+\/[^\/\s]+/) \ - ? content_type : MIME::Types.type_for(file_name).first.content_type + @msg.add(file_part) + return true + end - base_name = File.basename(file_name) - file_base64 = Base64.encode64(File.binread(file_name)) + def get_file_part(file_name=nil, content_type=nil, base64_encode=false) - base64_part = MIME::Text.new(file_base64) - base64_part.headers.delete('Content-Id') - base64_part.headers.set('Content-Type', content_type) - base64_part.headers.set('Content-Transfer-Encoding','base64') - if base_name.is_a?(String) && base_name.length>0 - base64_part.headers.set('Content-Disposition', "attachment; filename=\"#{base_name}\"") - else - base64_part.headers.set('Content-Disposition', 'attachment') + unless File.file?(file_name.to_s) + raise "File \"#{file_name.to_s}\" does not exist or cannot be read" end - @msg.add(base64_part) - return true + file_part = base64_encode \ + ? MIME::Text.new(Base64.encode64(File.binread(file_name))) \ + : MIME::Application.new(File.binread(file_name)) + 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 - def add_file_octet_stream(file_name=nil) - unless file_name.is_a?(String) && File.file?(file_name) - return false - end + 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 - content_type = (content_type.is_a?(String) && content_type =~ /^[^\/\s]+\/[^\/\s]+/) \ - ? content_type : MIME::Types.type_for(file_name).first.content_type - - base_name = File.basename(file_name) - file_bytes = File.binread(file_name) - - file_part = MIME::Application.new(file_bytes) - file_part.headers.delete('Content-Id') - file_part.headers.set('Content-Type', content_type) + 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 - file_part.headers.set('Content-Disposition', "attachment; filename=\"#{base_name}\"") + return "attachment; filename=\"#{base_name}\"" else - file_part.headers.set('Content-Disposition', 'attachment') + return 'attachment' end - - @msg.add(file_part) - return true end def method() return 'post' end def url() - account_id = "~" - extension_id = "~" + + vals = {:account_id => '~', :extension_id => '~'} + if @path_params.is_a?(Hash) - if @path_params.has_key?(:account_id) - if @path_params[:account_id].is_a?(String) && @path_params[:account_id].length>0 - account_id = @path_params[:account_id] - elsif @path_params[:account_id].is_a?(Integer) && @path_params[:account_id]>0 - account_id = @path_params[:account_id].to_s + 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 - if @path_params.has_key?(:extension_id) - if @path_params[:extension_id].is_a?(String) && @path_params[:extension_id].length>0 - extension_id = @path_params[:extension_id] - elsif @path_params[:extension_id].is_a?(Integer) && @path_params[:extension_id]>0 - extension_id = @path_params[:extension_id].to_s - end - end end - url = "account/#{account_id.to_s}/extension/#{extension_id.to_s}/fax" - return url + + return "account/#{vals[:account_id].to_s}/extension/#{vals[:extension_id].to_s}/fax" + end def content_type() return @msg.headers.get('Content-Type').to_s end @@ -135,17 +129,17 @@ def body() return @msg.body.to_s end # Experimental - def _add_file(file_name=nil) - if file_name.is_a?(String) && File.file?(file_name) - file_msg = MIME::DiscreteMediaFactory.create(file_name) - file_msg.headers.delete('Content-Id') - @msg.add(file_msg) - return true - end - return false - end - private :_add_file + #def _add_file(file_name=nil) + # if file_name.is_a?(String) && File.file?(file_name) + # file_msg = MIME::DiscreteMediaFactory.create(file_name) + # file_msg.headers.delete('Content-Id') + # @msg.add(file_msg) + # return true + # end + # return false + #end + #private :_add_file end end \ No newline at end of file