multi_mime 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.document +3 -0
- data/.gitignore +19 -0
- data/.rspec +3 -0
- data/.travis.yml +17 -0
- data/.yardopts +6 -0
- data/CHANGELOG.md +0 -0
- data/CONTRIBUTING.md +46 -0
- data/Gemfile +34 -0
- data/LICENSE.md +22 -0
- data/README.md +68 -0
- data/Rakefile +27 -0
- data/lib/multi_mime.rb +142 -0
- data/lib/multi_mime/adapter.rb +30 -0
- data/lib/multi_mime/adapters/mime_type.rb +35 -0
- data/lib/multi_mime/adapters/mime_types.rb +34 -0
- data/lib/multi_mime/adapters/mimemagic.rb +35 -0
- data/lib/multi_mime/adapters/rack_mime.rb +36 -0
- data/lib/multi_mime/null_type.rb +17 -0
- data/lib/multi_mime/version.rb +3 -0
- data/multi_mime.gemspec +20 -0
- data/spec/shared/adapter.rb +101 -0
- data/spec/spec_helper.rb +34 -0
- data/spec/support/fakefs.rb +6 -0
- data/spec/support/pry.rb +3 -0
- data/spec/support/simplecov.rb +15 -0
- data/spec/unit/multi_mime_spec.rb +111 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
M2VlZmY3YTAyZmVhMjJmMDQ0YzEyNzE1YzU3MjAyN2Y4NjY3NjM4Nw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ODI2MmFkMzNlMzVjZDY5MGI4YjA2ZTY0ZTQyZDMxMzk5ZTc0ZWQ3OQ==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YzE5YmNkMzNhZmU4N2Y1MTcwZGM4MTcyNzYzMDljNzZiMzk5MWEzMGE0NjYx
|
10
|
+
MjBkNzkzN2E4MWEwMmMxOGJiNmYxZjkyZjAwZWMyMDM3NDFlZmM2ZmMwMjg3
|
11
|
+
NjY1ZjcxOTdkZWNhZDBkZjQ5MDE1Y2E3MTg3ZjYzYTRjOWVkMzM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NjRmOThkMTA1MzkzN2EwODgwMjA0NTJhNWNlYTU4OWZkMzEwMWRiZjJkNWU1
|
14
|
+
MWExZDA2Y2U2MzNjMDc0MjE4YzU1OTQ0MmJhZDNmOTg0NmEzZWJhN2NhNDNm
|
15
|
+
NDZiOGYxYzFiMGNkZTEwZThhZTQ5ZjQ0MGJlZGViNmViN2Y4MTQ=
|
data/.document
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
bundler_args: --without development
|
2
|
+
language: ruby
|
3
|
+
rvm:
|
4
|
+
- rbx-18mode
|
5
|
+
- rbx-19mode
|
6
|
+
- jruby-18mode
|
7
|
+
- jruby-19mode
|
8
|
+
- 1.8.7
|
9
|
+
- 1.9.2
|
10
|
+
- 1.9.3
|
11
|
+
- 2.0.0
|
12
|
+
matrix:
|
13
|
+
allow_failures:
|
14
|
+
- rvm: rbx-18mode
|
15
|
+
- rvm: jruby-18mode
|
16
|
+
- rvm: 1.8.7
|
17
|
+
- rvm: rbx-19mode
|
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
File without changes
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
## Contributing
|
2
|
+
In the spirit of [free software][free-sw], **everyone** is encouraged to help
|
3
|
+
improve this project.
|
4
|
+
|
5
|
+
[free-sw]: http://www.fsf.org/licensing/essays/free-sw.html
|
6
|
+
|
7
|
+
Here are some ways *you* can contribute:
|
8
|
+
|
9
|
+
* by using alpha, beta, and prerelease versions
|
10
|
+
* by reporting bugs
|
11
|
+
* by suggesting new features
|
12
|
+
* by writing or editing documentation
|
13
|
+
* by writing specifications
|
14
|
+
* by writing code (**no patch is too small**: fix typos, add comments, clean up
|
15
|
+
inconsistent whitespace)
|
16
|
+
* by refactoring code
|
17
|
+
* by closing [issues][]
|
18
|
+
* by reviewing patches
|
19
|
+
|
20
|
+
[issues]: https://github.com/karlfreeman/multi_mime/issues
|
21
|
+
|
22
|
+
## Submitting an Issue
|
23
|
+
We use the [GitHub issue tracker][issues] to track bugs and features. Before
|
24
|
+
submitting a bug report or feature request, check to make sure it hasn't
|
25
|
+
already been submitted. When submitting a bug report, please include a [Gist][]
|
26
|
+
that includes a stack trace and any details that may be necessary to reproduce
|
27
|
+
the bug, including your gem version, Ruby version, and operating system.
|
28
|
+
Ideally, a bug report should include a pull request with failing specs.
|
29
|
+
|
30
|
+
[gist]: https://gist.github.com/
|
31
|
+
|
32
|
+
## Submitting a Pull Request
|
33
|
+
1. [Fork the repository.][fork]
|
34
|
+
2. [Create a topic branch.][branch]
|
35
|
+
3. Add specs for your unimplemented feature or bug fix.
|
36
|
+
4. Run `bundle exec rake spec`. If your specs pass, return to step 3.
|
37
|
+
5. Implement your feature or bug fix.
|
38
|
+
6. Run `bundle exec rake spec`. If your specs fail, return to step 5.
|
39
|
+
7. Run `open coverage/index.html`. If your changes are not completely covered
|
40
|
+
by your tests, return to step 3.
|
41
|
+
8. Add, commit, and push your changes.
|
42
|
+
9. [Submit a pull request.][pr]
|
43
|
+
|
44
|
+
[fork]: http://help.github.com/fork-a-repo/
|
45
|
+
[branch]: http://learn.github.com/p/branching.html
|
46
|
+
[pr]: http://help.github.com/send-pull-requests/
|
data/Gemfile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
gem "rake", ">= 1.2"
|
4
|
+
gem "yard"
|
5
|
+
|
6
|
+
# platforms :ruby_18 do
|
7
|
+
# end
|
8
|
+
# platforms :ruby, :mswin, :mingw do
|
9
|
+
# end
|
10
|
+
# platforms :jruby do
|
11
|
+
# end
|
12
|
+
|
13
|
+
gem "mime-types", :require => false
|
14
|
+
gem "actionpack", ">= 3.2", :require => false
|
15
|
+
gem "rack", :require => false
|
16
|
+
gem "mimemagic", :require => false
|
17
|
+
|
18
|
+
group :development do
|
19
|
+
gem "kramdown", ">= 0.14"
|
20
|
+
gem "pry"
|
21
|
+
gem "pry-debugger", :platforms => :mri_19
|
22
|
+
gem "awesome_print"
|
23
|
+
end
|
24
|
+
|
25
|
+
group :test do
|
26
|
+
gem "rspec"
|
27
|
+
gem "rspec-smart-formatter"
|
28
|
+
gem "fakefs", :git => "https://github.com/defunkt/fakefs.git", :require => "fakefs/safe"
|
29
|
+
gem "simplecov", :require => false
|
30
|
+
gem "coveralls", :require => false
|
31
|
+
gem "cane", :require => false, :platforms => :ruby_19
|
32
|
+
end
|
33
|
+
|
34
|
+
gemspec
|
data/LICENSE.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Karl Freeman
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# MultiMime
|
2
|
+
|
3
|
+
A generic swappable back-end for Mime Type detection
|
4
|
+
|
5
|
+
Lots of Ruby libraries utilize Mime Type detection in some form. In order to best support multiple Mime Type detection libraries, `multi_mime` is a general-purpose swappable Mime Type detection backend library.
|
6
|
+
|
7
|
+
## Features / Usage Examples
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
require "multi_mime"
|
11
|
+
|
12
|
+
MultiMime.type_for("text/html") # "text/html"
|
13
|
+
# alias :by_type
|
14
|
+
|
15
|
+
MultiMime.type_for_extension(".html") # "text/html"
|
16
|
+
# alias :by_extension
|
17
|
+
|
18
|
+
MultiMime.type_for_path("/usr/local/foo/bar/foo.html") # "text/html"
|
19
|
+
# alias :by_path
|
20
|
+
|
21
|
+
MultiMime.type_for_file( File.open("foo.html", "w") ) # "text/html"
|
22
|
+
# alias :by_file
|
23
|
+
```
|
24
|
+
|
25
|
+
## Build & Dependency Status
|
26
|
+
|
27
|
+
[![Gem Version](https://badge.fury.io/rb/multi_mime.png)][gem]
|
28
|
+
[![Build Status](https://travis-ci.org/karlfreeman/multi_mime.png)][travis]
|
29
|
+
[![Dependency Status](https://gemnasium.com/karlfreeman/multi_mime.png?travis)][gemnasium]
|
30
|
+
[![Code Quality](https://codeclimate.com/github/karlfreeman/multi_mime.png)][codeclimate]
|
31
|
+
[![Coverage Status](https://coveralls.io/repos/karlfreeman/multi_mime/badge.png?branch=master)][coveralls]
|
32
|
+
|
33
|
+
## Supported Mime Engines
|
34
|
+
|
35
|
+
* [MIME::Types](https://github.com/halostatue/mime-types)
|
36
|
+
* [MagicMime](https://github.com/minad/mimemagic)
|
37
|
+
* [Rails](http://api.rubyonrails.org/classes/Mime/Type.html) ( ActionDispatch::Http::Mime )
|
38
|
+
* [Rack](http://rack.rubyforge.org/doc/Rack/Mime.html) ( Rack::Mime )
|
39
|
+
|
40
|
+
## Supported Ruby Versions
|
41
|
+
This library aims to support and is [tested against][travis] the following Ruby
|
42
|
+
implementations:
|
43
|
+
|
44
|
+
* Ruby 1.9.2
|
45
|
+
* Ruby 1.9.3
|
46
|
+
* Ruby 2.0.0
|
47
|
+
* [JRuby][]
|
48
|
+
* [Rubinius][]
|
49
|
+
|
50
|
+
# Credits
|
51
|
+
|
52
|
+
Inspiration:
|
53
|
+
|
54
|
+
* [MultiJSON](https://github.com/intridea/multi_json)
|
55
|
+
* [MultiXML](https://github.com/sferik/multi_xml)
|
56
|
+
|
57
|
+
Cribbed:
|
58
|
+
|
59
|
+
* [MultiJSON](https://github.com/intridea/multi_json)
|
60
|
+
* [Asset_Sync"s MultiMime module](https://github.com/rumblelabs/asset_sync/commit/9333bd01ae1a7cf2ffa046b8390fbc4165c38030)
|
61
|
+
|
62
|
+
[gem]: https://rubygems.org/gems/multi_mime
|
63
|
+
[travis]: http://travis-ci.org/karlfreeman/multi_mime
|
64
|
+
[gemnasium]: https://gemnasium.com/karlfreeman/multi_mime
|
65
|
+
[coveralls]: https://coveralls.io/r/karlfreeman/multi_mime
|
66
|
+
[codeclimate]: https://codeclimate.com/github/karlfreeman/multi_mime
|
67
|
+
[jruby]: http://www.jruby.org
|
68
|
+
[rubinius]: http://rubini.us
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "bundler"
|
2
|
+
Bundler.setup
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
|
+
|
5
|
+
require "yard"
|
6
|
+
YARD::Rake::YardocTask.new
|
7
|
+
|
8
|
+
require "rspec/core/rake_task"
|
9
|
+
desc "Run all examples"
|
10
|
+
RSpec::Core::RakeTask.new(:spec)
|
11
|
+
|
12
|
+
begin
|
13
|
+
require "cane/rake_task"
|
14
|
+
namespace :metric do
|
15
|
+
desc "Analyze for code quality"
|
16
|
+
Cane::RakeTask.new(:quality) do |cane|
|
17
|
+
cane.abc_max = 27
|
18
|
+
cane.no_doc = true
|
19
|
+
cane.style_measure = 200
|
20
|
+
end
|
21
|
+
end
|
22
|
+
rescue LoadError
|
23
|
+
warn "Cane is not available, metric:quality task not provided."
|
24
|
+
end
|
25
|
+
|
26
|
+
task :default => :spec
|
27
|
+
task :test => :spec
|
data/lib/multi_mime.rb
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
module MultiMime
|
2
|
+
extend self
|
3
|
+
|
4
|
+
REQUIREMENT_MAP = [
|
5
|
+
["mime/types", :mime_types],
|
6
|
+
["mimemagic", :mimemagic],
|
7
|
+
["action_dispatch/http/mime_type", :mime_type],
|
8
|
+
["rack/mime", :rack_mime]
|
9
|
+
]
|
10
|
+
|
11
|
+
# The default adapter based on what you currently
|
12
|
+
# have loaded and installed. First checks to see
|
13
|
+
# if any adapters are already loaded, then checks
|
14
|
+
# to see which are installed if none are loaded.
|
15
|
+
def default_adapter
|
16
|
+
return :mime_types if defined?(::MIME::Types)
|
17
|
+
return :mimemagic if defined?(::MimeMagic)
|
18
|
+
return :mime_type if defined?(::Mime::Type)
|
19
|
+
return :rack_mime if defined?(::Rack::Mime)
|
20
|
+
|
21
|
+
REQUIREMENT_MAP.each do |library, adapter|
|
22
|
+
begin
|
23
|
+
require library
|
24
|
+
return adapter
|
25
|
+
rescue ::LoadError
|
26
|
+
next
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
Kernel.warn "[WARNING] MultiMime hasn't been able to detect an adapter"
|
31
|
+
|
32
|
+
nil
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
# Get the current adapter class.
|
37
|
+
def adapter
|
38
|
+
return @adapter if defined?(@adapter) && @adapter
|
39
|
+
self.use default_adapter # load default adapter
|
40
|
+
@adapter
|
41
|
+
end
|
42
|
+
|
43
|
+
# Set the Mime parser utilizing a symbol, string, or class.
|
44
|
+
#
|
45
|
+
# @param [Symbol, String, Class] new_adapter
|
46
|
+
# * mime_types
|
47
|
+
# * mimemagic
|
48
|
+
# * mime_type
|
49
|
+
# * rack_mime
|
50
|
+
def use(new_adapter)
|
51
|
+
@adapter = load_adapter(new_adapter)
|
52
|
+
end
|
53
|
+
alias :adapter= :use
|
54
|
+
|
55
|
+
|
56
|
+
# Get mime type by mime type
|
57
|
+
#
|
58
|
+
# @param [String] mime_type The mime_type to determine against
|
59
|
+
# @param [Hash] opts
|
60
|
+
# * adapter [String] If set, the selected adapter will be used for this call.
|
61
|
+
# @return [String] Mime type
|
62
|
+
def type_for(mime_type, opts={})
|
63
|
+
raise ArgumentError, "Mime Type must be a String. #{mime_type.inspect} given." unless mime_type.is_a? String
|
64
|
+
adapter = current_adapter(opts)
|
65
|
+
adapter.type_for(mime_type, opts)
|
66
|
+
end
|
67
|
+
alias :by_type :type_for
|
68
|
+
|
69
|
+
# Get mime type by extension
|
70
|
+
#
|
71
|
+
# @param [String] extension The extension to determine against
|
72
|
+
# @param [Hash] opts
|
73
|
+
# * adapter [String] If set, the selected adapter will be used for this call.
|
74
|
+
# @return [String] Mime type
|
75
|
+
def type_for_extension(extension, opts={})
|
76
|
+
raise ArgumentError, "Extension must be a String. #{extension.inspect} given." unless extension.is_a? String
|
77
|
+
adapter = current_adapter(opts)
|
78
|
+
adapter.type_for_extension(extension, opts)
|
79
|
+
end
|
80
|
+
alias :by_extension :type_for_extension
|
81
|
+
|
82
|
+
# Get mime type by path
|
83
|
+
#
|
84
|
+
# @param [String] path The path to determine against
|
85
|
+
# @param [Hash] opts
|
86
|
+
# * adapter [String] If set, the selected adapter will be used for this call.
|
87
|
+
# @return [String] Mime type
|
88
|
+
def type_for_path(path, opts={})
|
89
|
+
raise ArgumentError, "Path must be a String. #{path.inspect} given." unless path.is_a? String
|
90
|
+
adapter = current_adapter(opts)
|
91
|
+
adapter.type_for_path(path, opts)
|
92
|
+
end
|
93
|
+
alias :by_path :type_for_path
|
94
|
+
|
95
|
+
# Get mime type by file
|
96
|
+
#
|
97
|
+
# @param [File] file The file to determine against
|
98
|
+
# @param [Hash] opts
|
99
|
+
# * adapter [String] If set, the selected adapter will be used for this call.
|
100
|
+
# @return [String] Mime type
|
101
|
+
def type_for_file(file, opts={})
|
102
|
+
raise ArgumentError, "File must be a File. #{file.inspect} given." unless file.is_a? File
|
103
|
+
adapter = current_adapter(opts)
|
104
|
+
adapter.type_for_file(file, opts)
|
105
|
+
end
|
106
|
+
alias :by_file :type_for_file
|
107
|
+
|
108
|
+
private
|
109
|
+
|
110
|
+
#
|
111
|
+
def current_adapter(opts={})
|
112
|
+
if new_adapter = opts.delete(:adapter)
|
113
|
+
load_adapter(new_adapter)
|
114
|
+
else
|
115
|
+
self.adapter
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
#
|
120
|
+
def load_adapter(new_adapter)
|
121
|
+
case new_adapter
|
122
|
+
when String, Symbol
|
123
|
+
require "multi_mime/adapters/#{new_adapter}"
|
124
|
+
MultiMime::Adapters.const_get(:"#{new_adapter.to_s.split("_").map{|s| s.capitalize}.join("")}")
|
125
|
+
when NilClass, FalseClass
|
126
|
+
load_adapter self.default_adapter
|
127
|
+
when Class, Module
|
128
|
+
new_adapter
|
129
|
+
else
|
130
|
+
raise "Did not recognize your adapter specification. Please specify either a symbol or a class."
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
#
|
135
|
+
def with_adapter(new_adapter)
|
136
|
+
old_adapter, self.adapter = adapter, new_adapter
|
137
|
+
yield
|
138
|
+
ensure
|
139
|
+
self.adapter = old_adapter
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "singleton"
|
2
|
+
|
3
|
+
module MultiMime
|
4
|
+
class Adapter
|
5
|
+
include Singleton
|
6
|
+
class << self
|
7
|
+
|
8
|
+
#
|
9
|
+
def type_for(mime_type, opts={})
|
10
|
+
instance.type_for(mime_type, opts)
|
11
|
+
end
|
12
|
+
|
13
|
+
#
|
14
|
+
def type_for_extension(extension, opts={})
|
15
|
+
instance.type_for_extension(extension, opts)
|
16
|
+
end
|
17
|
+
|
18
|
+
#
|
19
|
+
def type_for_path(path, opts={})
|
20
|
+
instance.type_for_path(path, opts)
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
def type_for_file(file, opts={})
|
25
|
+
instance.type_for_file(file, opts)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "action_dispatch/http/mime_type" unless defined?(::Mime::Type)
|
2
|
+
require "multi_mime/adapter"
|
3
|
+
require "multi_mime/null_type"
|
4
|
+
|
5
|
+
module MultiMime
|
6
|
+
module Adapters
|
7
|
+
|
8
|
+
# Use the Mime::Type library
|
9
|
+
class MimeType < Adapter
|
10
|
+
|
11
|
+
#
|
12
|
+
def type_for(mime_type, opts={})
|
13
|
+
::Mime::Type.parse_data_with_trailing_star(mime_type).first || MultiMime::NullType.new
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
def type_for_extension(extension, opts={})
|
18
|
+
extension = extension[/(?:.*\.)(.*$)/, 1] if extension.include?(".") # requires just the extension, without the dot
|
19
|
+
::Mime::Type.lookup_by_extension(extension) || MultiMime::NullType.new
|
20
|
+
end
|
21
|
+
|
22
|
+
#
|
23
|
+
def type_for_path(path, opts={})
|
24
|
+
type_for_extension(File.extname(path))
|
25
|
+
end
|
26
|
+
|
27
|
+
#
|
28
|
+
def type_for_file(file, opts={})
|
29
|
+
type_for_extension(File.extname(file))
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "mime/types" unless defined?(::MIME::Types)
|
2
|
+
require "multi_mime/adapter"
|
3
|
+
require "multi_mime/null_type"
|
4
|
+
|
5
|
+
module MultiMime
|
6
|
+
module Adapters
|
7
|
+
|
8
|
+
# Use the MIME::Types library
|
9
|
+
class MimeTypes < Adapter
|
10
|
+
|
11
|
+
#
|
12
|
+
def type_for(mime_type, opts={})
|
13
|
+
::MIME::Types[mime_type].first || MultiMime::NullType.new
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
def type_for_extension(extension, opts={})
|
18
|
+
::MIME::Types.type_for(extension).first || MultiMime::NullType.new
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
def type_for_path(path, opts={})
|
23
|
+
type_for_extension(File.extname(path))
|
24
|
+
end
|
25
|
+
|
26
|
+
#
|
27
|
+
def type_for_file(file, opts={})
|
28
|
+
type_for_extension(File.extname(file))
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "mimemagic" unless defined?(::Mimemagic)
|
2
|
+
require "multi_mime/adapter"
|
3
|
+
require "multi_mime/null_type"
|
4
|
+
|
5
|
+
module MultiMime
|
6
|
+
module Adapters
|
7
|
+
|
8
|
+
# Use the Mimemagic library
|
9
|
+
class Mimemagic < Adapter
|
10
|
+
|
11
|
+
#
|
12
|
+
def type_for(mime_type, opts={})
|
13
|
+
type = ::MimeMagic::EXTENSIONS.key(mime_type) # Mimemagic actually doesnt allow you to simply verify that a mime type exists
|
14
|
+
type ? mime_type : MultiMime::NullType.new
|
15
|
+
end
|
16
|
+
|
17
|
+
#
|
18
|
+
def type_for_extension(extension, opts={})
|
19
|
+
::MimeMagic.by_extension(extension) || MultiMime::NullType.new
|
20
|
+
end
|
21
|
+
|
22
|
+
#
|
23
|
+
def type_for_path(path, opts={})
|
24
|
+
type_for_extension(File.extname(path))
|
25
|
+
end
|
26
|
+
|
27
|
+
#
|
28
|
+
def type_for_file(file, opts={})
|
29
|
+
type_for_extension(File.extname(file))
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "rack/mime" unless defined?(::Rack::Mime)
|
2
|
+
require "multi_mime/adapter"
|
3
|
+
require "multi_mime/null_type"
|
4
|
+
|
5
|
+
module MultiMime
|
6
|
+
module Adapters
|
7
|
+
|
8
|
+
# Use the Rack::Mime library
|
9
|
+
class RackMime < Adapter
|
10
|
+
|
11
|
+
#
|
12
|
+
def type_for(mime_type, opts={})
|
13
|
+
type = ::Rack::Mime::MIME_TYPES.key(mime_type) # Rack::Mime actually doesnt allow you to simply verify that a mime type exists
|
14
|
+
type ? mime_type : MultiMime::NullType.new
|
15
|
+
end
|
16
|
+
|
17
|
+
#
|
18
|
+
def type_for_extension(extension, opts={})
|
19
|
+
extension.insert(0, ".") unless extension[0] == "." # requires the extension with a dot
|
20
|
+
::Rack::Mime.mime_type(extension, MultiMime::NullType.new) # set the fallback to nil
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
def type_for_path(path, opts={})
|
25
|
+
type_for_extension(File.extname(path))
|
26
|
+
end
|
27
|
+
|
28
|
+
#
|
29
|
+
def type_for_file(file, opts={})
|
30
|
+
type_for_extension(File.extname(file))
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
data/multi_mime.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "multi_mime/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.add_development_dependency "awesome_print"
|
7
|
+
gem.add_development_dependency "bundler", "~> 1.0"
|
8
|
+
gem.name = "multi_mime"
|
9
|
+
gem.version = MultiMime::VERSION
|
10
|
+
gem.authors = ["Karl Freeman"]
|
11
|
+
gem.email = ["karlfreeman@gmail.com"]
|
12
|
+
gem.license = "MIT"
|
13
|
+
gem.description = %q{A generic swappable back-end for Mime::Type detection}
|
14
|
+
gem.summary = %q{A generic swappable back-end for Mime::Type detection}
|
15
|
+
gem.homepage = "https://github.com/karlfreeman/multi_mime"
|
16
|
+
gem.files = `git ls-files`.split("\n")
|
17
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.required_ruby_version = ">= 1.9.2"
|
20
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
shared_examples_for "an adapter" do |adapter|
|
2
|
+
|
3
|
+
before do
|
4
|
+
begin
|
5
|
+
MultiMime.use adapter
|
6
|
+
rescue LoadError
|
7
|
+
pending "Adapter #{adapter} couldn't be loaded (not installed?)"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe :type_for do
|
12
|
+
|
13
|
+
context :with_correct_mime_type do
|
14
|
+
subject { MultiMime.type_for("text/html") }
|
15
|
+
it { should eq "text/html" }
|
16
|
+
end
|
17
|
+
|
18
|
+
context :with_incorrect_mime_type do
|
19
|
+
subject { MultiMime.type_for("foo/bar") }
|
20
|
+
it { should be_nil }
|
21
|
+
it { should be_a MultiMime::NullType }
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
describe :type_for_extension do
|
27
|
+
|
28
|
+
context :without_dot do
|
29
|
+
subject { MultiMime.type_for_extension("html") }
|
30
|
+
it { should eq "text/html" }
|
31
|
+
end
|
32
|
+
|
33
|
+
context :with_dot do
|
34
|
+
subject { MultiMime.type_for_extension(".html") }
|
35
|
+
it { should eq "text/html" }
|
36
|
+
end
|
37
|
+
|
38
|
+
context :unknown_extension do
|
39
|
+
subject { MultiMime.type_for_extension(".foo.bar") }
|
40
|
+
it { should be_nil }
|
41
|
+
it { should be_a MultiMime::NullType }
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
describe :type_for_path do
|
47
|
+
|
48
|
+
context :known_extension do
|
49
|
+
subject { MultiMime.type_for_path("/usr/local/foo/bar.html") }
|
50
|
+
it { should eq "text/html" }
|
51
|
+
end
|
52
|
+
|
53
|
+
context :unknown_extension do
|
54
|
+
subject { MultiMime.type_for_path("/usr/local/foo/bar.foo") }
|
55
|
+
it { should be_nil }
|
56
|
+
it { should be_a MultiMime::NullType }
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
describe :type_for_file, :fakefs => true do
|
62
|
+
|
63
|
+
let(:extension_file) {
|
64
|
+
File.open("foo.html", "w")
|
65
|
+
}
|
66
|
+
let(:extensionless_file) {
|
67
|
+
File.open("foo", "w")
|
68
|
+
}
|
69
|
+
|
70
|
+
context :extension_file do
|
71
|
+
subject { MultiMime.type_for_file(extension_file) }
|
72
|
+
it { should eq "text/html" }
|
73
|
+
end
|
74
|
+
|
75
|
+
context :extensionless_file do
|
76
|
+
subject { MultiMime.type_for_file(extensionless_file) }
|
77
|
+
it { should be_nil }
|
78
|
+
it { should be_a MultiMime::NullType }
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
describe :null_type do
|
84
|
+
|
85
|
+
context :with_incorrect_mime_type do
|
86
|
+
|
87
|
+
it "should'nt raise an exception when querying for formats" do
|
88
|
+
mime = MultiMime.type_for("foo/bar")
|
89
|
+
expect(mime).to be_nil
|
90
|
+
expect(mime).to be_a MultiMime::NullType
|
91
|
+
expect(mime.html?).to be false
|
92
|
+
expect(mime.xml?).to be false
|
93
|
+
expect(mime.json?).to be false
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
2
|
+
|
3
|
+
require "bundler"
|
4
|
+
Bundler.setup
|
5
|
+
begin; require "awesome_print"; rescue LoadError; end
|
6
|
+
|
7
|
+
require "rspec"
|
8
|
+
|
9
|
+
require "support/pry"
|
10
|
+
require "support/fakefs"
|
11
|
+
require "support/simplecov"
|
12
|
+
|
13
|
+
require "shared/adapter"
|
14
|
+
|
15
|
+
require "multi_mime"
|
16
|
+
|
17
|
+
# used as a stupid mixin class
|
18
|
+
class DummyClass
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
RSpec.configure do |config|
|
23
|
+
|
24
|
+
#
|
25
|
+
config.expect_with :rspec do |c|
|
26
|
+
c.syntax = :expect
|
27
|
+
end
|
28
|
+
|
29
|
+
#
|
30
|
+
def jruby?
|
31
|
+
defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/spec/support/pry.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
if defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby"
|
2
|
+
|
3
|
+
require "simplecov"
|
4
|
+
|
5
|
+
if ENV["CI"]
|
6
|
+
require "coveralls"
|
7
|
+
Coveralls.wear!
|
8
|
+
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
|
9
|
+
end
|
10
|
+
|
11
|
+
SimpleCov.start do
|
12
|
+
add_filter "vendor"
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe MultiMime do
|
4
|
+
|
5
|
+
context :validations do
|
6
|
+
|
7
|
+
describe :type_for do
|
8
|
+
specify { expect { MultiMime.type_for(nil) }.to raise_error(ArgumentError) }
|
9
|
+
end
|
10
|
+
|
11
|
+
describe :type_for_extension do
|
12
|
+
specify { expect { MultiMime.type_for_extension(nil) }.to raise_error(ArgumentError) }
|
13
|
+
end
|
14
|
+
|
15
|
+
describe :type_for_path do
|
16
|
+
specify { expect { MultiMime.type_for_path(nil) }.to raise_error(ArgumentError) }
|
17
|
+
end
|
18
|
+
|
19
|
+
describe :type_for_file do
|
20
|
+
specify { expect { MultiMime.type_for_file(nil) }.to raise_error(ArgumentError) }
|
21
|
+
specify { expect { MultiMime.type_for_file(".html") }.to raise_error(ArgumentError) }
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
context :alias do
|
27
|
+
|
28
|
+
describe :by_type do
|
29
|
+
subject { MultiMime.instance_method(:by_type) }
|
30
|
+
it { should == MultiMime.instance_method(:type_for) }
|
31
|
+
end
|
32
|
+
|
33
|
+
describe :by_extension do
|
34
|
+
subject { MultiMime.instance_method(:by_extension) }
|
35
|
+
it { should == MultiMime.instance_method(:type_for_extension) }
|
36
|
+
end
|
37
|
+
|
38
|
+
describe :by_path do
|
39
|
+
subject { MultiMime.instance_method(:by_path) }
|
40
|
+
it { should == MultiMime.instance_method(:type_for_path) }
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
# context :defaults do
|
46
|
+
|
47
|
+
# describe "MIME::Types" do
|
48
|
+
|
49
|
+
# it "should detect adaptor" do
|
50
|
+
# Object.send(:remove_const, :MIME) if defined?(MIME)
|
51
|
+
# require "mime/types"
|
52
|
+
# MultiMime.default_adapter.should eq :mime_types
|
53
|
+
# end
|
54
|
+
|
55
|
+
# after(:each) do
|
56
|
+
# Object.send(:remove_const, :MIME) if defined?(MIME)
|
57
|
+
# end
|
58
|
+
|
59
|
+
# end
|
60
|
+
|
61
|
+
# describe "MimeMagic" do
|
62
|
+
|
63
|
+
# it "should detect adaptor" do
|
64
|
+
# Object.send(:remove_const, :MimeMagic) if defined?(MimeMagic)
|
65
|
+
# require "mimemagic"
|
66
|
+
# MultiMime.default_adapter.should eq :mimemagic
|
67
|
+
# end
|
68
|
+
|
69
|
+
# after(:each) do
|
70
|
+
# Object.send(:remove_const, :MimeMagic) if defined?(MimeMagic)
|
71
|
+
# end
|
72
|
+
|
73
|
+
# end
|
74
|
+
|
75
|
+
# describe "Mime::Type" do
|
76
|
+
|
77
|
+
# it "should detect adaptor" do
|
78
|
+
# Object.send(:remove_const, :Mime) if defined?(Mime)
|
79
|
+
# require "action_dispatch/http/mime_type"
|
80
|
+
# MultiMime.default_adapter.should eq :mime_type
|
81
|
+
# end
|
82
|
+
|
83
|
+
# after(:each) do
|
84
|
+
# Object.send(:remove_const, :Mime) if defined?(Mime)
|
85
|
+
# end
|
86
|
+
|
87
|
+
# end
|
88
|
+
|
89
|
+
# describe "Rack::Mime" do
|
90
|
+
|
91
|
+
# it "should detect adaptor" do
|
92
|
+
# Object.send(:remove_const, :Rack) if defined?(Rack)
|
93
|
+
# require "rack/mime"
|
94
|
+
# MultiMime.default_adapter.should eq :rack_mime
|
95
|
+
# end
|
96
|
+
|
97
|
+
# after(:each) do
|
98
|
+
# Object.send(:remove_const, :Rack) if defined?(Rack)
|
99
|
+
# end
|
100
|
+
|
101
|
+
# end
|
102
|
+
|
103
|
+
# end
|
104
|
+
|
105
|
+
%w(mime_types mimemagic mime_type rack_mime).each do |adapter|
|
106
|
+
context adapter do
|
107
|
+
it_behaves_like "an adapter", adapter
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: multi_mime
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Karl Freeman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: awesome_print
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
description: A generic swappable back-end for Mime::Type detection
|
42
|
+
email:
|
43
|
+
- karlfreeman@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .document
|
49
|
+
- .gitignore
|
50
|
+
- .rspec
|
51
|
+
- .travis.yml
|
52
|
+
- .yardopts
|
53
|
+
- CHANGELOG.md
|
54
|
+
- CONTRIBUTING.md
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.md
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/multi_mime.rb
|
60
|
+
- lib/multi_mime/adapter.rb
|
61
|
+
- lib/multi_mime/adapters/mime_type.rb
|
62
|
+
- lib/multi_mime/adapters/mime_types.rb
|
63
|
+
- lib/multi_mime/adapters/mimemagic.rb
|
64
|
+
- lib/multi_mime/adapters/rack_mime.rb
|
65
|
+
- lib/multi_mime/null_type.rb
|
66
|
+
- lib/multi_mime/version.rb
|
67
|
+
- multi_mime.gemspec
|
68
|
+
- spec/shared/adapter.rb
|
69
|
+
- spec/spec_helper.rb
|
70
|
+
- spec/support/fakefs.rb
|
71
|
+
- spec/support/pry.rb
|
72
|
+
- spec/support/simplecov.rb
|
73
|
+
- spec/unit/multi_mime_spec.rb
|
74
|
+
homepage: https://github.com/karlfreeman/multi_mime
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: 1.9.2
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.0.1
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: A generic swappable back-end for Mime::Type detection
|
98
|
+
test_files:
|
99
|
+
- spec/shared/adapter.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
- spec/support/fakefs.rb
|
102
|
+
- spec/support/pry.rb
|
103
|
+
- spec/support/simplecov.rb
|
104
|
+
- spec/unit/multi_mime_spec.rb
|
105
|
+
has_rdoc:
|