Module: Bearcat::Client::FileHelper

Defined in:
lib/bearcat/client/file_helper.rb

Instance Method Summary collapse

Instance Method Details

#confirm_file_upload(url) ⇒ Object



26
27
28
29
30
# File 'lib/bearcat/client/file_helper.rb', line 26

def confirm_file_upload(url)
  uri = URI(url)
  uri.scheme = uri.host = uri.port = nil
  get(uri.to_s)
end

#declare_file(api_path, params) ⇒ Object



11
12
13
# File 'lib/bearcat/client/file_helper.rb', line 11

def declare_file(api_path, params)
  post(api_path, params)
end

#file_params(file_path) ⇒ Object



4
5
6
7
8
9
# File 'lib/bearcat/client/file_helper.rb', line 4

def file_params(file_path)
  {
    size: File.open(file_path).size,
    name: File.basename(file_path)
  }.with_indifferent_access
end

#post_file(url, params, file_path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/bearcat/client/file_helper.rb', line 15

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_connectionObject



32
33
34
35
36
37
38
39
40
# File 'lib/bearcat/client/file_helper.rb', line 32

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