Skip to content

Commit

Permalink
version 0.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
minad committed May 28, 2010
1 parent 09cb30d commit 9d67842
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
10 changes: 4 additions & 6 deletions lib/mimemagic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Mime type detection
class MimeMagic
VERSION = '0.1.2'
VERSION = '0.1.4'

attr_reader :type, :mediatype, :subtype

Expand All @@ -23,15 +23,13 @@ def initialize(type)
# * <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
Expand Down
2 changes: 1 addition & 1 deletion mimemagic.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
20 changes: 19 additions & 1 deletion test/test_mimemagic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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'
Expand All @@ -44,11 +45,28 @@
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

0 comments on commit 9d67842

Please sign in to comment.