multi_mime 1.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +6 -4
- data/CONTRIBUTING.md +2 -4
- data/Gemfile +0 -4
- data/README.md +6 -4
- data/lib/multi_mime.rb +11 -15
- data/lib/multi_mime/adapter_error.rb +15 -0
- data/lib/multi_mime/version.rb +1 -1
- data/multi_mime.gemspec +1 -1
- data/spec/spec_helper.rb +17 -16
- data/spec/unit/multi_mime_spec.rb +10 -13
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 283efbc22a54d1e6c7966a912d6dc10ef5a18bb3
|
4
|
+
data.tar.gz: 0b56e2dfae1e0811531ac0d65da3850c4328be3e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91ffaa1e34ddaee7b6b981e30ed06fbc47c2046415371ff8e60b8e4044cc1d68769780c49a53792d4a6900881bc844a7739d2fcda2891879521f07129e8d4e05
|
7
|
+
data.tar.gz: a86a627ab130712773aa5949800ed4b9aa4b4233e7362c5bd4fae34c180df652b976fa8ea533d9dd4a5691f86265bc043f11d3c3daea0ddede68a1adb9dcf06f
|
data/.travis.yml
CHANGED
@@ -3,18 +3,20 @@ cache: bundler
|
|
3
3
|
bundler_args: --without development
|
4
4
|
rvm:
|
5
5
|
- ruby-head
|
6
|
+
- ruby
|
6
7
|
- jruby-head
|
8
|
+
- jruby
|
7
9
|
- 2.1.0
|
8
10
|
- 2.0.0
|
9
11
|
- 1.9.3
|
10
|
-
- rbx-
|
11
|
-
- jruby-19mode
|
12
|
+
- rbx-2
|
12
13
|
matrix:
|
13
14
|
fast_finish: true
|
14
15
|
allow_failures:
|
15
16
|
- rvm: ruby-head
|
17
|
+
- rvm: ruby
|
16
18
|
- rvm: jruby-head
|
17
|
-
- rvm:
|
18
|
-
- rvm:
|
19
|
+
- rvm: jruby
|
20
|
+
- rvm: rbx-2
|
19
21
|
notifications:
|
20
22
|
email: false
|
data/CONTRIBUTING.md
CHANGED
@@ -36,10 +36,8 @@ Ideally, a bug report should include a pull request with failing specs.
|
|
36
36
|
4. Run `bundle exec rake spec`. If your specs pass, return to step 3.
|
37
37
|
5. Implement your feature or bug fix.
|
38
38
|
6. Run `bundle exec rake spec`. If your specs fail, return to step 5.
|
39
|
-
7.
|
40
|
-
|
41
|
-
8. Add, commit, and push your changes.
|
42
|
-
9. [Submit a pull request.][pr]
|
39
|
+
7. Add, commit, and push your changes.
|
40
|
+
8. [Submit a pull request.][pr]
|
43
41
|
|
44
42
|
[fork]: http://help.github.com/fork-a-repo/
|
45
43
|
[branch]: http://learn.github.com/p/branching.html
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
A generic swappable back-end for MIME type detection
|
4
4
|
|
5
|
-
Lots of ruby libraries utilize
|
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
6
|
|
7
7
|
eg [MultiJson](https://github.com/intridea/multi_json) for MIME types.
|
8
8
|
|
@@ -18,7 +18,7 @@ require 'multi_mime'
|
|
18
18
|
|
19
19
|
## Features / Usage Examples
|
20
20
|
|
21
|
-
Part of utilizing various
|
21
|
+
Part of utilizing various MIME type libraries is the need for a common interface. `MultiMime` provides just that with following methods.
|
22
22
|
|
23
23
|
```ruby
|
24
24
|
MultiMime.type_for('text/html') # 'text/html'
|
@@ -30,7 +30,7 @@ MultiMime.type_for_extension('.html') # 'text/html'
|
|
30
30
|
MultiMime.type_for_path('/usr/local/foo/bar/foo.html') # 'text/html'
|
31
31
|
# alias :by_path
|
32
32
|
|
33
|
-
MultiMime.type_for_file(
|
33
|
+
MultiMime.type_for_file(File.open('foo.html', 'w')) # 'text/html'
|
34
34
|
# alias :by_file
|
35
35
|
```
|
36
36
|
|
@@ -54,6 +54,8 @@ MultiMime.adapter # MultiMime::Adapters::MimeTypes
|
|
54
54
|
MultiMime.type_for_extension('.json') # 'application/json'
|
55
55
|
```
|
56
56
|
|
57
|
+
When MultiMime fails to load the specified adapter, it'll throw `MultiMime::AdapterError` which inherits from `ArgumentError`.
|
58
|
+
|
57
59
|
## Badges
|
58
60
|
|
59
61
|
[![Gem Version](http://img.shields.io/gem/v/multi_mime.svg)][gem]
|
@@ -61,7 +63,7 @@ MultiMime.type_for_extension('.json') # 'application/json'
|
|
61
63
|
[![Code Quality](http://img.shields.io/codeclimate/github/karlfreeman/multi_mime.svg)][codeclimate]
|
62
64
|
[![Gittip](http://img.shields.io/gittip/karlfreeman.svg)][gittip]
|
63
65
|
|
64
|
-
## Supported
|
66
|
+
## Supported MIME Engines
|
65
67
|
|
66
68
|
* [MIME::Types](https://github.com/halostatue/mime-types)
|
67
69
|
* [MagicMime](https://github.com/minad/mimemagic)
|
data/lib/multi_mime.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'thread'
|
2
|
+
require 'multi_mime/adapter_error'
|
1
3
|
module MultiMime
|
2
4
|
extend self
|
3
5
|
|
@@ -26,16 +28,13 @@ module MultiMime
|
|
26
28
|
next
|
27
29
|
end
|
28
30
|
end
|
29
|
-
|
30
|
-
Kernel.warn '[WARNING] MultiMime hasn\'t been able to detect an adapter'
|
31
|
-
|
31
|
+
fail 'MultiMime hasn\'t been able to detect a default_adapter'
|
32
32
|
nil
|
33
33
|
end
|
34
34
|
|
35
35
|
# Get the current adapter class.
|
36
36
|
def adapter
|
37
|
-
|
38
|
-
use default_adapter # load default adapter
|
37
|
+
use(default_adapter) unless defined?(@adapter) # load default adapter
|
39
38
|
@adapter
|
40
39
|
end
|
41
40
|
|
@@ -47,7 +46,9 @@ module MultiMime
|
|
47
46
|
# * mime_type
|
48
47
|
# * rack_mime
|
49
48
|
def use(new_adapter)
|
50
|
-
|
49
|
+
Thread.exclusive do
|
50
|
+
@adapter = load_adapter(new_adapter)
|
51
|
+
end
|
51
52
|
end
|
52
53
|
alias_method :adapter=, :use
|
53
54
|
|
@@ -132,8 +133,7 @@ module MultiMime
|
|
132
133
|
end
|
133
134
|
end
|
134
135
|
if adapter_clazz.nil?
|
135
|
-
|
136
|
-
load_adapter nil
|
136
|
+
raise ::LoadError, new_adapter
|
137
137
|
else
|
138
138
|
return adapter_clazz
|
139
139
|
end
|
@@ -142,14 +142,10 @@ module MultiMime
|
|
142
142
|
when Class, Module
|
143
143
|
new_adapter
|
144
144
|
else
|
145
|
-
|
145
|
+
raise ::LoadError, new_adapter
|
146
146
|
end
|
147
|
+
rescue ::LoadError => exception
|
148
|
+
raise AdapterError.build(exception)
|
147
149
|
end
|
148
150
|
|
149
|
-
def with_adapter(new_adapter)
|
150
|
-
old_adapter, self.adapter = adapter, new_adapter
|
151
|
-
yield
|
152
|
-
ensure
|
153
|
-
self.adapter = old_adapter
|
154
|
-
end
|
155
151
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module MultiMime
|
2
|
+
class AdapterError < ArgumentError
|
3
|
+
attr_reader :cause
|
4
|
+
|
5
|
+
def self.build(original_exception)
|
6
|
+
message = "Did not recognize your adapter specification (#{original_exception.message})."
|
7
|
+
new(message).tap do |exception|
|
8
|
+
exception.instance_eval do
|
9
|
+
@cause = original_exception
|
10
|
+
set_backtrace original_exception.backtrace
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/multi_mime/version.rb
CHANGED
data/multi_mime.gemspec
CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_development_dependency 'bundler', '~> 1.5'
|
23
23
|
spec.add_development_dependency 'rake', '~> 10.0'
|
24
24
|
spec.add_development_dependency 'kramdown', '>= 0.14'
|
25
|
-
spec.add_development_dependency 'rubocop'
|
25
|
+
spec.add_development_dependency 'rubocop', '~> 0.19'
|
26
26
|
spec.add_development_dependency 'pry'
|
27
27
|
spec.add_development_dependency 'yard'
|
28
28
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -13,23 +13,24 @@ RSpec.configure do |config|
|
|
13
13
|
config.expect_with :rspec do |c|
|
14
14
|
c.syntax = :expect
|
15
15
|
end
|
16
|
-
end
|
17
16
|
|
18
|
-
def jruby?
|
19
|
-
|
20
|
-
end
|
17
|
+
def jruby?
|
18
|
+
defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
|
19
|
+
end
|
21
20
|
|
22
|
-
def undefine_constants(*consts)
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
21
|
+
def undefine_constants(*consts)
|
22
|
+
values = {}
|
23
|
+
consts.each do |const|
|
24
|
+
if Object.const_defined?(const)
|
25
|
+
values[const] = Object.const_get(const)
|
26
|
+
Object.send :remove_const, const
|
27
|
+
end
|
28
|
+
end
|
29
|
+
yield
|
30
|
+
ensure
|
31
|
+
values.each do |const, value|
|
32
|
+
Object.const_set const, value
|
28
33
|
end
|
29
34
|
end
|
30
|
-
|
31
|
-
|
32
|
-
values.each do |const, value|
|
33
|
-
Object.const_set const, value
|
34
|
-
end
|
35
|
-
end
|
35
|
+
|
36
|
+
end
|
@@ -2,8 +2,13 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe MultiMime do
|
4
4
|
|
5
|
-
|
5
|
+
it 'should be thread safe' do
|
6
|
+
t1 = Thread.new { adapter = MultiMime.adapter; sleep 0.1; adapter }
|
7
|
+
t2 = Thread.new { MultiMime.adapter }
|
8
|
+
expect(t1.value).to eql t2.value
|
9
|
+
end
|
6
10
|
|
11
|
+
context :validations do
|
7
12
|
describe :type_for do
|
8
13
|
specify { expect { MultiMime.type_for(nil) }.to raise_error(ArgumentError) }
|
9
14
|
end
|
@@ -21,10 +26,13 @@ describe MultiMime do
|
|
21
26
|
specify { expect { MultiMime.type_for_file('.html') }.to raise_error(ArgumentError) }
|
22
27
|
end
|
23
28
|
|
29
|
+
describe :adapter= do
|
30
|
+
specify { expect { MultiMime.adapter = Time.new }.to raise_error(MultiMime::AdapterError, /Did not recognize your adapter specification/) }
|
31
|
+
specify { expect { MultiMime.adapter = 'foo' }.to raise_error(MultiMime::AdapterError, /Did not recognize your adapter specification/) }
|
32
|
+
end
|
24
33
|
end
|
25
34
|
|
26
35
|
context :alias do
|
27
|
-
|
28
36
|
describe :by_type do
|
29
37
|
subject { MultiMime.instance_method(:by_type) }
|
30
38
|
it { should == MultiMime.instance_method(:type_for) }
|
@@ -44,13 +52,10 @@ describe MultiMime do
|
|
44
52
|
subject { MultiMime.instance_method(:by_file) }
|
45
53
|
it { should == MultiMime.instance_method(:type_for_file) }
|
46
54
|
end
|
47
|
-
|
48
55
|
end
|
49
56
|
|
50
57
|
context :defaults do
|
51
|
-
|
52
58
|
describe 'MIME::Types' do
|
53
|
-
|
54
59
|
around do |example|
|
55
60
|
undefine_constants(:MimeMagic, :Mime, :Rack) { example.call }
|
56
61
|
MultiMime.reset_adapter
|
@@ -60,11 +65,9 @@ describe MultiMime do
|
|
60
65
|
require 'mime/types'
|
61
66
|
expect(MultiMime.default_adapter).to eq :mime_types
|
62
67
|
end
|
63
|
-
|
64
68
|
end
|
65
69
|
|
66
70
|
describe 'MimeMagic' do
|
67
|
-
|
68
71
|
around do |example|
|
69
72
|
undefine_constants(:MIME, :Mime, :Rack) { example.call }
|
70
73
|
MultiMime.reset_adapter
|
@@ -74,11 +77,9 @@ describe MultiMime do
|
|
74
77
|
require 'mimemagic'
|
75
78
|
expect(MultiMime.default_adapter).to eq :mimemagic
|
76
79
|
end
|
77
|
-
|
78
80
|
end
|
79
81
|
|
80
82
|
describe 'Mime::Type' do
|
81
|
-
|
82
83
|
around do |example|
|
83
84
|
undefine_constants(:MIME, :MimeMagic, :Rack) { example.call }
|
84
85
|
MultiMime.reset_adapter
|
@@ -88,11 +89,9 @@ describe MultiMime do
|
|
88
89
|
require 'action_dispatch/http/mime_type'
|
89
90
|
expect(MultiMime.default_adapter).to eq :mime_type
|
90
91
|
end
|
91
|
-
|
92
92
|
end
|
93
93
|
|
94
94
|
describe 'Rack::Mime' do
|
95
|
-
|
96
95
|
around do |example|
|
97
96
|
undefine_constants(:MIME, :Mime, :MimeMagic) { example.call }
|
98
97
|
MultiMime.reset_adapter
|
@@ -102,7 +101,6 @@ describe MultiMime do
|
|
102
101
|
require 'rack/mime'
|
103
102
|
expect(MultiMime.default_adapter).to eq :rack_mime
|
104
103
|
end
|
105
|
-
|
106
104
|
end
|
107
105
|
|
108
106
|
end
|
@@ -112,5 +110,4 @@ describe MultiMime do
|
|
112
110
|
it_behaves_like 'an adapter', adapter
|
113
111
|
end
|
114
112
|
end
|
115
|
-
|
116
113
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multi_mime
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Karl Freeman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -56,16 +56,16 @@ dependencies:
|
|
56
56
|
name: rubocop
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
61
|
+
version: '0.19'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
68
|
+
version: '0.19'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: pry
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -114,6 +114,7 @@ files:
|
|
114
114
|
- Rakefile
|
115
115
|
- lib/multi_mime.rb
|
116
116
|
- lib/multi_mime/adapter.rb
|
117
|
+
- lib/multi_mime/adapter_error.rb
|
117
118
|
- lib/multi_mime/adapters/mime_type.rb
|
118
119
|
- lib/multi_mime/adapters/mime_types.rb
|
119
120
|
- lib/multi_mime/adapters/mimemagic.rb
|
@@ -158,4 +159,3 @@ test_files:
|
|
158
159
|
- spec/support/pry.rb
|
159
160
|
- spec/support/timecop.rb
|
160
161
|
- spec/unit/multi_mime_spec.rb
|
161
|
-
has_rdoc:
|