Class: Rack::SmartCompress::Configuration

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

Constant Summary collapse

DEFAULT_MIN_SIZE =

1 KB

1024
DEFAULT_ENCODINGS =
%w[zstd br gzip deflate].freeze
DEFAULT_MIME_TYPES =
[
  "text/html",
  "text/plain",
  "text/css",
  "text/javascript",
  "text/xml",
  "application/json",
  "application/javascript",
  "application/x-javascript",
  "application/xml",
  "application/xhtml+xml",
  "application/rss+xml",
  "application/atom+xml",
  "application/wasm",
  "image/svg+xml"
].freeze
DEFAULT_EXCLUDED_MIME_TYPES =
[
  "image/jpeg",
  "image/png",
  "image/gif",
  "image/webp",
  "image/avif",
  "audio/",
  "video/",
  "application/zip",
  "application/x-gzip",
  "application/pdf"
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rack/smart_compress/configuration.rb', line 62

def initialize
  @min_size = DEFAULT_MIN_SIZE
  @encodings = DEFAULT_ENCODINGS.dup
  @mime_types = DEFAULT_MIME_TYPES.dup
  @exclude_mime_types = DEFAULT_EXCLUDED_MIME_TYPES.dup
  @cache = false
  @cache_size = 200
  @dynamic_levels = false
  @zstd_level = 3
  @brotli_level = 4
  @gzip_level = Zlib::DEFAULT_COMPRESSION
  @deflate_level = Zlib::DEFAULT_COMPRESSION
  @if_condition = nil
  @unless_condition = nil
  @instrumentation = true
  @static_assets = false
  @static_root = "public"
  @static_urls = ["/"]
  @static_headers = {}
  @static_cascade = true
end

Instance Attribute Details

#brotli_levelObject

Returns the value of attribute brotli_level.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def brotli_level
  @brotli_level
end

#cacheObject

Returns the value of attribute cache.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def cache
  @cache
end

#cache_sizeObject

Returns the value of attribute cache_size.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def cache_size
  @cache_size
end

#deflate_levelObject

Returns the value of attribute deflate_level.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def deflate_level
  @deflate_level
end

#dynamic_levelsObject

Returns the value of attribute dynamic_levels.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def dynamic_levels
  @dynamic_levels
end

#encodingsObject

Returns the value of attribute encodings.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def encodings
  @encodings
end

#exclude_mime_typesObject

Returns the value of attribute exclude_mime_types.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def exclude_mime_types
  @exclude_mime_types
end

#gzip_levelObject

Returns the value of attribute gzip_level.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def gzip_level
  @gzip_level
end

#if_conditionObject

Returns the value of attribute if_condition.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def if_condition
  @if_condition
end

#instrumentationObject

Returns the value of attribute instrumentation.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def instrumentation
  @instrumentation
end

#mime_typesObject

Returns the value of attribute mime_types.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def mime_types
  @mime_types
end

#min_sizeObject

Returns the value of attribute min_size.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def min_size
  @min_size
end

#static_assetsObject

Returns the value of attribute static_assets.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def static_assets
  @static_assets
end

#static_cascadeObject

Returns the value of attribute static_cascade.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def static_cascade
  @static_cascade
end

#static_headersObject

Returns the value of attribute static_headers.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def static_headers
  @static_headers
end

#static_rootObject

Returns the value of attribute static_root.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def static_root
  @static_root
end

#static_urlsObject

Returns the value of attribute static_urls.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def static_urls
  @static_urls
end

#unless_conditionObject

Returns the value of attribute unless_condition.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def unless_condition
  @unless_condition
end

#zstd_levelObject

Returns the value of attribute zstd_level.



42
43
44
# File 'lib/rack/smart_compress/configuration.rb', line 42

def zstd_level
  @zstd_level
end

Instance Method Details

#if(&block) ⇒ Object

Convenience aliases for :if and :unless



85
86
87
88
89
90
91
# File 'lib/rack/smart_compress/configuration.rb', line 85

def if(&block)
  if block_given?
    @if_condition = block
  else
    @if_condition
  end
end

#if=(val) ⇒ Object



93
94
95
# File 'lib/rack/smart_compress/configuration.rb', line 93

def if=(val)
  @if_condition = val
end

#to_hObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rack/smart_compress/configuration.rb', line 109

def to_h
  {
    min_size: @min_size,
    encodings: @encodings,
    mime_types: @mime_types,
    exclude_mime_types: @exclude_mime_types,
    cache: @cache,
    cache_size: @cache_size,
    dynamic_levels: @dynamic_levels,
    zstd_level: @zstd_level,
    brotli_level: @brotli_level,
    gzip_level: @gzip_level,
    deflate_level: @deflate_level,
    if: @if_condition,
    unless: @unless_condition,
    instrumentation: @instrumentation,
    static_assets: @static_assets,
    static_root: @static_root,
    static_urls: @static_urls,
    static_headers: @static_headers,
    static_cascade: @static_cascade
  }
end

#unless(&block) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/rack/smart_compress/configuration.rb', line 97

def unless(&block)
  if block_given?
    @unless_condition = block
  else
    @unless_condition
  end
end

#unless=(val) ⇒ Object



105
106
107
# File 'lib/rack/smart_compress/configuration.rb', line 105

def unless=(val)
  @unless_condition = val
end