class Uploadcare::Client::UploaderClient

This is client for general uploads

@see uploadcare.com/api-refs/upload-api/#tag/Upload

Public Instance Methods

upload(file, **options) click to toggle source

syntactic sugar for upload_many There is actual upload method for one file, but it is redundant

# File lib/uploadcare/client/uploader_client.rb, line 25
def upload(file, **options)
  upload_many([file], **options)
end
upload_from_url(url, **options) click to toggle source

Upload files from url @see uploadcare.com/api-refs/upload-api/#operation/fromURLUpload options:

  • check_URL_duplicates

  • filename

  • save_URL_duplicates

  • async - returns upload token instead of upload data

# File lib/uploadcare/client/uploader_client.rb, line 36
def upload_from_url(url, **options)
  body = upload_from_url_body(url, **options)
  token_response = post(path: 'from_url/', headers: { 'Content-type': body.content_type }, body: body)
  return token_response if options[:async]

  uploaded_response = poll_upload_response(token_response.success[:token])
  return uploaded_response if uploaded_response.success[:status] == 'error'

  Dry::Monads::Success(files: [uploaded_response.success])
end
upload_many(arr, **options) click to toggle source

@see uploadcare.com/api-refs/upload-api/#operation/baseUpload

# File lib/uploadcare/client/uploader_client.rb, line 15
def upload_many(arr, **options)
  body = upload_many_body(arr, **options)
  post(path: 'base/',
       headers: { 'Content-type': body.content_type },
       body: body)
end

Private Instance Methods

api_struct_post(**args)
Alias for: post
get_status_response(token) click to toggle source

Check upload status

@see uploadcare.com/api-refs/upload-api/#operation/fromURLUploadStatus

# File lib/uploadcare/client/uploader_client.rb, line 68
def get_status_response(token)
  query_params = { token: token }
  get(path: 'from_url/status/', params: query_params)
end
poll_upload_response(token) click to toggle source
# File lib/uploadcare/client/uploader_client.rb, line 54
def poll_upload_response(token)
  with_retries(max_tries: Uploadcare.config.max_request_tries,
               base_sleep_seconds: Uploadcare.config.base_request_sleep,
               max_sleep_seconds: Uploadcare.config.max_request_sleep) do
    response = get_status_response(token)
    raise RequestError if %w[progress waiting unknown].include?(response.success[:status])

    response
  end
end
post(**args) click to toggle source
# File lib/uploadcare/client/uploader_client.rb, line 50
def post(**args)
  handle_throttling { api_struct_post(**args) }
end
Also aliased as: api_struct_post
upload_from_url_body(url, **options) click to toggle source

Prepare upload_from_url initial request body

# File lib/uploadcare/client/uploader_client.rb, line 85
def upload_from_url_body(url, **options)
  HTTP::FormData::Multipart.new({
    'pub_key': Uploadcare.config.public_key,
    'source_url': url
  }.merge(**options))
end
upload_many_body(arr, **options) click to toggle source

Prepares body for upload_many method

# File lib/uploadcare/client/uploader_client.rb, line 74
def upload_many_body(arr, **options)
  files_formdata = arr.map do |file|
    [HTTP::FormData::File.new(file).filename,
     HTTP::FormData::File.new(file)]
  end .to_h
  HTTP::FormData::Multipart.new(
    Param::Upload::UploadParamsGenerator.call(options[:store]).merge(files_formdata)
  )
end