Class: Redox::FileUpload

Inherits:
Object
  • Object
show all
Defined in:
lib/redox/file_upload.rb

Constant Summary collapse

ENDPOINT =
"https://blob.redoxengine.com/upload"

Instance Method Summary collapse

Constructor Details

#initialize(filename_or_io, content_type, upload_as_filename = nil) ⇒ FileUpload

Create a multipart file upload.

or an open IO object

Parameters:

  • filename_or_io (String, IO)

    Either a String filename of a local file

  • content_type (String)

    String content type of the file data

  • upload_as_filename (String) (defaults to: nil)

    (optional) Name to use for uploaded file



13
14
15
# File 'lib/redox/file_upload.rb', line 13

def initialize(filename_or_io, content_type, upload_as_filename = nil)
  @file_part = Faraday::FilePart.new(filename_or_io, content_type, upload_as_filename)
end

Instance Method Details

#perform(source) ⇒ Object

Upload the file.

Parameters:

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/redox/file_upload.rb', line 20

def perform(source)
  source.ensure_access_token

  connection = Faraday.new { |f|
    f.authorization :Bearer, source.access_token
    f.request :multipart
    f.headers[:accept] = "application/json"
  }
  res = connection.post ENDPOINT, {file: @file_part}

  raise Redox::Error.new(status: res.status, body: res.body) unless res.success?

  uri =
    begin
      JSON.parse(res.body)["URI"]
    rescue
    end
  raise Redox::Error.new(status: res.status, body: res.body) unless uri

  uri
end