Class: Dassets::Server::Response::Body

Inherits:
Object
  • Object
show all
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)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, asset_file) ⇒ Body

Returns a new instance of Body.



58
59
60
61
62
# File 'lib/dassets/server/response.rb', line 58

def initialize(env, asset_file)
  @asset_file = asset_file
  @range, @content_range = get_range_info(env, @asset_file)
  @size = range_end - range_begin + 1
end

Instance Attribute Details

#asset_fileObject (readonly)

Returns the value of attribute asset_file.



56
57
58
# File 'lib/dassets/server/response.rb', line 56

def asset_file
  @asset_file
end

#content_rangeObject (readonly)

Returns the value of attribute content_range.



56
57
58
# File 'lib/dassets/server/response.rb', line 56

def content_range
  @content_range
end

#sizeObject (readonly)

Returns the value of attribute size.



56
57
58
# File 'lib/dassets/server/response.rb', line 56

def size
  @size
end

Instance Method Details

#==(other) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/dassets/server/response.rb', line 96

def ==(other)
  if other.is_a?(self.class)
    asset_file  == other.asset_file  &&
    range_begin == other.range_begin &&
    range_end   == other.range_end
  else
    super
  end
end

#eachObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dassets/server/response.rb', line 76

def each
  StringIO.open(@asset_file.content, "rb") do |io|
    io.seek(@range.begin)
    remaining_len = 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

#inspectObject



90
91
92
93
94
# File 'lib/dassets/server/response.rb', line 90

def inspect
  "#<#{self.class}:#{"0x0%x" % (object_id << 1)} " \
    "digest_path=#{asset_file.digest_path} " \
    "range_begin=#{range_begin} range_end=#{range_end}>"
end

#partial?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/dassets/server/response.rb', line 64

def partial?
  !@content_range.nil?
end

#range_beginObject



68
69
70
# File 'lib/dassets/server/response.rb', line 68

def range_begin
  @range.begin
end

#range_endObject



72
73
74
# File 'lib/dassets/server/response.rb', line 72

def range_end
  @range.end
end