Class: Utopia::Middleware::Static::FileReader

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/middleware/static.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileReader

Returns a new instance of FileReader.



48
49
50
51
# File 'lib/utopia/middleware/static.rb', line 48

def initialize(path)
	@path = path
	@etag = Digest::SHA1.hexdigest("#{File.size(@path)}#{mtime_date}")
end

Instance Attribute Details

#etagObject (readonly)

Returns the value of attribute etag.



54
55
56
# File 'lib/utopia/middleware/static.rb', line 54

def etag
  @etag
end

#pathObject (readonly)

Returns the value of attribute path.



53
54
55
# File 'lib/utopia/middleware/static.rb', line 53

def path
  @path
end

Instance Method Details

#eachObject



68
69
70
71
72
73
74
# File 'lib/utopia/middleware/static.rb', line 68

def each
	File.open(@path, "rb") do |fp|
		while part = fp.read(8192)
			yield part
		end
	end
end

#modified?(env) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/utopia/middleware/static.rb', line 76

def modified?(env)
	if modified_since = env['HTTP_IF_MODIFIED_SINCE']
		return false if File.mtime(@path) <= Time.parse(modified_since)
	end

	if etags = env['HTTP_IF_NONE_MATCH']
		etags = etags.split(/\s*,\s*/)
		return false if etags.include?(etag) || etags.include?('*')
	end

	return true
end

#mtime_dateObject



60
61
62
# File 'lib/utopia/middleware/static.rb', line 60

def mtime_date
	File.mtime(@path).httpdate
end

#sizeObject



64
65
66
# File 'lib/utopia/middleware/static.rb', line 64

def size
	File.size(@path)
end

#to_pathObject



56
57
58
# File 'lib/utopia/middleware/static.rb', line 56

def to_path
	@path
end