Skip to content

Commit

Permalink
IANA Provisional Registration Support
Browse files Browse the repository at this point in the history
- Added a new field to `MIME::Type` for checking provisional registrations
  from IANA.
  • Loading branch information
halostatue committed Nov 15, 2021
1 parent 6c8324d commit f147050
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# NOTE: This file is present to keep Travis CI happy. Edits to it will not
# be accepted.
# NOTE: This file is not the canonical source of dependencies. Edit the
# Rakefile, instead.

source "https://rubygems.org/"

Expand Down
7 changes: 6 additions & 1 deletion History.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

## NEXT / 2021-MM-DD
## 3.4.0 / 2021-06-02

- 1 minor enhancement:

- Added a new field to `MIME::Type` for checking provisional registrations
from IANA.

- Documentation:

Expand Down
1 change: 1 addition & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ files). A MIME::Type stores the known information about one MIME type.
puts text.ascii? # => true
puts text.obsolete? # => false
puts text.registered? # => true
puts text.provisional? # => false
puts text.complete? # => true

puts text # => 'text/plain'
Expand Down
3 changes: 1 addition & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ spec = Hoe.spec "mime-types" do
extra_dev_deps << ["minitest-bonus-assertions", "~> 3.0"]
extra_dev_deps << ["minitest-hooks", "~> 1.4"]
extra_dev_deps << ["rake", ">= 10.0", "< 14.0"]
extra_dev_deps << ["psych", "~> 3.0"]

if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.0")
extra_dev_deps << ["simplecov", "~> 0.7"]
Expand Down Expand Up @@ -249,7 +248,7 @@ task "deps:top", [:number] do |_, args|
end

task :console do
arguments = %w[pry]
arguments = %w[irb]
arguments.push(*spec.spec.require_paths.map { |dir| "-I#{dir}" })
arguments.push("-r#{spec.spec.name.gsub("-", File::SEPARATOR)}")
unless system(*arguments)
Expand Down
15 changes: 13 additions & 2 deletions lib/mime/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module MIME
# puts text.ascii? # => true
# puts text.obsolete? # => false
# puts text.registered? # => true
# puts text.provisional? # => false
# puts text.complete? # => true
#
# puts text # => 'text/plain'
Expand Down Expand Up @@ -92,7 +93,7 @@ def to_s
end

# The released version of the mime-types library.
VERSION = "3.3.1"
VERSION = "3.4.0"

include Comparable

Expand Down Expand Up @@ -123,7 +124,7 @@ def to_s
# Yields the newly constructed +self+ object.
def initialize(content_type) # :yields: self
@friendly = {}
@obsolete = @registered = false
@obsolete = @registered = @provisional = false
@preferred_extension = @docs = @use_instead = nil
self.extensions = []

Expand Down Expand Up @@ -419,6 +420,14 @@ def xref_urls
attr_accessor :registered
alias_method :registered?, :registered

# Indicates whether the MIME type's registration with IANA is provisional.
attr_accessor :provisional

# Indicates whether the MIME type's registration with IANA is provisional.
def provisional?
registered? && @provisional
end

# MIME types can be specified to be sent across a network in particular
# formats. This method returns +true+ when the MIME::Type encoding is set
# to <tt>base64</tt>.
Expand Down Expand Up @@ -493,6 +502,7 @@ def encode_with(coder)
end
end
coder["registered"] = registered?
coder["provisional"] = provisional? if provisional?
coder["signature"] = signature? if signature?
coder
end
Expand All @@ -509,6 +519,7 @@ def init_with(coder)
self.preferred_extension = coder["preferred-extension"]
self.obsolete = coder["obsolete"] || false
self.registered = coder["registered"] || false
self.provisional = coder["provisional"] || false
self.signature = coder["signature"]
self.xrefs = coder["xrefs"] || {}
self.use_instead = coder["use-instead"]
Expand Down
4 changes: 2 additions & 2 deletions lib/mime/type/columnar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def self.column(*methods, file: nil) # :nodoc:
column :encoding, :encoding=
column :docs, :docs=
column :preferred_extension, :preferred_extension=
column :obsolete, :obsolete=, :obsolete?, :registered, :registered=,
:registered?, :signature, :signature=, :signature?, file: "flags"
column :obsolete, :obsolete=, :obsolete?, :registered, :registered=, :registered?, :signature, :signature=,
:signature?, :provisional, :provisional=, :provisional?, file: "flags"
column :xrefs, :xrefs=, :xref_urls
column :use_instead, :use_instead=

Expand Down
1 change: 1 addition & 0 deletions lib/mime/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Types
# puts plaintext.ascii? # => true
# puts plaintext.obsolete? # => false
# puts plaintext.registered? # => true
# puts plaintext.provisional? # => false
# puts plaintext == 'text/plain' # => true
# puts MIME::Type.simplified('x-appl/x-zip') # => 'appl/zip'
#
Expand Down
1 change: 1 addition & 0 deletions lib/mime/types/_columnar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def load_flags
type.instance_variable_set(:@obsolete, flag(line.shift))
type.instance_variable_set(:@registered, flag(line.shift))
type.instance_variable_set(:@signature, flag(line.shift))
type.instance_variable_set(:@provisional, flag(line.shift))
end
end

Expand Down
2 changes: 0 additions & 2 deletions lib/mime/types/loader.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

# -*- ruby encoding: utf-8 -*-

##
module MIME; end

Expand Down
2 changes: 0 additions & 2 deletions lib/mime/types/logger.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

# -*- ruby encoding: utf-8 -*-

require "logger"

##
Expand Down
4 changes: 2 additions & 2 deletions mime-types.gemspec
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# -*- encoding: utf-8 -*-
# stub: mime-types 3.3.1 ruby lib
# stub: mime-types 3.4.0 ruby lib

Gem::Specification.new do |s|
s.name = "mime-types".freeze
s.version = "3.3.1"
s.version = "3.4.0"

s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
s.metadata = { "bug_tracker_uri" => "https://github.com/mime-types/ruby-mime-types/issues", "changelog_uri" => "https://github.com/mime-types/ruby-mime-types/blob/master/History.md", "homepage_uri" => "https://github.com/mime-types/ruby-mime-types/", "source_code_uri" => "https://github.com/mime-types/ruby-mime-types/" } if s.respond_to? :metadata=
Expand Down
15 changes: 13 additions & 2 deletions test/test_mime_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,23 @@ def assert_priority(left, middle, right)
end

describe "#to_json" do
let(:expected) {
let(:expected_1) {
'{"content-type":"a/b","encoding":"base64","registered":false}'
}
let(:expected_2) {
'{"content-type":"a/b","encoding":"base64","registered":true,"provisional":true}'
}

it "converts to JSON when requested" do
assert_equal expected, mime_type("a/b").to_json
assert_equal expected_1, mime_type("a/b").to_json
end

it "converts to JSON with provisional when requested" do
type = mime_type("a/b") do |t|
t.registered = true
t.provisional = true
end
assert_equal expected_2, type.to_json
end
end

Expand Down

0 comments on commit f147050

Please sign in to comment.