lib/ringcentral_sdk/helpers/fax.rb in ringcentral_sdk-0.1.2 vs lib/ringcentral_sdk/helpers/fax.rb in ringcentral_sdk-0.1.3

- old
+ new

@@ -69,26 +69,34 @@ return true end def get_file_part(file_name=nil, content_type=nil, base64_encode=false) - unless File.file?(file_name.to_s) - raise "File \"#{file_name.to_s}\" does not exist or cannot be read" - end + file_bytes = get_file_bytes(file_name) - file_bytes = RUBY_VERSION < '1.9' \ - ? File.open(file_name, 'rb') { |f| f.read } \ - : File.open(file_name, 'rb:BINARY') { |f| f.read } - 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 + + 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 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' \ No newline at end of file