Skip to content

Commit 3bbef2f

Browse files
authored
Merge pull request #291 from SciRuby/stop-using-mimemagic
Stop using mimemagic
2 parents 5bd57ec + 230b916 commit 3bbef2f

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

iruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Gem::Specification.new do |s|
2020
s.add_dependency 'bond', '~> 0.5'
2121
s.add_dependency 'data_uri', '~> 0.1'
2222
s.add_dependency 'ffi-rzmq'
23-
s.add_dependency 'mimemagic', '~> 0.3'
23+
s.add_dependency 'mime-types', '>= 3.3.1'
2424
s.add_dependency 'multi_json', '~> 1.11'
2525

2626
s.add_development_dependency 'pycall', '>= 1.2.1'

lib/iruby.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'mimemagic'
1+
require 'mime/types'
22
require 'multi_json'
33
require 'securerandom'
44
require 'openssl'

lib/iruby/display.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def clear_output(wait = false)
4444
private
4545

4646
def protect(mime, data)
47-
MimeMagic.new(mime).text? ? data.to_s : [data.to_s].pack('m0')
47+
MIME::Type.new(mime).ascii? ? data.to_s : [data.to_s].pack('m0')
4848
end
4949

5050
def render(data, obj, exact_mime, fuzzy_mime)
@@ -304,7 +304,7 @@ def format(mime = nil, &block)
304304

305305
match { |obj| obj.respond_to?(:path) && obj.method(:path).arity == 0 && File.readable?(obj.path) }
306306
format do |obj|
307-
mime = MimeMagic.by_path(obj.path).to_s
307+
mime = MIME::Types.of(obj.path).first.to_s
308308
[mime, File.read(obj.path)] if SUPPORTED_MIMES.include?(mime)
309309
end
310310

test/iruby/mime_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class IRubyTest::MimeTest < IRubyTest::TestBase
2+
sub_test_case("IRuby::Display") do
3+
def test_display_with_mime_type
4+
html = "<b>Bold Text</b>"
5+
6+
obj = Object.new
7+
obj.define_singleton_method(:to_s) { html }
8+
9+
res = IRuby::Display.display(obj, mime: "text/html")
10+
assert_equal({ plain: obj.inspect, html: html },
11+
{ plain: res["text/plain"], html: res["text/html"] })
12+
end
13+
end
14+
15+
sub_test_case("Rendering a file") do
16+
def setup
17+
@html = "<b>Bold Text</b>"
18+
Dir.mktmpdir do |tmpdir|
19+
@file = File.join(tmpdir, "test.html")
20+
File.write(@file, @html)
21+
yield
22+
end
23+
end
24+
25+
def test_display
26+
File.open(@file, "rb") do |f|
27+
res = IRuby::Display.display(f)
28+
assert_equal(@html, res["text/html"])
29+
end
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)