module UploadUtils

Includes general utility functions for the Filestack Ruby SDK

Public Instance Methods

build_store_task(options = {}) click to toggle source
# File lib/filestack/utils/utils.rb, line 65
def build_store_task(options = {})
  return 'store' if options.nil? || options.empty?
  tasks = []
  options.each do |key, value|
    value = case key
            when :workflows
              [value.join('","')]
            when :path
              "\"#{value}\""
            else
              value
            end
    tasks.push("#{key}:#{value.to_s.downcase}")
  end
  "store=#{tasks.join(',')}"
end
get_url(base, handle: nil, path: nil, security: nil) click to toggle source

Generates the URL for a FilestackFilelink object @param [String] base The base Filestack URL @param [String] handle The FilestackFilelink handle (optional) @param [String] path The specific API path (optional) @param [String] security Security for the FilestackFilelink (optional)

@return [String]

# File lib/filestack/utils/utils.rb, line 121
def get_url(base, handle: nil, path: nil, security: nil)
  url_components = [base]

  url_components.push(path) unless path.nil?
  url_components.push(handle) unless handle.nil?
  url = url_components.join('/')

  if security
    policy = security.policy
    signature = security.signature
    security_path = "policy=#{policy}&signature=#{signature}"
    url = "#{url}?#{security_path}"
  end
  url
end
make_call(url, action, parameters: nil, headers: nil) click to toggle source

General request function @param [String] url The URL being called @param [String] action The specific HTTP action

('get', 'post', 'delete', 'put')

@param [Hash] parameters The query and/or body parameters

@return [Typhoeus::Request]

# File lib/filestack/utils/utils.rb, line 54
def make_call(url, action, parameters: nil, headers: nil)
  headers = if headers
              headers.merge!(FilestackConfig::HEADERS)
            else
              FilestackConfig::HEADERS
            end
  Typhoeus.public_send(
    action, url, params: parameters, headers: headers
  )
end
send_upload(apikey, external_url = nil, security = nil, options = nil) click to toggle source

Uploads to v1 REST API (for external URLs or if multipart is turned off)

@param [String] apikey Filestack API key @param [String] external_url External URL to be uploaded @param [FilestackSecurity] security Security object with

policy/signature

@param [Hash] options User-defined options for

multipart uploads

@return [Hash]

# File lib/filestack/utils/utils.rb, line 91
def send_upload(apikey, external_url = nil, security = nil, options = nil)
  base = "#{FilestackConfig::CDN_URL}/#{apikey}/#{build_store_task(options)}"

  if security
    policy = security.policy
    signature = security.signature
    base = "#{base}/security=s:#{signature},p:#{policy}"
  end

  response = Typhoeus.post("#{base}/#{external_url}", headers: FilestackConfig::HEADERS)

  if response.code == 200
    begin
      response_body = JSON.parse(response.body)
      handle = response_body['url'].split('/').last
      return { 'handle' => handle }
    rescue
      raise response.body
    end
  end
  raise response.body
end