Skip to content

Commit

Permalink
Add support for reading MIME types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Erol committed May 1, 2013
1 parent e683543 commit 26686f8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/yomu.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'yomu/version'

require 'net/http'
require 'mime/types'
require 'yaml'

class Yomu
Expand All @@ -19,6 +20,8 @@ def self.read(type, data)
'-t'
when :metadata
'-m'
when :mimetype
'-m'
end

result = IO.popen "#{java} -Djava.awt.headless=true -jar #{Yomu::JARPATH} #{switch}", 'r+' do |io|
Expand All @@ -27,9 +30,14 @@ def self.read(type, data)
io.read
end

result = enclose_metadata_fields(result)

type == :metadata ? YAML.load(result) : result
case type
when :text
result
when :metadata
YAML.load enclose_metadata_fields(result)
when :mimetype
MIME::Types[YAML.load(enclose_metadata_fields(result))['Content-Type']].first
end
end

# Create a new instance of Yomu with a given document.
Expand Down Expand Up @@ -84,6 +92,18 @@ def metadata
@metadata = Yomu.read :metadata, data
end

# Returns the mimetype object of the Yomu document.
#
# yomu = Yomu.new 'sample.docx'
# yomu.mimetype.content_type #=> 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
# yomu.mimetype.extensions #=> ['docx']

def mimetype
return @mimetype if defined? @mimetype

@mimetype = MIME::Types[metadata['Content-Type']].first
end

# Returns +true+ if the Yomu document was specified using a file path.
#
# yomu = Yomu.new 'sample.pages'
Expand Down
7 changes: 7 additions & 0 deletions test/specs/yomu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@

assert_equal 'problem: test', metadata['dc:title']
end

it 'reads mimetype' do
mimetype = Yomu.read :mimetype, data

assert_equal 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', mimetype.content_type
assert_includes mimetype.extensions, 'docx'
end
end

describe '.new' do
Expand Down
2 changes: 2 additions & 0 deletions yomu.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]
gem.version = Yomu::VERSION

gem.add_runtime_dependency 'mime-types', '~> 1.23'

gem.add_development_dependency 'minitest'
end

0 comments on commit 26686f8

Please sign in to comment.