Class: FilestackClient

Inherits:
Object
  • Object
show all
Includes:
MultipartUploadUtils, UploadUtils
Defined in:
lib/filestack/models/filestack_client.rb

Overview

The Filestack FilestackClient class acts as a hub for all Filestack actions that do not require a file handle, including uploading files (both local and external), initiating an external transformation, and other tasks

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UploadUtils

#build_store_task, #get_url, #make_call, #send_upload

Methods included from MultipartUploadUtils

#create_upload_jobs, #get_file_attributes, #get_io_attributes, #multipart_complete, #multipart_start, #multipart_upload, #run_uploads, #upload_chunk

Constructor Details

#initialize(apikey, security: nil) ⇒ FilestackClient

Initialize FilestackClient

Parameters:

  • apikey (String)

    Your Filestack API key

  • security (FilestackSecurity) (defaults to: nil)

    A Filestack security object, if security is enabled



21
22
23
24
# File 'lib/filestack/models/filestack_client.rb', line 21

def initialize(apikey, security: nil)
  @apikey = apikey
  @security = security
end

Instance Attribute Details

#apikeyObject (readonly)

Returns the value of attribute apikey.



14
15
16
# File 'lib/filestack/models/filestack_client.rb', line 14

def apikey
  @apikey
end

#securityObject (readonly)

Returns the value of attribute security.



14
15
16
# File 'lib/filestack/models/filestack_client.rb', line 14

def security
  @security
end

Instance Method Details

#transform_external(external_url) ⇒ Filestack::Transform

Transform an external URL

Parameters:

  • external_url (string)

    A valid URL

Returns:

  • (Filestack::Transform)


52
53
54
# File 'lib/filestack/models/filestack_client.rb', line 52

def transform_external(external_url)
  Transform.new(external_url: external_url, security: @security, apikey: @apikey)
end

#upload(filepath: nil, external_url: nil, io: nil, options: {}, intelligent: false, timeout: 60, storage: 'S3') ⇒ Object

Upload a local file, external url or IO object return [Filestack::FilestackFilelink]

Parameters:

  • filepath (String) (defaults to: nil)

    The path of a local file

  • external_url (String) (defaults to: nil)

    An external URL

  • io (StringIO) (defaults to: nil)

    The IO object

  • options (Hash) (defaults to: {})

    User-supplied upload options

  • intelligent (Boolean) (defaults to: false)

    Upload file using Filestack Intelligent Ingestion

  • storage (String) (defaults to: 'S3')

    Default storage to be used for uploads



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/filestack/models/filestack_client.rb', line 35

def upload(filepath: nil, external_url: nil, io: nil, options: {}, intelligent: false, timeout: 60, storage: 'S3')
  return 'You cannot upload a URL and file at the same time' if (filepath || io) && external_url

  response = if external_url
              send_upload(@apikey, external_url, @security, options)
             else
              return 'You cannot upload IO object and file at the same time' if io && filepath
              multipart_upload(@apikey, filepath, io, @security, options, timeout, storage, intelligent)
             end

  FilestackFilelink.new(response['handle'], security: @security, apikey: @apikey)
end

#zip(destination, files) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/filestack/models/filestack_client.rb', line 56

def zip(destination, files)
  encoded_files = JSON.generate(files).gsub('"', '')
  zip_url = "#{FilestackConfig::CDN_URL}/#{@apikey}/zip/#{encoded_files}"
  escaped_zip_url = zip_url.gsub("[","%5B").gsub("]","%5D")
  response = UploadUtils.make_call(escaped_zip_url, 'get')
  File.write(destination, response.body)
end