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 0c76e5c
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 34 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
17 changes: 11 additions & 6 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
# Changelog

## NEXT / 2021-MM-DD
## 3.4.0 / 2021-11-15

- 1 minor enhancement:

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

- Documentation:

- Kevin Menard synced the documentation so that all examples are correct
[#153].
- Kevin Menard synced the documentation so that all examples are correct.
[#153]

- Administrivia:

- Added Ruby 3.0 to the CI test matrix. Added `windows/jruby` to the
CI exclusion list; it refuses to run successfully.
- Removed the Travis CI configuration and changed it to Github Workflows
[#150]. Removed Coveralls configuration.
- Igor Victor added TruffleRuby to the Travis CI configuration [#149].
- Koichi ITO loosened an excessively tight dependency [#147].
- Igor Victor added TruffleRuby to the Travis CI configuration. [#149]
- Koichi ITO loosened an excessively tight dependency. [#147]
- Started using `standardrb` for Ruby formatting and validation.

## 3.3.1 / 2019-12-26
Expand Down Expand Up @@ -76,7 +81,7 @@
because when Enumerable#inject isn't provided a starting value, the first
value is used as the default value. In every case where this error was
happening, the result was supposed to be an array containing Set objects
so they can be reduced to a single Set. [#117], [#127], [#134].
so they can be reduced to a single Set. [#117], [#127], [#134]

- Fixed an uncontrolled growth bug in MIME::Types::Container where a key
miss would create a new entry with an empty Set in the container. This
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
36 changes: 29 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ require "rubygems"
require "hoe"
require "rake/clean"

# This is required until https://github.com/seattlerb/hoe/issues/112 is fixed
class Hoe
def with_config
config = Hoe::DEFAULT_CONFIG

homeconfig = load_config(File.expand_path("~/.hoerc"))
config = config.merge(homeconfig)

localconfig = load_config(File.expand_path(File.join(Dir.pwd, ".hoerc")))
config = config.merge(localconfig)

yield config, rc
end

def load_config(name)
File.exist? name ? safe_load_yaml(name) : {}
end

def safe_load_yaml(name)
YAML.safe_load_file(name, permitted_classes: [Regexp])
rescue
YAML.safe_load_file(name, [Regexp])
end
end

Hoe.plugin :doofus
Hoe.plugin :gemspec2
Hoe.plugin :git
Expand Down Expand Up @@ -34,7 +59,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 @@ -234,11 +258,9 @@ task "deps:top", [:number] do |_, args|

weighted_results = {}
results.each do |name|
begin
weighted_results[name] = rubygems_get(gem_name: name)["downloads"]
rescue => e
puts "#{name} #{e.message}"
end
weighted_results[name] = rubygems_get(gem_name: name)["downloads"]
rescue => e
puts "#{name} #{e.message}"
end

weighted_results.sort { |(_k1, v1), (_k2, v2)|
Expand All @@ -249,7 +271,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
3 changes: 2 additions & 1 deletion lib/mime/types/_columnar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def self.extended(obj) # :nodoc:
end

# Load the first column data file (type and extensions).
def load_base_data(path) #:nodoc:
def load_base_data(path) # :nodoc:
@__root__ = path

each_file_line("content_type", false) do |line|
Expand Down 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: 1 addition & 1 deletion lib/mime/types/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# format (plus, a default of a mutable object resuls in a shared mess).
# Hash#default_proc cannot be used without a wrapper because it prevents
# Marshal serialization (and doesn't survive the round-trip).
class MIME::Types::Container #:nodoc:
class MIME::Types::Container # :nodoc:
extend Forwardable

def initialize(hash = {})
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
6 changes: 2 additions & 4 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 All @@ -14,8 +12,8 @@ class << self
attr_accessor :logger
end

class WarnLogger < ::Logger #:nodoc:
class WarnLogDevice < ::Logger::LogDevice #:nodoc:
class WarnLogger < ::Logger # :nodoc:
class WarnLogDevice < ::Logger::LogDevice # :nodoc:
def initialize(*)
end

Expand Down
8 changes: 3 additions & 5 deletions mime-types.gemspec
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# -*- 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=
s.require_paths = ["lib".freeze]
s.authors = ["Austin Ziegler".freeze]
s.date = "2021-06-02"
s.date = "2021-11-15"
s.description = "The mime-types library provides a library and registry for information about\nMIME content type definitions. It can be used to determine defined filename\nextensions for MIME types, or to use filename extensions to look up the likely\nMIME type definitions.\n\nVersion 3.0 is a major release that requires Ruby 2.0 compatibility and removes\ndeprecated functions. The columnar registry format introduced in 2.6 has been\nmade the primary format; the registry data has been extracted from this library\nand put into {mime-types-data}[https://github.com/mime-types/mime-types-data].\nAdditionally, mime-types is now licensed exclusively under the MIT licence and\nthere is a code of conduct in effect. There are a number of other smaller\nchanges described in the History file.".freeze
s.email = ["halostatue@gmail.com".freeze]
s.extra_rdoc_files = ["Code-of-Conduct.md".freeze, "Contributing.md".freeze, "History.md".freeze, "Licence.md".freeze, "Manifest.txt".freeze, "README.rdoc".freeze]
Expand Down Expand Up @@ -38,7 +38,6 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<minitest-bonus-assertions>.freeze, ["~> 3.0"])
s.add_development_dependency(%q<minitest-hooks>.freeze, ["~> 1.4"])
s.add_development_dependency(%q<rake>.freeze, [">= 10.0", "< 14.0"])
s.add_development_dependency(%q<psych>.freeze, ["~> 3.0"])
s.add_development_dependency(%q<simplecov>.freeze, ["~> 0.7"])
s.add_development_dependency(%q<rdoc>.freeze, [">= 4.0", "< 7"])
s.add_development_dependency(%q<hoe>.freeze, ["~> 3.23"])
Expand All @@ -55,7 +54,6 @@ Gem::Specification.new do |s|
s.add_dependency(%q<minitest-bonus-assertions>.freeze, ["~> 3.0"])
s.add_dependency(%q<minitest-hooks>.freeze, ["~> 1.4"])
s.add_dependency(%q<rake>.freeze, [">= 10.0", "< 14.0"])
s.add_dependency(%q<psych>.freeze, ["~> 3.0"])
s.add_dependency(%q<simplecov>.freeze, ["~> 0.7"])
s.add_dependency(%q<rdoc>.freeze, [">= 4.0", "< 7"])
s.add_dependency(%q<hoe>.freeze, ["~> 3.23"])
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 0c76e5c

Please sign in to comment.