lib/yomu.rb in yomu-0.1.0 vs lib/yomu.rb in yomu-0.1.1
- old
+ new
@@ -1,6 +1,6 @@
-require "yomu/version"
+require 'yomu/version'
require 'net/http'
require 'yaml'
class Yomu
@@ -44,15 +44,16 @@
#
# Yomu.new File.open('sample.pages')
def initialize(input)
if input.is_a? String
- uri = URI.parse input
- if uri.scheme and uri.host
- @uri = uri
- else
+ if input =~ URI::regexp
+ @uri = URI.parse input
+ elsif File.exists? input
@path = input
+ else
+ raise Errno::ENOENT.new "missing file or invalid URI - #{input}"
end
elsif input.respond_to? :read
@stream = input
else
raise TypeError.new "can't read from #{input.class.name}"
@@ -79,19 +80,31 @@
return @metadata if defined? @metadata
@metadata = Yomu.read :metadata, data
end
+ def path?
+ defined? @path
+ end
+
+ def uri?
+ defined? @uri
+ end
+
+ def stream?
+ defined? @stream
+ end
+
protected
def data
return @data if defined? @data
- if defined? @path
+ if path?
@data = File.read @path
- elsif defined? @uri
+ elsif uri?
@data = Net::HTTP.get @uri
- elsif defined? @stream
+ elsif stream?
@data = @stream.read
end
@data
end
\ No newline at end of file