Class: WebpackerUploader::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/webpacker_uploader/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configWebpackerUploader::Configuration

Returns the global WebpackerUploader::Configuration object. Use this to set and retrieve specific configuration options.

Examples:

Disable log output

WebpackerUploader.config.log_output = false

Get the list of excluded file extension

puts WebpackerUploader.config.ignored_extensions

Returns:

See Also:



23
24
25
# File 'lib/webpacker_uploader/instance.rb', line 23

def config
  @config ||= WebpackerUploader::Configuration.new
end

Instance Method Details

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

Yields the global configuration to a block. Use this to configure the base gem features.

Examples:

WebpackerUploader.configure do |config|
  config.ignored_extensions = [".png", ".jpg", ".webp"]
  config.log_output = false
  config.public_manifest_path = "path/to/manifest.json"
  config.public_path = "path/to/public/dir"
end

Yields:

See Also:



39
40
41
# File 'lib/webpacker_uploader/instance.rb', line 39

def configure
  yield config
end

#upload!(provider, prefix: nil, cache_control: nil)

This method returns an undefined value.

Uploads assets using the supplied provider. Currently only AWS S3 is implemented.

Parameters:

  • provider (WebpackerUploader::Providers::Aws)

    A provider to use for file uploading.

  • prefix (String) (defaults to: nil)

    Used to prefix the remote file paths.

  • cache_control (String) (defaults to: nil)

    Used to add cache-control header to files.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/webpacker_uploader/instance.rb', line 49

def upload!(provider, prefix: nil, cache_control: nil)
  manifest.assets.each do |name, js_path|
    path = js_path[1..-1]

    remote_path =
      if prefix.nil?
        path
      else
        "#{prefix}/#{path}"
      end

    file_path = config.public_path.join(path)

    if name.end_with?(*config.ignored_extensions)
      config.logger.info("Skipping #{file_path}") if config.log_output?
    else
      content_type = WebpackerUploader::Mime.mime_type(path)

      config.logger.info("Processing #{file_path} as #{content_type}") if config.log_output?

      provider.upload!(remote_path, file_path, content_type, cache_control)
    end
  end
end