lib/phaxio/client.rb in phaxio-2.0.0 vs lib/phaxio/client.rb in phaxio-2.0.1

- old
+ new

@@ -53,14 +53,11 @@ if content_type.start_with? 'application/json' body = JSON.parse(response.body).with_indifferent_access else extension = MimeTypeHelper.extension_for_mimetype content_type - filename = File.join( - Dir.tmpdir, - Dir::Tmpname.make_tmpname('phaxio-', "download.#{extension}") - ) + filename = File.join Dir.tmpdir, tmpname(extension) File.open(filename, 'wb') { |file| file.write response.body } body = {'success' => response.success?, 'data' => File.open(filename, 'rb')} end if response.success? @@ -91,16 +88,27 @@ raise Error::GeneralError, "#{status}: #{message}" end end end + def tmpname(extension) + t = Time.now.strftime("%Y%m%d") + "phaxio-#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-download.#{extension}" + end + def post endpoint, params = {} # Handle file params params.each do |k, v| next unless k.to_s == 'file' - mime_type = MimeTypeHelper.mimetype_for_file v.path - params[k] = Faraday::UploadIO.new v, mime_type + + if v.is_a? Array + file_param = v.map { |file| file_to_param file } + else + file_param = file_to_param v + end + + params[k] = file_param end conn.post endpoint, params end @@ -127,9 +135,14 @@ def default_params { api_key: Phaxio.api_key, api_secret: Phaxio.api_secret } + end + + def file_to_param file + mime_type = MimeTypeHelper.mimetype_for_file file.path + Faraday::UploadIO.new file, mime_type end end end end