Commit 9d678423 by minad

version 0.1.4

parent 09cb30d1
......@@ -3,7 +3,7 @@ require 'stringio'
# Mime type detection
class MimeMagic
VERSION = '0.1.2'
VERSION = '0.1.4'
attr_reader :type, :mediatype, :subtype
......@@ -23,15 +23,13 @@ class MimeMagic
# * <i>:parents</i>: String list or single string of parent mime types
# * <i>:magic</i>: Mime magic specification
# * <i>:comment</i>: Comment string
def self.add(type, options = {})
def self.add(type, options)
extensions = [options[:extensions]].flatten.compact
TYPES[type] = [extensions,
[options[:parents]].flatten.compact,
options[:comment]]
extensions.each do |ext|
EXTENSIONS[ext] = type
end
MAGIC.unshift [type, [options[:magic]].flatten.compact] if options[:magic]
extensions.each {|ext| EXTENSIONS[ext] = type }
MAGIC.unshift [type, options[:magic]] if options[:magic]
end
# Returns true if type is a text format
......
......@@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = %q{mimemagic}
s.version = "0.1.2"
s.version = "0.1.4"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Daniel Mendler"]
......
......@@ -28,6 +28,7 @@ describe 'MimeMagic' do
end
it 'should recognize extensions' do
MimeMagic.by_extension('.html').to_s.should.equal 'text/html'
MimeMagic.by_extension('html').to_s.should.equal 'text/html'
MimeMagic.by_extension(:html).to_s.should.equal 'text/html'
MimeMagic.by_extension('rb').to_s.should.equal 'application/x-ruby'
......@@ -44,11 +45,28 @@ describe 'MimeMagic' do
end
it 'should have add' do
MimeMagic.add('application/mimemagic-test', :extensions => %w(ext1 ext2), :parents => 'application/xml', :comment => 'Comment')
MimeMagic.add('application/mimemagic-test',
:extensions => %w(ext1 ext2),
:parents => 'application/xml',
:comment => 'Comment')
MimeMagic.by_extension('ext1').to_s.should.equal 'application/mimemagic-test'
MimeMagic.by_extension('ext2').to_s.should.equal 'application/mimemagic-test'
MimeMagic.by_extension('ext2').comment.should.equal 'Comment'
MimeMagic.new('application/mimemagic-test').extensions.should.equal %w(ext1 ext2)
MimeMagic.new('application/mimemagic-test').should.be.child_of 'text/plain'
end
it 'should process magic' do
MimeMagic.add('application/mimemagic-test',
:magic => [[0, 'MAGICTEST'], # MAGICTEST at position 0
[1, 'MAGICTEST'], # MAGICTEST at position 1
[2, 'MAGICTEST', [[0, 'X'], [0, 'Y']]]]) # MAGICTEST at position 2 and (X at 0 or Y at 0)
MimeMagic.by_magic('MAGICTEST').to_s.should.equal 'application/mimemagic-test'
MimeMagic.by_magic('XMAGICTEST').to_s.should.equal 'application/mimemagic-test'
MimeMagic.by_magic(' MAGICTEST').to_s.should.equal 'application/mimemagic-test'
MimeMagic.by_magic('X MAGICTEST').to_s.should.equal 'application/mimemagic-test'
MimeMagic.by_magic('Y MAGICTEST').to_s.should.equal 'application/mimemagic-test'
MimeMagic.by_magic('Z MAGICTEST').should.equal nil
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment