mini_mime 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a17e3917b461405b2d2cbbc1b2d334c91cfce23c
4
- data.tar.gz: a3739e8603675783ea2bc00f76bbd06a35f91b6e
3
+ metadata.gz: 29fa488976dc79f0fb56e06c67ba6bf35bafb827
4
+ data.tar.gz: 1eeb58e612b54db1e0afd64c6482fbf907ad53fe
5
5
  SHA512:
6
- metadata.gz: 619ecef721791234a856cda1d310361b001c148f2a7f8d470fb922e591af6b2a8296ec6a630472965dfec537dc97ca20b056a2af93fb03a0a718b80c97b9450d
7
- data.tar.gz: b690f059fee37bcef80e4fa3e21151c8bf88080f60b93ed7a41f6ea8ede9174d370bfde2fff74725619cf75191c7aea60d629d4e257184ba107f35c30b7376b1
6
+ metadata.gz: 1536aebde61795c374f7734347fcc0f41c9772c2cf2994006c0af7f5e4836f587fe61101c27e3658e7a8a5577ec099e819fe70cf1a223c8691b838c5ba88e41f
7
+ data.tar.gz: 3776428c96d2584faeb2a811628269a41f9e4ab0818f9a156a58b48a5c51ee0ad59e1d1ad7f0b313d26473dab5de92ccab890ab1ecac3b9118931903df383f8c
data/CHANGELOG CHANGED
@@ -1,4 +1,9 @@
1
1
  14-12-2016
2
2
 
3
- - Version 0.1.0
4
- - Initial version
3
+ - Version 0.1.1
4
+ - Adjusted API to be more consistent
5
+
6
+ 14-12-2016
7
+
8
+ - Version 0.1.0
9
+ - Initial version
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # MiniMime
2
2
 
