mimer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ariejan de Vroom
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ = Mimer
2
+
3
+ Mimer tries to find a file's mime-type by using unix' `file` command. File extensions are never used to identify a file.
4
+
5
+ Mimer has been tested to work on Debian 5.0 and Mac OS X 10.5+. This gem is useless on Windows.
6
+
7
+ == Install
8
+
9
+ gem install mimer
10
+
11
+ You may need to install the gemcutter gem first.
12
+
13
+ == Usage
14
+
15
+ It's quite easy:
16
+
17
+ mimer = Mimer.identify('/tmp/testfile')
18
+
19
+ mimer.mime_type
20
+ => "image/jpeg; charset=binary"
21
+
22
+ mimer.text?
23
+ => false
24
+
25
+ mimer.image?
26
+ => true
27
+
28
+ == Get the code
29
+
30
+ http://github.com/ariejan/mimer
31
+
32
+ == Note on Patches/Pull Requests
33
+
34
+ * Fork the project.
35
+ * Make your feature addition or bug fix.
36
+ * Add tests for it. This is important so I don't break it in a
37
+ future version unintentionally.
38
+ * Commit, do not mess with rakefile, version, or history.
39
+ (if you want to have your own version, that is fine but
40
+ bump version in a commit by itself I can ignore when I pull)
41
+ * Send me a pull request. Bonus points for topic branches.
42
+
43
+ == Copyright
44
+
45
+ Copyright (c) 2009 Ariejan de Vroom, Kabisa ICT. See LICENSE for details.
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "mimer"
8
+ gem.summary = %Q{Find the mime-type of a file using unix' `file` command.}
9
+ gem.description = %Q{Find the mime-type of a file using unix' `file` command. This does not look at file extension, ever.}
10
+ gem.email = "ariejan@ariejan.net"
11
+ gem.homepage = "http://github.com/ariejan/mimer"
12
+ gem.authors = ["Ariejan de Vroom"]
13
+ gem.rubyforge_project = "mimer"
14
+ gem.add_development_dependency "rspec"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ if File.exist?('VERSION')
41
+ version = File.read('VERSION')
42
+ else
43
+ version = ""
44
+ end
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "mimer #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,64 @@
1
+ # Mimer find a file's mime-type using unix' `file` command. It will never
2
+ # look at a file's extension.
3
+ #
4
+ # Basic usage:
5
+ #
6
+ # mimer = Mimer.identify('/tmp/testfile')
7
+ # => #<Mimer:0x9f @filename="/tmp/testfile", @mime_type="image/jpeg; charset=binary">
8
+ #
9
+ # mimer.mime_type
10
+ # => "image/jpeg; charset=binary"
11
+ #
12
+ # mimer.text?
13
+ # => false
14
+ #
15
+ # mimer.image?
16
+ # => true
17
+ #
18
+ class Mimer
19
+
20
+ # Create a new Mimer object for the specified file.
21
+ def self.identify(filename)
22
+ return nil if !File.exists?(filename)
23
+ return Mimer.new(filename)
24
+ end
25
+
26
+ # Find the mime type for +filename+
27
+ def initialize(filename)
28
+ @filename = filename
29
+ identify
30
+ end
31
+
32
+ # Returns the mime-type in string form.
33
+ def mime_type
34
+ @mime_type
35
+ end
36
+
37
+ # Returns true if the file is a text file.
38
+ def text?
39
+ mime_type.match /^text\/.*/i
40
+ end
41
+
42
+ # Returns true if the file is an image file.
43
+ def image?
44
+ mime_type.match /^image\/.*/i
45
+ end
46
+
47
+ # Returns true if the file is a video file.
48
+ def video?
49
+ mime_type.match /^video\/.*/i
50
+ end
51
+
52
+ # Returns true if the file is an audio file.
53
+ def audio?
54
+ mime_type.match /^audio\/.*/i
55
+ end
56
+
57
+ private
58
+
59
+ # Identifies the file and stores the result in +@mime_type+
60
+ def identify
61
+ @mime_type = `/usr/bin/env file --brief --mime #{@filename}`.strip
62
+ end
63
+
64
+ end
@@ -0,0 +1,56 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mimer}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ariejan de Vroom"]
12
+ s.date = %q{2009-10-12}
13
+ s.description = %q{Find the mime-type of a file using unix' `file` command. This does not look at file extension, ever.}
14
+ s.email = %q{ariejan@ariejan.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/mimer.rb",
27
+ "mimer.gemspec",
28
+ "spec/fixtures/facepalm.jpg",
29
+ "spec/fixtures/plain.txt",
30
+ "spec/mimer_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/ariejan/mimer}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubyforge_project = %q{mimer}
37
+ s.rubygems_version = %q{1.3.5}
38
+ s.summary = %q{Find the mime-type of a file using unix' `file` command.}
39
+ s.test_files = [
40
+ "spec/mimer_spec.rb",
41
+ "spec/spec_helper.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<rspec>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<rspec>, [">= 0"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<rspec>, [">= 0"])
55
+ end
56
+ end
@@ -0,0 +1,6 @@
1
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
2
+ tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
3
+ quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
4
+ consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
5
+ cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
6
+ non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
@@ -0,0 +1,47 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Mimer" do
4
+
5
+ it "should create a new Mimer instance with identify" do
6
+ m = Mimer.identify(fixture_path('plain.txt'))
7
+
8
+ m.should_not be_nil
9
+ m.should be_a(Mimer)
10
+ end
11
+
12
+ it "should return nil if the file cannot be found" do
13
+ m = Mimer.identify(fixture_path('does_not.exist'))
14
+
15
+ m.should be_nil
16
+ end
17
+
18
+ describe "text" do
19
+
20
+ before(:each) do
21
+ @mimer = Mimer.identify(fixture_path('plain.txt'))
22
+ end
23
+
24
+ it "should identify mime correctly" do
25
+ @mimer.mime_type.should match(/text\/plain/)
26
+ end
27
+
28
+ it "should find this text" do
29
+ @mimer.should be_text
30
+ end
31
+ end
32
+
33
+ describe "image" do
34
+
35
+ before(:each) do
36
+ @mimer = Mimer.identify(fixture_path('facepalm.jpg'))
37
+ end
38
+
39
+ it "should identify mime correctly" do
40
+ @mimer.mime_type.should match(/image\/jpeg/)
41
+ end
42
+
43
+ it "should find this image" do
44
+ @mimer.should be_image
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'mimer'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
10
+
11
+ # Get a filename to a fixture file
12
+ def fixture_path(filename)
13
+ File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename)
14
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mimer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ariejan de Vroom
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-12 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Find the mime-type of a file using unix' `file` command. This does not look at file extension, ever.
26
+ email: ariejan@ariejan.net
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/mimer.rb
42
+ - mimer.gemspec
43
+ - spec/fixtures/facepalm.jpg
44
+ - spec/fixtures/plain.txt
45
+ - spec/mimer_spec.rb
46
+ - spec/spec_helper.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/ariejan/mimer
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project: mimer
71
+ rubygems_version: 1.3.5
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Find the mime-type of a file using unix' `file` command.
75
+ test_files:
76
+ - spec/mimer_spec.rb
77
+ - spec/spec_helper.rb