Class: Dassets::Server::Response::Body
- Inherits:
-
Object
- Object
- Dassets::Server::Response::Body
- Defined in:
- lib/dassets/server/response.rb
Overview
This class borrows from the body range handling in Rack::File and adapts it for use with Dasset’s asset files and their generic string content.
Constant Summary collapse
- CHUNK_SIZE =
8k
(8*1024).freeze
Instance Attribute Summary collapse
-
#asset_file ⇒ Object
readonly
Returns the value of attribute asset_file.
-
#content_range ⇒ Object
readonly
Returns the value of attribute content_range.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #==(other_body) ⇒ Object
- #each ⇒ Object
-
#initialize(env, asset_file) ⇒ Body
constructor
A new instance of Body.
- #inspect ⇒ Object
- #partial? ⇒ Boolean
- #range_begin ⇒ Object
- #range_end ⇒ Object
Constructor Details
#initialize(env, asset_file) ⇒ Body
Returns a new instance of Body.
55 56 57 58 59 |
# File 'lib/dassets/server/response.rb', line 55 def initialize(env, asset_file) @asset_file = asset_file @range, @content_range = get_range_info(env, @asset_file) @size = self.range_end - self.range_begin + 1 end |
Instance Attribute Details
#asset_file ⇒ Object (readonly)
Returns the value of attribute asset_file.
53 54 55 |
# File 'lib/dassets/server/response.rb', line 53 def asset_file @asset_file end |
#content_range ⇒ Object (readonly)
Returns the value of attribute content_range.
53 54 55 |
# File 'lib/dassets/server/response.rb', line 53 def content_range @content_range end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
53 54 55 |
# File 'lib/dassets/server/response.rb', line 53 def size @size end |
Instance Method Details
#==(other_body) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/dassets/server/response.rb', line 93 def ==(other_body) if other_body.is_a?(self.class) self.asset_file == other_body.asset_file && self.range_begin == other_body.range_begin && self.range_end == other_body.range_end else super end end |
#each ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/dassets/server/response.rb', line 73 def each StringIO.open(@asset_file.content, "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
87 88 89 90 91 |
# File 'lib/dassets/server/response.rb', line 87 def inspect "#<#{self.class}:#{"0x0%x" % (self.object_id << 1)} " \ "digest_path=#{self.asset_file.digest_path} " \ "range_begin=#{self.range_begin} range_end=#{self.range_end}>" end |
#partial? ⇒ Boolean
61 62 63 |
# File 'lib/dassets/server/response.rb', line 61 def partial? !@content_range.nil? end |
#range_begin ⇒ Object
65 66 67 |
# File 'lib/dassets/server/response.rb', line 65 def range_begin @range.begin end |
#range_end ⇒ Object
69 70 71 |
# File 'lib/dassets/server/response.rb', line 69 def range_end @range.end end |