Class: ContentDisposition

Inherits:
Object
  • Object
show all
Defined in:
lib/content_disposition.rb,
lib/content_disposition/version.rb

Constant Summary collapse

ATTACHMENT =
"attachment"
INLINE =
"inline"
DEFAULT_TO_ASCII =
->(filename) do
  filename.encode("US-ASCII", undef: :replace, replace: "?")
end
TRADITIONAL_ESCAPED_CHAR =
/[^ A-Za-z0-9!#$+.^_`|~-]/
RFC_5987_ESCAPED_CHAR =
/[^A-Za-z0-9!#$&+.^_`|~-]/
VERSION =
"1.0.0"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(disposition:, filename:, to_ascii: nil) ⇒ ContentDisposition

Returns a new instance of ContentDisposition.



32
33
34
35
36
37
38
39
40
# File 'lib/content_disposition.rb', line 32

def initialize(disposition:, filename:, to_ascii: nil)
  unless [ATTACHMENT, INLINE].include?(disposition.to_s)
    fail ArgumentError, "unknown disposition: #{disposition.inspect}"
  end

  @disposition = disposition
  @filename    = filename
  @to_ascii    = to_ascii || self.class.to_ascii || DEFAULT_TO_ASCII
end

Class Attribute Details

.to_asciiObject

Returns the value of attribute to_ascii.



27
28
29
# File 'lib/content_disposition.rb', line 27

def to_ascii
  @to_ascii
end

Instance Attribute Details

#dispositionObject (readonly)

Returns the value of attribute disposition.



30
31
32
# File 'lib/content_disposition.rb', line 30

def disposition
  @disposition
end

#filenameObject (readonly)

Returns the value of attribute filename.



30
31
32
# File 'lib/content_disposition.rb', line 30

def filename
  @filename
end

#to_asciiObject (readonly)

Returns the value of attribute to_ascii.



30
31
32
# File 'lib/content_disposition.rb', line 30

def to_ascii
  @to_ascii
end

Class Method Details

.attachment(filename = nil) ⇒ Object



14
15
16
# File 'lib/content_disposition.rb', line 14

def attachment(filename = nil)
  format(disposition: ATTACHMENT, filename: filename)
end

.format(**options) ⇒ Object Also known as: call



22
23
24
# File 'lib/content_disposition.rb', line 22

def format(**options)
  new(**options).to_s
end

.inline(filename = nil) ⇒ Object



18
19
20
# File 'lib/content_disposition.rb', line 18

def inline(filename = nil)
  format(disposition: INLINE, filename: filename)
end

Instance Method Details

#ascii_filenameObject



52
53
54
# File 'lib/content_disposition.rb', line 52

def ascii_filename
  'filename="' + percent_escape(to_ascii[filename], TRADITIONAL_ESCAPED_CHAR) + '"'
end

#to_sObject



42
43
44
45
46
47
48
# File 'lib/content_disposition.rb', line 42

def to_s
  if filename
    "#{disposition}; #{ascii_filename}; #{utf8_filename}"
  else
    "#{disposition}"
  end
end

#utf8_filenameObject



58
59
60
# File 'lib/content_disposition.rb', line 58

def utf8_filename
  "filename*=UTF-8''" + percent_escape(filename, RFC_5987_ESCAPED_CHAR)
end