mimer_plus 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,6 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mimer (0.0.2)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.2)
10
+ rspec (2.4.0)
11
+ rspec-core (~> 2.4.0)
12
+ rspec-expectations (~> 2.4.0)
13
+ rspec-mocks (~> 2.4.0)
14
+ rspec-core (2.4.0)
15
+ rspec-expectations (2.4.0)
16
+ diff-lcs (~> 1.1.2)
17
+ rspec-mocks (2.4.0)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ mimer!
24
+ rspec
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.3
@@ -0,0 +1,56 @@
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
+ attr_accessor :mime_type
20
+
21
+ # Create a new Mimer object for the specified file.
22
+ def self.identify(filename)
23
+ return !File.exists?(filename) ? nil : Mimer.new(filename)
24
+ end
25
+
26
+ # Find the mime type for +filename+
27
+ def initialize(filename)
28
+ @filename = filename; identify!
29
+ end
30
+
31
+ # Suggests an extention to use like .jpg or .png
32
+ def suggested_extension
33
+ case(mime_type)
34
+ when /^image\/jpeg/
35
+ '.jpg'
36
+ when /^image\/gif/
37
+ '.gif'
38
+ when /^image\/png/
39
+ '.png'
40
+ else
41
+ nil
42
+ end
43
+ end
44
+
45
+ # Responds to every method that ends with a question mark that isn't implemented in {Mimer}
46
+ def method_missing(m, *args, &block)
47
+ return mime_type.match (/^#{m.to_s.gsub(/\?$/, '')}\/.*/i) if m.to_s.match(/\?$/)
48
+ super(m, *args, &block)
49
+ end
50
+
51
+ private
52
+ # Identifies the file and stores the result in +@mime_type+
53
+ def identify!
54
+ @mime_type = `/usr/bin/env file --brief --mime #{@filename.gsub(/\s+/, '\ ')}`.strip
55
+ end
56
+ end
@@ -0,0 +1,18 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "mimer_plus"
5
+ s.version = "0.0.1"
6
+ s.platform = Gem::Platform::RUBY
7
+ s.authors = ["Linus Oleander", "Ariejan de Vroom"]
8
+ s.email = ["linus@oleander.nu", "ariejan@ariejan.net"]
9
+ s.homepage = "https://github.com/oleander/Undertexter"
10
+ s.summary = %q{Find the mime-type of a file using unix' `file` command. This does not look at file extension, ever.}
11
+ s.description = %q{Find the mime-type of a file using unix' `file` command. This does not look at file extension, ever...}
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.require_paths = ["lib"]
17
+ s.add_development_dependency('rspec')
18
+ end
Binary file
Binary file
@@ -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,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Mimer" do
4
+ it "should create a new Mimer instance with identify" do
5
+ m = Mimer.identify(fixture_path('plain.txt'))
6
+ m.should_not be_nil
7
+ m.should be_a(Mimer)
8
+ end
9
+
10
+ it "should return nil if the file cannot be found" do
11
+ m = Mimer.identify(fixture_path('does_not.exist'))
12
+ m.should be_nil
13
+ end
14
+
15
+ describe "text" do
16
+
17
+ before(:each) do
18
+ @mimer = Mimer.identify(fixture_path('plain.txt'))
19
+ end
20
+
21
+ it "should identify mime correctly" do
22
+ @mimer.mime_type.should match(/text\/plain/)
23
+ end
24
+
25
+ it "should find this text" do
26
+ @mimer.should be_text
27
+ end
28
+ end
29
+
30
+ describe "image extensions" do
31
+ it "should work for jpeg" do
32
+ mimer = Mimer.identify(fixture_path('facepalm.jpg'))
33
+ mimer.suggested_extension.should eql(".jpg")
34
+ end
35
+
36
+ it "should work for gif" do
37
+ mimer = Mimer.identify(fixture_path('google.gif'))
38
+ mimer.suggested_extension.should eql(".gif")
39
+ end
40
+
41
+ it "should work for png" do
42
+ mimer = Mimer.identify(fixture_path('kirk.png'))
43
+ mimer.suggested_extension.should eql(".png")
44
+ end
45
+ end
46
+
47
+ describe "image" do
48
+
49
+ before(:each) do
50
+ @mimer = Mimer.identify(fixture_path('facepalm.jpg'))
51
+ end
52
+
53
+ it "should identify mime correctly" do
54
+ @mimer.mime_type.should match(/image\/jpeg/)
55
+ end
56
+
57
+ it "should find this image" do
58
+ @mimer.should be_image
59
+ end
60
+ end
61
+ end
62
+
63
+ describe Mimer, "should have a missing method" do
64
+ before(:each) do
65
+ @mimer = Mimer.identify(fixture_path('facepalm.jpg'))
66
+ end
67
+
68
+ it "should raise an exception if a method isn't implemented" do
69
+ lambda {
70
+ @mimer.some_strange_method
71
+ }.should raise_error(NoMethodError)
72
+ end
73
+
74
+ it "should not raise an exception when trying to get the get the mime type" do
75
+ lambda {
76
+ @mimer.some_strange_mimetype?
77
+ }.should_not raise_error(NoMethodError)
78
+ end
79
+ end
@@ -0,0 +1,10 @@
1
+ require "#{File.dirname(__FILE__)}/../lib/mimer_plus"
2
+
3
+ RSpec.configure do |config|
4
+
5
+ end
6
+
7
+ # Get a filename to a fixture file
8
+ def fixture_path(filename)
9
+ File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename)
10
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mimer_plus
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Linus Oleander
13
+ - Ariejan de Vroom
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-28 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Find the mime-type of a file using unix' `file` command. This does not look at file extension, ever...
35
+ email:
36
+ - linus@oleander.nu
37
+ - ariejan@ariejan.net
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - Gemfile
48
+ - Gemfile.lock
49
+ - LICENSE
50
+ - README.rdoc
51
+ - Rakefile
52
+ - VERSION
53
+ - lib/mimer_plus.rb
54
+ - mimer_plus.gemspec
55
+ - spec/fixtures/facepalm.jpg
56
+ - spec/fixtures/google.gif
57
+ - spec/fixtures/kirk.png
58
+ - spec/fixtures/plain.txt
59
+ - spec/mimer_plus_spec.rb
60
+ - spec/spec_helper.rb
61
+ has_rdoc: true
62
+ homepage: https://github.com/oleander/Undertexter
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options: []
67
+
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ requirements: []
87
+
88
+ rubyforge_project:
89
+ rubygems_version: 1.3.7
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Find the mime-type of a file using unix' `file` command. This does not look at file extension, ever.
93
+ test_files:
94
+ - spec/fixtures/facepalm.jpg
95
+ - spec/fixtures/google.gif
96
+ - spec/fixtures/kirk.png
97
+ - spec/fixtures/plain.txt
98
+ - spec/mimer_plus_spec.rb
99
+ - spec/spec_helper.rb