lib/hello_sign/client.rb in hellosign-ruby-sdk-3.0.18 vs lib/hello_sign/client.rb in hellosign-ruby-sdk-3.0.19

- old
+ new

@@ -22,14 +22,17 @@ # SOFTWARE. # require 'faraday' require 'multi_json' +require 'mime/types' require 'hello_sign/error' require 'hello_sign/configuration' require 'hello_sign/resource' require 'hello_sign/api' +require 'openssl' + OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE require 'logger' module HelloSign # @@ -185,17 +188,47 @@ "Server responded with code #{response.status}\n" \ "Request URI: #{response.to_hash[:url].to_s}\n"\ "Message: #{response.body}" end + def MIMEfromName(name) + parts = name.split('.') + #default to pdf if no extension + if parts.length < 2 + return 'application/pdf' + end + extension = parts[-1] + types = MIME::Types.type_for(extension) + types[0] + end + + def MIMEfromIO(file) + begin + path = File.path file + MIMEfromName path + # in case of error in type detection, return default type + rescue + return 'application/pdf' + end + end + def prepare_files(opts) if opts[:files] opts[:files].each_with_index do |file, index| if file.is_a? String - opts[:"file[#{index}]"] = Faraday::UploadIO.new(StringIO.new(file), 'application/pdf') + if File.file?(file) + mime_type = MIMEfromName file + opts[:"file[#{index}]"] = Faraday::UploadIO.new(file, mime_type) + else + raise HelloSign::Error::FileNotFound.new "#{file} was not found on the filesystem" + end + elsif file.is_a? File + mime_type = MIMEfromIO file + opts[:"file[#{index}]"] = Faraday::UploadIO.new(file, mime_type) elsif defined? ActionDispatch::Http::UploadedFile if file.is_a? ActionDispatch::Http::UploadedFile - opts[:"file[#{index}]"] = UploadIO.new(file.tempfile, 'application/pdf') + mime_type MIMEfromIO file + opts[:"file[#{index}]"] = UploadIO.new(file.tempfile, mime_type) end else raise HelloSign::Error::NotSupportedType.new "#{file.class} is not a supported. Must be a string or ActionDispatch::Http::UploadedFile" end end