summaryrefslogtreecommitdiff
path: root/lib/mime/type.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/mime/type.rb')
-rw-r--r--lib/mime/type.rb92
1 files changed, 48 insertions, 44 deletions
diff --git a/lib/mime/type.rb b/lib/mime/type.rb
index 352af93..2a86bdc 100644
--- a/lib/mime/type.rb
+++ b/lib/mime/type.rb
@@ -50,8 +50,9 @@ class MIME::Type
end
def to_s
- "Invalid Encoding #{@encoding.inspect} (valid values: #{VALID_ENCODINGS.inspect})."
+ "Invalid Encoding #{@encoding.inspect}"
end
+
# :startdoc:
end
@@ -71,16 +72,17 @@ class MIME::Type
TEXT_ENCODINGS = %w(7bit quoted-printable)
VALID_ENCODINGS = DEFAULT_ENCODINGS + BINARY_ENCODINGS + TEXT_ENCODINGS
- IANA_URL = "http://www.iana.org/assignments/media-types/%s/%s"
- RFC_URL = "http://rfc-editor.org/rfc/rfc%s.txt"
- DRAFT_URL = "http://datatracker.ietf.org/public/idindex.cgi?command=id_details&filename=%s"
- CONTACT_URL = "http://www.iana.org/assignments/contact-people.htm#%s"
+ IANA_URL = 'http://www.iana.org/assignments/media-types/%s/%s'
+ RFC_URL = 'http://rfc-editor.org/rfc/rfc%s.txt'
+ DRAFT_URL = 'http://datatracker.ietf.org/public/idindex.cgi?command=id_details&filename=%s' # rubocop:disable Metrics/LineLength
+ CONTACT_URL = 'http://www.iana.org/assignments/contact-people.htm#%s'
# :startdoc:
if respond_to? :private_constant
private_constant :MEDIA_TYPE_RE, :UNREGISTERED_RE, :I18N_RE, :PLATFORM_RE,
- :DEFAULT_ENCODINGS, :BINARY_ENCODINGS, :TEXT_ENCODINGS, :VALID_ENCODINGS,
- :IANA_URL, :RFC_URL, :DRAFT_URL, :CONTACT_URL
+ :DEFAULT_ENCODINGS, :BINARY_ENCODINGS, :TEXT_ENCODINGS,
+ :VALID_ENCODINGS, :IANA_URL, :RFC_URL, :DRAFT_URL,
+ :CONTACT_URL
end
# Builds a MIME::Type object from the +content_type+, a MIME Content Type
@@ -97,33 +99,33 @@ class MIME::Type
#
# Yields the newly constructed +self+ object.
def initialize(content_type) # :yields self:
- @friendly = {}
- self.system = nil
- self.obsolete = false
- self.registered = nil
+ @friendly = {}
+ self.system = nil
+ self.obsolete = false
+ self.registered = nil
self.use_instead = nil
- self.signature = nil
+ self.signature = nil
case content_type
when Hash
init_with(content_type)
when Array
self.content_type = content_type[0]
- self.extensions = content_type[1] || []
+ self.extensions = content_type[1] || []
when MIME::Type
init_with(content_type.to_h)
else
self.content_type = content_type
end
- self.extensions ||= []
- self.docs ||= []
- self.encoding ||= :default
+ self.extensions ||= []
+ self.docs ||= []
+ self.encoding ||= :default
# This value will be deprecated in the future, as it will be an
# alternative view on #xrefs. Silence an unnecessary warning for now by
# assigning directly to the instance variable.
- @references ||= []
- self.xrefs ||= {}
+ @references ||= []
+ self.xrefs ||= {}
yield self if block_given?
end
@@ -281,11 +283,11 @@ class MIME::Type
attr_reader :encoding
def encoding=(enc) # :nodoc:
if DEFAULT_ENCODINGS.include?(enc)
- @encoding = self.default_encoding
+ @encoding = default_encoding
elsif BINARY_ENCODINGS.include?(enc) or TEXT_ENCODINGS.include?(enc)
@encoding = enc
else
- raise InvalidEncoding, enc
+ fail InvalidEncoding, enc
end
end
@@ -303,7 +305,7 @@ class MIME::Type
if os.nil? or os.kind_of?(Regexp)
@system = os
else
- @system = %r|#{os}|
+ @system = %r{#{os}}
end
end
@@ -350,7 +352,7 @@ class MIME::Type
when Hash
@friendly.merge!(lang)
else
- raise ArgumentError
+ fail ArgumentError
end
end
@@ -468,7 +470,7 @@ class MIME::Type
}
when 'person'.freeze
values.map { |data|
- 'http://www.iana.org/assignments/media-types/media-types.xhtml#%s'.freeze % data
+ 'http://www.iana.org/assignments/media-types/media-types.xhtml#%s'.freeze % data # rubocop:disable Metrics/LineLength
}
when 'template'.freeze
values.map { |data|
@@ -519,7 +521,7 @@ class MIME::Type
# formats. This method returns +false+ when the MIME::Type encoding is
# set to <tt>base64</tt>.
def ascii?
- not binary?
+ !binary?
end
# Returns +true+ when the simplified MIME::Type is in the list of known
@@ -538,7 +540,7 @@ class MIME::Type
# mime-types.
def system?(__internal__ = false)
MIME.deprecated(self, __method__) unless __internal__
- not @system.nil?
+ !@system.nil?
end
# Returns +true+ if the MIME::Type is specific to the current operating
@@ -554,7 +556,7 @@ class MIME::Type
# Returns +true+ if the MIME::Type specifies an extension list,
# indicating that it is a complete MIME::Type.
def complete?
- not @extensions.empty?
+ !@extensions.empty?
end
# Returns the MIME::Type as a string.
@@ -636,7 +638,7 @@ class MIME::Type
def init_with(coder)
self.content_type = coder['content-type']
self.docs = coder['docs'] || []
- self.friendly(coder['friendly'] || {})
+ friendly(coder['friendly'] || {})
self.encoding = coder['encoding']
self.extensions = coder['extensions'] || []
self.obsolete = coder['obsolete']
@@ -666,13 +668,13 @@ class MIME::Type
MEDIA_TYPE_RE.match(content_type)
end
- if matchdata
- matchdata.captures.map { |e|
- e.downcase!
- e.gsub!(UNREGISTERED_RE, ''.freeze)
- e
- }.join('/'.freeze)
- end
+ return unless matchdata
+
+ matchdata.captures.map { |e|
+ e.downcase!
+ e.gsub!(UNREGISTERED_RE, ''.freeze)
+ e
+ }.join('/'.freeze)
end
# Converts a provided +content_type+ into a translation key suitable for
@@ -685,14 +687,14 @@ class MIME::Type
MEDIA_TYPE_RE.match(content_type)
end
- if matchdata
- matchdata.captures.map { |e|
- e.downcase!
- e.gsub!(UNREGISTERED_RE, ''.freeze)
- e.gsub!(I18N_RE, '-'.freeze)
- e
- }.join('.'.freeze)
- end
+ return unless matchdata
+
+ matchdata.captures.map { |e|
+ e.downcase!
+ e.gsub!(UNREGISTERED_RE, ''.freeze)
+ e.gsub!(I18N_RE, '-'.freeze)
+ e
+ }.join('.'.freeze)
end
# Creates a MIME::Type from an +args+ array in the form of:
@@ -721,7 +723,8 @@ class MIME::Type
args = args.first if args.first.kind_of? Array
unless args.size.between?(1, 8)
- raise ArgumentError, "Array provided must contain between one and eight elements."
+ fail ArgumentError,
+ 'Array provided must contain between one and eight elements.'
end
MIME::Type.new(args.shift) do |t|
@@ -801,9 +804,10 @@ class MIME::Type
end
private
+
def content_type=(type_string)
match = MEDIA_TYPE_RE.match(type_string)
- raise InvalidContentType, type_string if match.nil?
+ fail InvalidContentType, type_string if match.nil?
@content_type = type_string
@raw_media_type, @raw_sub_type = match.captures