Class: SimpleImagesDownloader::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/simple_images_downloader/configuration.rb

Overview

Configuration class

Examples:

SimpleImagesDownloader::Configuration.configure do |config|
  config.destination = './images'
  config.valid_mime_types = ['image/jpeg', 'image/png']
end

SimpleImagesDownloader::Configuration.destination
# => './images'

SimpleImagesDownloader::Configuration.valid_mime_types
# => ['image/jpeg', 'image/png']

Constant Summary collapse

ACCESSORS =
%i[
  destination
  valid_mime_types
].freeze
REQUEST_OPTIONS =
{
  'User-Agent' => "SimpleImagesDownloader/#{SimpleImagesDownloader::VERSION}",
  redirect: false,
  open_timeout: 30,
  read_timeout: 30
}.freeze
DEFAULT_VALID_MIME_TYPES =
[
  'image/avif',
  'image/gif',
  'image/apng',
  'image/jpg',
  'image/jpeg',
  'image/png',
  'image/svg+xml',
  'image/webp'
].freeze
DEFAULT_DESTINATION =
'./'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



48
49
50
51
# File 'lib/simple_images_downloader/configuration.rb', line 48

def initialize
  @destination = DEFAULT_DESTINATION
  @valid_mime_types = DEFAULT_VALID_MIME_TYPES
end

Class Method Details

.accessor?(method) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/simple_images_downloader/configuration.rb', line 91

def self.accessor?(method)
  ACCESSORS.include?(method)
end

.configure {|config| ... } ⇒ Object

Configurable options

destination: String, default: ‘./’, description: ‘Destination folder to put downloaded images’ valid_mime_types: Array, default: [

'image/avif', 'image/gif', 'image/apng', 'image/jpg',
'image/jpeg', 'image/png', 'image/svg+xml', 'image/webp'

], description: ‘Valid mime types to download’

Yields:

  • (config)

    Yields the Configuration object to the block



76
77
78
# File 'lib/simple_images_downloader/configuration.rb', line 76

def self.configure
  yield instance
end

.method_missing(method, *args) ⇒ Object

rubocop:enable Style/OptionalBooleanParameter



86
87
88
89
# File 'lib/simple_images_downloader/configuration.rb', line 86

def self.method_missing(method, *args)
  super unless accessor?(method.to_s.gsub(/=$/, '').to_sym)
  instance.public_send(method, *args)
end

.respond_to_missing?(method, include_private = false) ⇒ Boolean

rubocop:disable Style/OptionalBooleanParameter

Returns:

  • (Boolean)


81
82
83
# File 'lib/simple_images_downloader/configuration.rb', line 81

def self.respond_to_missing?(method, include_private = false)
  accessor?(method) || super
end

Instance Method Details

#valid_mime_types=(value) ⇒ Object

Allows to set valid_mime_types to check against allowed mime types

RFC 1341 - MIME (Multipurpose Internet Mail Extensions) format

Parameters:

  • value (Array)

    Array of valid mime types followed by

Raises:

  • (BaseError)

    if value is not an Array



59
60
61
62
63
# File 'lib/simple_images_downloader/configuration.rb', line 59

def valid_mime_types=(value)
  raise Errors::BaseError, 'valid_mime_types must be an array' unless value.is_a?(Array)

  @valid_mime_types = value
end