lib/bearcat/client/file_helper.rb in bearcat-1.4.8 vs lib/bearcat/client/file_helper.rb in bearcat-1.4.9

- old
+ new

@@ -1,40 +1,43 @@ -module FileHelper +module Bearcat + class Client < Footrest::Client + module FileHelper + def file_params(file_path) + { + size: File.open(file_path).size, + name: File.basename(file_path) + } + end - def file_params(file_path) - { - size: File.open(file_path).size, - name: File.basename(file_path) - } - end + def declare_file(api_path, params) + post(api_path, params) + end - def declare_file(api_path, params) - post(api_path, params) - end + def post_file(url, params, file_path) + params['Filename'] = File.basename(file_path) + params['file'] = Faraday::UploadIO.new(file_path, params['content-type']) + response = upload_connection.post(url, params) + if [201, 302, 303].include? response.status #success if it is a redirect or 201 + response.headers['Location'] + else + raise 'FailedFileUpload' + end + end - def post_file(url, params, file_path) - params['Filename'] = File.basename(file_path) - params['file'] = Faraday::UploadIO.new(file_path, params['content-type']) - response = upload_connection.post(url, params) - if [201, 302, 303].include? response.status #success if it is a redirect or 201 - response.headers['Location'] - else - raise 'FailedFileUpload' - end - end + def confirm_file_upload(url) + uri = URI(url) + query = uri.query + query.blank? ? get(uri.path) : get(uri.path, CGI::parse(query)) + end - def confirm_file_upload(url) - uri = URI(url) - query = uri.query - query.blank? ? get(uri.path) : get(uri.path, CGI::parse(query)) - end - - def upload_connection - Faraday.new do |f| - f.options[:open_timeout] = ENV.fetch('FARADAY_OPEN_TIMEOUT', 60).to_i - f.options[:timeout] = ENV.fetch('FARADAY_TIMEOUT', 60).to_i - f.request :multipart - f.request :url_encoded - f.adapter :net_http + def upload_connection + Faraday.new do |f| + f.options[:open_timeout] = ENV.fetch('FARADAY_OPEN_TIMEOUT', 60).to_i + f.options[:timeout] = ENV.fetch('FARADAY_TIMEOUT', 60).to_i + f.request :multipart + f.request :url_encoded + f.adapter :net_http + end + end end end end