Class: Deas::Runner::SendFileBody
- Inherits:
-
Object
- Object
- Deas::Runner::SendFileBody
- Defined in:
- lib/deas/runner.rb
Constant Summary collapse
- CHUNK_SIZE =
this class borrows from the body range handling in Rack::File.
(8*1024).freeze
Instance Attribute Summary collapse
-
#content_range ⇒ Object
readonly
Returns the value of attribute content_range.
-
#path_name ⇒ Object
readonly
Returns the value of attribute path_name.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #==(other_body) ⇒ Object
- #each ⇒ Object
-
#initialize(env, path_name) ⇒ SendFileBody
constructor
A new instance of SendFileBody.
- #inspect ⇒ Object
- #partial? ⇒ Boolean
- #range_begin ⇒ Object
- #range_end ⇒ Object
Constructor Details
#initialize(env, path_name) ⇒ SendFileBody
Returns a new instance of SendFileBody.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/deas/runner.rb', line 214 def initialize(env, path_name) @path_name = path_name file_size = @path_name.size? || Rack::Utils.bytesize(path_name.read) ranges = Rack::Utils.byte_ranges(env, file_size) if ranges.nil? || ranges.empty? || ranges.length > 1 # No ranges or multiple ranges are not supported @range = 0..file_size-1 @content_range = nil else # single range @range = ranges[0] @content_range = "bytes #{@range.begin}-#{@range.end}/#{file_size}" end @size = self.range_end - self.range_begin + 1 end |
Instance Attribute Details
#content_range ⇒ Object (readonly)
Returns the value of attribute content_range.
212 213 214 |
# File 'lib/deas/runner.rb', line 212 def content_range @content_range end |
#path_name ⇒ Object (readonly)
Returns the value of attribute path_name.
212 213 214 |
# File 'lib/deas/runner.rb', line 212 def path_name @path_name end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
212 213 214 |
# File 'lib/deas/runner.rb', line 212 def size @size end |
Instance Method Details
#==(other_body) ⇒ Object
259 260 261 262 263 |
# File 'lib/deas/runner.rb', line 259 def ==(other_body) self.path_name.to_s == other_body.path_name.to_s && self.range_begin == other_body.range_begin && self.range_end == other_body.range_end end |
#each ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/deas/runner.rb', line 239 def each @path_name.open("rb") do |io| io.seek(@range.begin) remaining_len = self.size while remaining_len > 0 part = io.read([CHUNK_SIZE, remaining_len].min) break if part.nil? remaining_len -= part.length yield part end end end |
#inspect ⇒ Object
253 254 255 256 257 |
# File 'lib/deas/runner.rb', line 253 def inspect "#<#{self.class}:#{'0x0%x' % (self.object_id << 1)} " \ "path=#{self.path_name} " \ "range_begin=#{self.range_begin} range_end=#{self.range_end}>" end |
#partial? ⇒ Boolean
232 233 234 |
# File 'lib/deas/runner.rb', line 232 def partial? !@content_range.nil? end |
#range_begin ⇒ Object
236 |
# File 'lib/deas/runner.rb', line 236 def range_begin; @range.begin; end |
#range_end ⇒ Object
237 |
# File 'lib/deas/runner.rb', line 237 def range_end; @range.end; end |