Class: ServeByteRange::BlockWritableWithLimit

Inherits:
Object
  • Object
show all
Defined in:
lib/serve_byte_range.rb

Instance Method Summary collapse

Constructor Details

#initialize(limit, &block_that_accepts_writes) ⇒ BlockWritableWithLimit

Returns a new instance of BlockWritableWithLimit.



7
8
9
10
11
# File 'lib/serve_byte_range.rb', line 7

def initialize(limit, &block_that_accepts_writes)
  @limit = limit
  @written = 0
  @block_that_accepts_bytes = block_that_accepts_writes
end

Instance Method Details

#verify!Object



22
23
24
# File 'lib/serve_byte_range.rb', line 22

def verify!
  raise "You wrote #{@written} bytes but the range requires #{@limit} bytes" unless @written == @limit
end

#write(string) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/serve_byte_range.rb', line 13

def write(string)
  return 0 if string.empty?
  would_have_output = @written + string.bytesize
  raise "You are trying to write more than advertised - #{would_have_output} bytes, the limit for this range is #{@limit}" if @limit < would_have_output
  @written += string.bytesize
  @block_that_accepts_bytes.call(string.b)
  string.bytesize
end