Class: Uploadcare::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/uploadcare/configuration.rb

Overview

Configuration container for client defaults and per-client overrides.

Constant Summary collapse

DEFAULTS =

Default configuration values used as the template for new instances.

{
  public_key: nil,
  secret_key: nil,
  auth_type: 'Uploadcare',
  multipart_size_threshold: 100 * 1024 * 1024,
  rest_api_root: 'https://api.uploadcare.com',
  upload_api_root: 'https://upload.uploadcare.com',
  max_request_tries: 100,
  base_request_sleep: 1,         # seconds
  max_request_sleep: 60.0,       # seconds
  sign_uploads: false,
  upload_signature_lifetime: 30 * 60, # seconds
  max_throttle_attempts: 5,
  upload_threads: 2, # used for multiupload only ATM
  framework_data: '',
  file_chunk_size: 100,
  logger: nil,
  use_subdomains: false,
  cdn_base_postfix: 'https://ucarecd.net/',
  default_cdn_base: 'https://ucarecdn.com/',
  multipart_chunk_size: 5 * 1024 * 1024, # 5MB chunks for multipart upload
  upload_timeout: 60,            # seconds
  max_upload_retries: 3          # retry failed uploads 3 times
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Configuration

Returns a new instance of Configuration.

Parameters:

  • options (Hash)

    Configuration overrides



40
41
42
43
44
45
46
47
48
49
# File 'lib/uploadcare/configuration.rb', line 40

def initialize(**options)
  values = DEFAULTS.merge(options)
  values[:public_key] = ENV.fetch('UPLOADCARE_PUBLIC_KEY', '') unless options.key?(:public_key)
  values[:secret_key] = ENV.fetch('UPLOADCARE_SECRET_KEY', '') unless options.key?(:secret_key)

  values.each do |attribute, value|
    send("#{attribute}=", value)
  end
  @logger ||= Logger.new($stdout)
end

Instance Attribute Details

#auth_typeObject

Returns the value of attribute auth_type.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def auth_type
  @auth_type
end

#base_request_sleepObject

Returns the value of attribute base_request_sleep.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def base_request_sleep
  @base_request_sleep
end

#cdn_base_postfixObject

Returns the value of attribute cdn_base_postfix.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def cdn_base_postfix
  @cdn_base_postfix
end

#default_cdn_baseObject

Returns the value of attribute default_cdn_base.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def default_cdn_base
  @default_cdn_base
end

#file_chunk_sizeObject

Returns the value of attribute file_chunk_size.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def file_chunk_size
  @file_chunk_size
end

#framework_dataObject

Returns the value of attribute framework_data.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def framework_data
  @framework_data
end

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def logger
  @logger
end

#max_request_sleepObject

Returns the value of attribute max_request_sleep.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def max_request_sleep
  @max_request_sleep
end

#max_request_triesObject

Returns the value of attribute max_request_tries.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def max_request_tries
  @max_request_tries
end

#max_throttle_attemptsObject

Returns the value of attribute max_throttle_attempts.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def max_throttle_attempts
  @max_throttle_attempts
end

#max_upload_retriesObject

Returns the value of attribute max_upload_retries.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def max_upload_retries
  @max_upload_retries
end

#multipart_chunk_sizeObject

Returns the value of attribute multipart_chunk_size.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def multipart_chunk_size
  @multipart_chunk_size
end

#multipart_size_thresholdObject

Returns the value of attribute multipart_size_threshold.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def multipart_size_threshold
  @multipart_size_threshold
end

#public_keyObject

Returns the value of attribute public_key.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def public_key
  @public_key
end

#rest_api_rootObject

Returns the value of attribute rest_api_root.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def rest_api_root
  @rest_api_root
end

#secret_keyObject

Returns the value of attribute secret_key.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def secret_key
  @secret_key
end

#sign_uploadsObject

Returns the value of attribute sign_uploads.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def sign_uploads
  @sign_uploads
end

#upload_api_rootObject

Returns the value of attribute upload_api_root.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def upload_api_root
  @upload_api_root
end

#upload_signature_lifetimeObject

Returns the value of attribute upload_signature_lifetime.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def upload_signature_lifetime
  @upload_signature_lifetime
end

#upload_threadsObject

Returns the value of attribute upload_threads.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def upload_threads
  @upload_threads
end

#upload_timeoutObject

Returns the value of attribute upload_timeout.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def upload_timeout
  @upload_timeout
end

#use_subdomainsObject

Returns the value of attribute use_subdomains.



7
8
9
# File 'lib/uploadcare/configuration.rb', line 7

def use_subdomains
  @use_subdomains
end

Instance Method Details

#cdn_baseString

Resolve the CDN base URL for this configuration.

Returns:

  • (String)


61
62
63
64
65
# File 'lib/uploadcare/configuration.rb', line 61

def cdn_base
  return Uploadcare::CnameGenerator.cdn_base_postfix(config: self) if use_subdomains

  default_cdn_base
end

#custom_cnameString

Build the deterministic subdomain prefix for the configured public key.

Returns:

  • (String)


54
55
56
# File 'lib/uploadcare/configuration.rb', line 54

def custom_cname
  Uploadcare::CnameGenerator.generate_cname(public_key: public_key)
end

#to_hHash

Convert this configuration to a serializable hash.

Returns:

  • (Hash)


78
79
80
# File 'lib/uploadcare/configuration.rb', line 78

def to_h
  DEFAULTS.keys.to_h { |attribute| [attribute, public_send(attribute)] }
end

#with(**options) ⇒ Uploadcare::Configuration

Clone this configuration with overrides.

Parameters:

  • options (Hash)

Returns:



71
72
73
# File 'lib/uploadcare/configuration.rb', line 71

def with(**options)
  self.class.new(**to_h, **options)
end