Class: Utopia::Middleware::Static::FileReader
- Inherits:
-
Object
- Object
- Utopia::Middleware::Static::FileReader
- Defined in:
- lib/utopia/middleware/static.rb
Instance Attribute Summary collapse
-
#etag ⇒ Object
readonly
Returns the value of attribute etag.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(path) ⇒ FileReader
constructor
A new instance of FileReader.
- #modified?(env) ⇒ Boolean
- #mtime_date ⇒ Object
- #size ⇒ Object
- #to_path ⇒ Object
Constructor Details
#initialize(path) ⇒ FileReader
Returns a new instance of FileReader.
49 50 51 52 |
# File 'lib/utopia/middleware/static.rb', line 49 def initialize(path) @path = path @etag = Digest::SHA1.hexdigest("#{File.size(@path)}#{mtime_date}") end |
Instance Attribute Details
#etag ⇒ Object (readonly)
Returns the value of attribute etag.
55 56 57 |
# File 'lib/utopia/middleware/static.rb', line 55 def etag @etag end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
54 55 56 |
# File 'lib/utopia/middleware/static.rb', line 54 def path @path end |
Instance Method Details
#each ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/utopia/middleware/static.rb', line 69 def each File.open(@path, "rb") do |fp| while part = fp.read(8192) yield part end end end |
#modified?(env) ⇒ Boolean
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/utopia/middleware/static.rb', line 77 def modified?(env) if modified_since = env['HTTP_IF_MODIFIED_SINCE'] return false if File.mtime(@path) <= Time.parse(modified_since) end if = env['HTTP_IF_NONE_MATCH'] = .split(/\s*,\s*/) return false if .include?(etag) || .include?('*') end return true end |
#mtime_date ⇒ Object
61 62 63 |
# File 'lib/utopia/middleware/static.rb', line 61 def mtime_date File.mtime(@path).httpdate end |
#size ⇒ Object
65 66 67 |
# File 'lib/utopia/middleware/static.rb', line 65 def size File.size(@path) end |
#to_path ⇒ Object
57 58 59 |
# File 'lib/utopia/middleware/static.rb', line 57 def to_path @path end |