Module: FileHelper
- Included in:
- Bearcat::Client
- Defined in:
- lib/bearcat/client/file_helper.rb
Instance Method Summary collapse
- #confirm_file_upload(url) ⇒ Object
- #declare_file(api_path, params) ⇒ Object
- #file_params(file_path) ⇒ Object
- #post_file(url, params, file_path) ⇒ Object
- #upload_connection ⇒ Object
Instance Method Details
#confirm_file_upload(url) ⇒ Object
25 26 27 28 29 |
# File 'lib/bearcat/client/file_helper.rb', line 25 def confirm_file_upload(url) uri = URI(url) query = uri.query query.blank? ? get(uri.path) : get(uri.path, CGI::parse(query)) end |
#declare_file(api_path, params) ⇒ Object
10 11 12 |
# File 'lib/bearcat/client/file_helper.rb', line 10 def declare_file(api_path, params) post(api_path, params) end |
#file_params(file_path) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/bearcat/client/file_helper.rb', line 3 def file_params(file_path) { size: File.open(file_path).size, name: File.basename(file_path) } end |
#post_file(url, params, file_path) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/bearcat/client/file_helper.rb', line 14 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 |
#upload_connection ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/bearcat/client/file_helper.rb', line 31 def upload_connection Faraday.new do |f| f.[:open_timeout] = ENV.fetch('FARADAY_OPEN_TIMEOUT', 60).to_i f.[:timeout] = ENV.fetch('FARADAY_TIMEOUT', 60).to_i f.request :multipart f.request :url_encoded f.adapter :net_http end end |