multi_mime 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
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=
2
+ SHA1:
3
+ metadata.gz: 8ca98ae851bcf551a7b48af37da35d64b7b96442
4
+ data.tar.gz: 61f05c26320dff71a61eeeca7cd37da0e2481842
5
+ SHA512:
6
+ metadata.gz: c2b2780e6c326826415799b681c7b0d7987eb83c02cc7de0f10efca6d5f39bd100544139c09052bad6d9643f0cffd820581267047cc6bb79bbdf62ff33851ea4
7
+ data.tar.gz: 095d2022c8fda77d7d29d1d42718b0b0dec45725f953ac2db81cc9e412d975badc0f8108baff67346bec7c79a7adb235556e924b3316473eecb8e0bb48a3bb7d
data/.gitignore CHANGED
@@ -2,8 +2,12 @@
2
2
  *.rbc
3
3
  .bundle
4
4
  .config
5
- coverage
5
+ .yardoc
6
+ Gemfile.lock
6
7
  InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
7
11
  lib/bundler/man
8
12
  pkg
9
13
  rdoc
@@ -11,9 +15,4 @@ spec/reports
11
15
  test/tmp
12
16
  test/version_tmp
13
17
  tmp
14
- *.lock
15
-
16
- # YARD artifacts
17
- .yardoc
18
- _yardoc
19
- doc/
18
+ bin
data/.rubocop.yml ADDED
@@ -0,0 +1,24 @@
1
+ AllCops:
2
+ Includes:
3
+ - Rakefile
4
+ - Gemfile
5
+ Excludes:
6
+ - script/**
7
+ - vendor/**
8
+ - bin/**
9
+ LineLength:
10
+ Enabled: false
11
+ MethodLength:
12
+ Enabled: false
13
+ ClassLength:
14
+ Enabled: false
15
+ Documentation:
16
+ Enabled: false
17
+ Encoding:
18
+ Enabled: false
19
+ Blocks:
20
+ Enabled: false
21
+ AlignParameters:
22
+ Enabled: false
23
+ HashSyntax:
24
+ EnforcedStyle: ruby19
data/.travis.yml CHANGED
@@ -1,17 +1,19 @@
1
- bundler_args: --without development
2
1
  language: ruby
2
+ cache: bundler
3
3
  rvm:
4
- - rbx-18mode
4
+ - ruby-head
5
+ - jruby-head
6
+ - 2.1.0
7
+ - 2.0.0
8
+ - 1.9.3
5
9
  - rbx-19mode
6
- - jruby-18mode
7
10
  - jruby-19mode
8
- - 1.8.7
9
- - 1.9.2
10
- - 1.9.3
11
- - 2.0.0
12
11
  matrix:
12
+ fast_finish: true
13
13
  allow_failures:
14
- - rvm: rbx-18mode
15
- - rvm: jruby-18mode
16
- - rvm: 1.8.7
17
- - rvm: rbx-19mode
14
+ - rvm: ruby-head
15
+ - rvm: jruby-head
16
+ - rvm: rbx-19mode
17
+ - rvm: jruby-19mode
18
+ notifications:
19
+ email: false
data/Gemfile CHANGED
@@ -1,34 +1,7 @@
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
1
+ source 'https://rubygems.org'
24
2
 
25
3
  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
4
+ gem 'fakefs', github: 'defunkt/fakefs', require: 'fakefs/safe'
32
5
  end
33
6
 
34
- gemspec
7
+ gemspec
data/README.md CHANGED
@@ -2,67 +2,74 @@
2
2
 
3
3
  A generic swappable back-end for Mime Type detection
4
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.
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. eg [MultiJson](https://github.com/intridea/multi_json) for Mime Types.
6
6
 
7
- ## Features / Usage Examples
7
+ ## Installation
8
8
 
9
9
  ```ruby
10
- require "multi_mime"
10
+ gem 'multi_mime', '~> 0.0.3'
11
+ ```
12
+
13
+ ```ruby
14
+ require 'multi_mime'
15
+ ```
11
16
 
12
- MultiMime.type_for("text/html") # "text/html"
17
+ ## Features / Usage Examples
18
+
19
+ ```ruby
20
+ MultiMime.type_for('text/html') # 'text/html'
13
21
  # alias :by_type
14
22
 
15
- MultiMime.type_for_extension(".html") # "text/html"
23
+ MultiMime.type_for_extension('.html') # 'text/html'
16
24
  # alias :by_extension
17
25
 
18
- MultiMime.type_for_path("/usr/local/foo/bar/foo.html") # "text/html"
26
+ MultiMime.type_for_path('/usr/local/foo/bar/foo.html') # 'text/html'
19
27
  # alias :by_path
20
28
 
21
- MultiMime.type_for_file( File.open("foo.html", "w") ) # "text/html"
29
+ MultiMime.type_for_file( File.open('foo.html', 'w') ) # 'text/html'
22
30
  # alias :by_file
23
31
  ```
24
32
 
25
- ## Build & Dependency Status
33
+ ## Badges
26
34
 
27
35
  [![Gem Version](https://badge.fury.io/rb/multi_mime.png)][gem]
28
36
  [![Build Status](https://travis-ci.org/karlfreeman/multi_mime.png)][travis]
29
- [![Dependency Status](https://gemnasium.com/karlfreeman/multi_mime.png?travis)][gemnasium]
30
37
  [![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]
38
+ [![Gittip](http://img.shields.io/gittip/karlfreeman.png)][gittip]
32
39
 
33
40
  ## Supported Mime Engines
34
41
 
35
42
  * [MIME::Types](https://github.com/halostatue/mime-types)
36
43
  * [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 )
44
+ * [Rails](http://api.rubyonrails.org/classes/Mime/Type.html) (ActionDispatch::Http::Mime)
45
+ * [Rack](http://rack.rubyforge.org/doc/Rack/Mime.html) (Rack::Mime)
39
46
 
40
47
  ## Supported Ruby Versions
48
+
41
49
  This library aims to support and is [tested against][travis] the following Ruby
42
50
  implementations:
43
51
 
44
- * Ruby 1.9.2
45
- * Ruby 1.9.3
46
- * Ruby 2.0.0
47
- * [JRuby][]
48
- * [Rubinius][]
52
+ - Ruby 2.1.0
53
+ - Ruby 2.0.0
54
+ - Ruby 1.9.3
55
+ - [JRuby][jruby]
56
+ - [Rubinius][rubinius]
49
57
 
50
58
  # Credits
51
59
 
52
60
  Inspiration:
53
61
 
54
- * [MultiJSON](https://github.com/intridea/multi_json)
55
- * [MultiXML](https://github.com/sferik/multi_xml)
62
+ - [MultiJSON](https://github.com/intridea/multi_json)
63
+ - [MultiXML](https://github.com/sferik/multi_xml)
56
64
 
57
65
  Cribbed:
58
66
 
59
- * [MultiJSON](https://github.com/intridea/multi_json)
60
- * [Asset_Sync"s MultiMime module](https://github.com/rumblelabs/asset_sync/commit/9333bd01ae1a7cf2ffa046b8390fbc4165c38030)
67
+ - [MultiJSON](https://github.com/intridea/multi_json)
68
+ - [Asset_Sync"s MultiMime module](https://github.com/rumblelabs/asset_sync/commit/9333bd01ae1a7cf2ffa046b8390fbc4165c38030)
61
69
 
62
70
  [gem]: https://rubygems.org/gems/multi_mime
63
71
  [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
72
  [codeclimate]: https://codeclimate.com/github/karlfreeman/multi_mime
73
+ [gittip]: https://www.gittip.com/karlfreeman/
67
74
  [jruby]: http://www.jruby.org
68
75
  [rubinius]: http://rubini.us
data/Rakefile CHANGED
@@ -1,27 +1,17 @@
1
- require "bundler"
1
+ require 'bundler'
2
2
  Bundler.setup
3
3
  Bundler::GemHelper.install_tasks
4
4
 
5
- require "yard"
5
+ require 'yard'
6
6
  YARD::Rake::YardocTask.new
7
7
 
8
- require "rspec/core/rake_task"
9
- desc "Run all examples"
8
+ require 'rspec/core/rake_task'
9
+ desc 'Run all examples'
10
10
  RSpec::Core::RakeTask.new(:spec)
11
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
12
+ require 'rubocop/rake_task'
13
+ desc 'Run rubocop'
14
+ Rubocop::RakeTask.new(:rubocop)
25
15
 
26
- task :default => :spec
27
- task :test => :spec
16
+ task default: :spec
17
+ task test: :spec
data/lib/multi_mime.rb CHANGED
@@ -1,11 +1,13 @@
1
1
  module MultiMime
2
2
  extend self
3
3
 
4
+ ADAPTERS = %i(mime_types rack_mime mimemagic mime_type)
5
+
4
6
  REQUIREMENT_MAP = [
5
- ["mime/types", :mime_types],
6
- ["mimemagic", :mimemagic],
7
- ["action_dispatch/http/mime_type", :mime_type],
8
- ["rack/mime", :rack_mime]
7
+ [:mime_types, 'mime/types', :MIME].freeze,
8
+ [:mimemagic, 'mimemagic', :MimeMagic].freeze,
9
+ [:mime_type, 'action_dispatch/http/mime_type', :Mime].freeze,
10
+ [:rack_mime, 'rack/mime', :Rack].freeze
9
11
  ]
10
12
 
11
13
  # The default adapter based on what you currently
@@ -18,7 +20,7 @@ module MultiMime
18
20
  return :mime_type if defined?(::Mime::Type)
19
21
  return :rack_mime if defined?(::Rack::Mime)
20
22
 
21
- REQUIREMENT_MAP.each do |library, adapter|
23
+ REQUIREMENT_MAP.each do |adapter, library, clazz|
22
24
  begin
23
25
  require library
24
26
  return adapter
@@ -27,16 +29,15 @@ module MultiMime
27
29
  end
28
30
  end
29
31
 
30
- Kernel.warn "[WARNING] MultiMime hasn't been able to detect an adapter"
32
+ Kernel.warn '[WARNING] MultiMime hasn\'t been able to detect an adapter'
31
33
 
32
34
  nil
33
-
34
35
  end
35
36
 
36
37
  # Get the current adapter class.
37
38
  def adapter
38
39
  return @adapter if defined?(@adapter) && @adapter
39
- self.use default_adapter # load default adapter
40
+ use default_adapter # load default adapter
40
41
  @adapter
41
42
  end
42
43
 
@@ -50,8 +51,12 @@ module MultiMime
50
51
  def use(new_adapter)
51
52
  @adapter = load_adapter(new_adapter)
52
53
  end
53
- alias :adapter= :use
54
+ alias_method :adapter=, :use
54
55
 
56
+ # Remove the currently loaded adapter
57
+ def reset_adapter
58
+ remove_instance_variable :@adapter if defined?(@adapter)
59
+ end
55
60
 
56
61
  # Get mime type by mime type
57
62
  #
@@ -59,12 +64,12 @@ module MultiMime
59
64
  # @param [Hash] opts
60
65
  # * adapter [String] If set, the selected adapter will be used for this call.
61
66
  # @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
67
+ def type_for(mime_type, opts = {})
68
+ fail ArgumentError, "Mime Type must be a String. #{mime_type.inspect} given." unless mime_type.is_a? String
64
69
  adapter = current_adapter(opts)
65
70
  adapter.type_for(mime_type, opts)
66
71
  end
67
- alias :by_type :type_for
72
+ alias_method :by_type, :type_for
68
73
 
69
74
  # Get mime type by extension
70
75
  #
@@ -72,12 +77,12 @@ module MultiMime
72
77
  # @param [Hash] opts
73
78
  # * adapter [String] If set, the selected adapter will be used for this call.
74
79
  # @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
80
+ def type_for_extension(extension, opts = {})
81
+ fail ArgumentError, "Extension must be a String. #{extension.inspect} given." unless extension.is_a? String
77
82
  adapter = current_adapter(opts)
78
83
  adapter.type_for_extension(extension, opts)
79
84
  end
80
- alias :by_extension :type_for_extension
85
+ alias_method :by_extension, :type_for_extension
81
86
 
82
87
  # Get mime type by path
83
88
  #
@@ -85,12 +90,12 @@ module MultiMime
85
90
  # @param [Hash] opts
86
91
  # * adapter [String] If set, the selected adapter will be used for this call.
87
92
  # @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
93
+ def type_for_path(path, opts = {})
94
+ fail ArgumentError, "Path must be a String. #{path.inspect} given." unless path.is_a? String
90
95
  adapter = current_adapter(opts)
91
96
  adapter.type_for_path(path, opts)
92
97
  end
93
- alias :by_path :type_for_path
98
+ alias_method :by_path, :type_for_path
94
99
 
95
100
  # Get mime type by file
96
101
  #
@@ -98,45 +103,55 @@ module MultiMime
98
103
  # @param [Hash] opts
99
104
  # * adapter [String] If set, the selected adapter will be used for this call.
100
105
  # @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
106
+ def type_for_file(file, opts = {})
107
+ fail ArgumentError, "File must be a File. #{file.inspect} given." unless file.is_a? File
103
108
  adapter = current_adapter(opts)
104
109
  adapter.type_for_file(file, opts)
105
110
  end
106
- alias :by_file :type_for_file
111
+ alias_method :by_file, :type_for_file
107
112
 
108
113
  private
109
114
 
110
- #
111
- def current_adapter(opts={})
115
+ def current_adapter(opts = {})
112
116
  if new_adapter = opts.delete(:adapter)
113
117
  load_adapter(new_adapter)
114
118
  else
115
- self.adapter
119
+ adapter
116
120
  end
117
121
  end
118
122
 
119
- #
120
123
  def load_adapter(new_adapter)
121
124
  case new_adapter
122
125
  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("")}")
126
+ adapter_clazz = nil
127
+ REQUIREMENT_MAP.each do |adapter, library, clazz|
128
+ if new_adapter.to_sym == adapter
129
+ require library
130
+ require "multi_mime/adapters/#{new_adapter.to_s}"
131
+ adapter_clazz = MultiMime::Adapters.const_get(:"#{new_adapter.to_s.split('_').map { |s| s.capitalize }.join('')}")
132
+ else
133
+ next
134
+ end
135
+ end
136
+ if adapter_clazz.nil?
137
+ Kernel.warn '[WARNING] MultiMime hasn\'t been able to detect an adapter'
138
+ load_adapter nil
139
+ else
140
+ return adapter_clazz
141
+ end
125
142
  when NilClass, FalseClass
126
- load_adapter self.default_adapter
143
+ load_adapter default_adapter
127
144
  when Class, Module
128
145
  new_adapter
129
146
  else
130
- raise "Did not recognize your adapter specification. Please specify either a symbol or a class."
147
+ fail 'Did not recognize your adapter specification. Please specify either a symbol or a class.'
131
148
  end
132
149
  end
133
150
 
134
- #
135
151
  def with_adapter(new_adapter)
136
152
  old_adapter, self.adapter = adapter, new_adapter
137
153
  yield
138
154
  ensure
139
155
  self.adapter = old_adapter
140
156
  end
141
-
142
- end
157
+ end