lib/yomu.rb in yomu-0.1.10 vs lib/yomu.rb in yomu-0.2.0
- old
+ new
@@ -1,10 +1,10 @@
require 'yomu/version'
require 'net/http'
require 'mime/types'
-require 'yaml'
+require 'json'
class Yomu
GEMPATH = File.dirname(File.dirname(__FILE__))
JARPATH = File.join(Yomu::GEMPATH, 'jar', 'tika-app-1.5.jar')
@@ -19,15 +19,15 @@
when :text
'-t'
when :html
'-h'
when :metadata
- '-m'
+ '-m -j'
when :mimetype
- '-m'
+ '-m -j'
end
-
+
result = IO.popen "#{java} -Djava.awt.headless=true -jar #{Yomu::JARPATH} #{switch}", 'r+' do |io|
io.write data
io.close_write
io.read
end
@@ -36,13 +36,13 @@
when :text
result
when :html
result
when :metadata
- YAML.load quote(result)
+ JSON.parse(result)
when :mimetype
- MIME::Types[YAML.load(quote(result))['Content-Type']].first
+ MIME::Types[JSON.parse(result)['Content-Type']].first
end
end
# Create a new instance of Yomu with a given document.
#
@@ -58,14 +58,14 @@
#
# Yomu.new File.open('sample.pages')
def initialize(input)
if input.is_a? String
- if input =~ URI::regexp
- @uri = URI.parse input
- elsif File.exists? input
+ if File.exists? input
@path = input
+ elsif input =~ URI::regexp
+ @uri = URI.parse input
else
raise Errno::ENOENT.new "missing file or invalid URI - #{input}"
end
elsif input.respond_to? :read
@stream = input
@@ -89,13 +89,13 @@
#
# yomu = Yomu.new 'sample.pages'
# yomu.html
def html
- return @text if defined? @text
+ return @html if defined? @html
- @text = Yomu.read :html, data
+ @html = Yomu.read :html, data
end
# Returns the metadata hash of the Yomu document.
#
# yomu = Yomu.new 'sample.pages'
@@ -114,18 +114,32 @@
# yomu.mimetype.extensions #=> ['docx']
def mimetype
return @mimetype if defined? @mimetype
- @mimetype = MIME::Types[metadata['Content-Type']].first
+ type = metadata["Content-Type"].is_a?(Array) ? metadata["Content-Type"].first : metadata["Content-Type"]
+
+ @mimetype = MIME::Types[type].first
end
# Returns +true+ if the Yomu document was specified using a file path.
#
# yomu = Yomu.new 'sample.pages'
# yomu.path? #=> true
+
+ def creation_date
+ return @creation_date if defined? @creation_date
+
+ if metadata['Creation-Date']
+ @creation_date = Time.parse(metadata['Creation-Date'])
+ else
+ nil
+ end
+ end
+
+
def path?
defined? @path
end
# Returns +true+ if the Yomu document was specified using a URI.
@@ -163,14 +177,9 @@
@data = @stream.read
end
@data
end
-
- def self.quote(metadata)
- metadata.gsub(/: (.*: .*)$/, ': "\1"')
- end
- private_class_method :quote
def self.java
ENV['JAVA_HOME'] ? ENV['JAVA_HOME'] + '/bin/java' : 'java'
end
private_class_method :java