3
- Minimal mime type implementation for use with the mail and rest-client gem
3
+ Minimal mime type implementation for use with the mail and rest-client gem.
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,16 +20,74 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
+ ```
24
+ require 'mini_mime'
25
+
26
+ MiniMime.lookup_by_filename("a.txt").content_type
27
+ # => "text/plain"
28
+
29
+ MiniMime.lookup_by_content_type("text/plain").extension
30
+ # => "txt"
31
+
32
+ MiniMime.lookup_by_content_type("text/plain").binary?
33
+ # => false
34
+
35
+ ```
36
+
37
+ ## Performance
38
+
39
+ MiniMime is optimised to minimize memory usage. It keeps a cache of 100 mime type lookups (and 100 misses). There are benchmarks in the (bench directory)[https://github.com/discourse/mini_mime/bench/bench.rb]
40
+
41
+ ```
42
+ Memory stats for requiring mime/types/columnar
43
+ Total allocated: 9869358 bytes (109796 objects)
44
+ Total retained: 3138198 bytes (31165 objects)
45
+
46
+ Memory stats for requiring mini_mime
47
+ Total allocated: 58898 bytes (398 objects)
48
+ Total retained: 7784 bytes (62 objects)
49
+ Warming up --------------------------------------
50
+ cached content_type lookup MiniMime
51
+ 52.136k i/100ms
52
+ content_type lookup Mime::Types
53
+ 32.701k i/100ms
54
+ Calculating -------------------------------------
55
+ cached content_type lookup MiniMime
56
+ 641.305k (± 3.2%) i/s - 3.232M in 5.045630s
57
+ content_type lookup Mime::Types
58
+ 361.041k (± 1.5%) i/s - 1.831M in 5.073290s
59
+ Warming up --------------------------------------
60
+ uncached content_type lookup MiniMime
61
+ 3.333k i/100ms
62
+ content_type lookup Mime::Types
63
+ 33.177k i/100ms
64
+ Calculating -------------------------------------
65
+ uncached content_type lookup MiniMime
66
+ 33.660k (± 1.7%) i/s - 169.983k in 5.051415s
67
+ content_type lookup Mime::Types
68
+ 364.931k (± 2.8%) i/s - 1.825M in 5.004112s
69
+ ```
70
+
71
+ As a general guideline, cached lookups are 2x faster than MIME::Types equivelent. Uncached lookups are 10x slower.
72
+
23
73
 
24
74
  ## Development
25
75
 
76
+ MiniMime uses the officially maintained list of mime types at (mime-types-data)[https://github.com/mime-types/mime-types-data] repo to build the internal database.
77
+
78
+ To update the database run:
79
+
80
+ ```ruby
81
+ bundle exec rake rebuild_db
82
+ ```
83
+
26
84
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
27
85
 
28
86
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
29
87
 
30
88
  ## Contributing
31
89
 
32
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mini_mime. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
90
+ Bug reports and pull requests are welcome on GitHub at https://github.com/discourse/mini_mime. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
33
91
 
34
92
 
35
93
  ## License
data/bench/bench.rb ADDED
@@ -0,0 +1,52 @@
1
+ require 'memory_profiler'
2
+ require 'benchmark/ips'
3
+
4
+ $: << File.expand_path('../../lib', __FILE__)
5
+
6
+
7
+ puts
8
+ puts "Memory stats for requiring mime/types/columnar"
9
+ result = MemoryProfiler.report do
10
+ require 'mime/types/columnar'
11
+ end
12
+
13
+ puts "Total allocated: #{result.total_allocated_memsize} bytes (#{result.total_allocated} objects)"
14
+ puts "Total retained: #{result.total_retained_memsize} bytes (#{result.total_retained} objects)"
15
+
16
+ puts
17
+ puts "Memory stats for requiring mini_mime"
18
+ result = MemoryProfiler.report do
19
+ require 'mini_mime'
20
+ end
21
+
22
+ puts "Total allocated: #{result.total_allocated_memsize} bytes (#{result.total_allocated} objects)"
23
+ puts "Total retained: #{result.total_retained_memsize} bytes (#{result.total_retained} objects)"
24
+
25
+
26
+ Benchmark.ips do |bm|
27
+ bm.report 'cached content_type lookup MiniMime' do
28
+ MiniMime.lookup_by_filename("a.txt").content_type
29
+ end
30
+
31
+ bm.report 'content_type lookup Mime::Types' do
32
+ MIME::Types.type_for("a.txt")[0].content_type
33
+ end
34
+ end
35
+
36
+ module MiniMime
37
+ class Db
38
+ class RandomAccessDb
39
+ alias_method :lookup, :lookup_uncached
40
+ end
41
+ end
42
+ end
43
+
44
+ Benchmark.ips do |bm|
45
+ bm.report 'uncached content_type lookup MiniMime' do
46
+ MiniMime.lookup_by_filename("a.txt").content_type
47
+ end
48
+
49
+ bm.report 'content_type lookup Mime::Types' do
50
+ MIME::Types.type_for("a.txt")[0].content_type
51
+ end
52
+ end
data/lib/mini_mime.rb CHANGED
@@ -2,36 +2,18 @@ require "mini_mime/version"
2
2
  require "thread"
3
3
 
4
4
  module MiniMime
5
- BINARY_ENCODINGS = %w(base64 8bit)
6
-
7
- # return true if this filename is known to have binary encoding
8
- #
9
- # puts MiniMime.binary?("file.gif") => true
10
- # puts MiniMime.binary?("file.txt") => false
11
- def self.binary?(filename)
12
- info = Db.lookup_by_filename(filename)
13
- !!(info && BINARY_ENCODINGS.include?(info.encoding))
14
- end
15
5
 
16
- # return true if this content type is known to have binary encoding
17
- #
18
- # puts MiniMime.binary_content_type?("text/plain") => false
19
- # puts MiniMime.binary?("application/x-compress") => true
20
- def self.binary_content_type?(mime)
21
- info = Db.lookup_by_content_type(mime)
22
- !!(info && BINARY_ENCODINGS.include?(info.encoding))
6
+ def self.lookup_by_filename(filename)
7
+ Db.lookup_by_filename(filename)
23
8
  end
24
9
 
25
- # return first matching content type for a file
26
- #
27
- # puts MiniMime.content_type("test.xml") => "application/xml"
28
- # puts MiniMime.content_type("test.gif") => "image/gif"
29
- def self.content_type(filename)
30
- info = Db.lookup_by_filename(filename)
31
- info && info.content_type
10
+ def self.lookup_by_content_type(mime)
11
+ Db.lookup_by_content_type(mime)
32
12
  end
33
13
 
34
14
  class Info
15
+ BINARY_ENCODINGS = %w(base64 8bit)
16
+
35
17
  attr_accessor :extension, :content_type, :encoding
36
18
  def initialize(buffer)
37
19
  @extension,@content_type,@encoding = buffer.split(/\s+/).map!(&:freeze)
@@ -46,6 +28,10 @@ module MiniMime
46
28
  @encoding
47
29
  end
48
30
  end
31
+
32
+ def binary?
33
+ BINARY_ENCODINGS.include?(encoding)
34
+ end
49
35
  end
50
36
 
51
37
  class Db
@@ -1,3 +1,3 @@
1
1
  module MiniMime
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_mime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-13 00:00:00.000000000 Z
11
+ date: 2016-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -81,6 +81,7 @@ files:
81
81
  - LICENSE.txt
82
82
  - README.md
83
83
  - Rakefile
84
+ - bench/bench.rb
84
85
  - bin/console
85
86
  - bin/setup
86
87
  - lib/db/content_type_mime.db