Class: Protocol::HTTP::Header::Range::ByteRange

Inherits:
Struct
  • Object
show all
Defined in:
lib/protocol/http/header/range.rb

Overview

Represents one byte-range-spec or suffix-byte-range-spec.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#firstObject

Returns the value of attribute first

Returns:

  • (Object)

    the current value of first



22
23
24
# File 'lib/protocol/http/header/range.rb', line 22

def first
  @first
end

#lastObject

Returns the value of attribute last

Returns:

  • (Object)

    the current value of last



22
23
24
# File 'lib/protocol/http/header/range.rb', line 22

def last
  @last
end

Class Method Details

.parse(value) ⇒ Object

Parse one byte range.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/protocol/http/header/range.rb', line 26

def self.parse(value)
  unless match = BYTE_RANGE.match(value)
    raise ParseError, "Invalid byte range: #{value.inspect}"
  end
  
  if suffix = match[:suffix]
    return self.new(nil, Integer(suffix))
  else
    first = Integer(match[:first])
    last = match[:last]
    last = last.empty? ? nil : Integer(last)
    
    if last && last < first
      raise ParseError, "Invalid byte range: #{value.inspect}"
    end
    
    return self.new(first, last)
  end
end

Instance Method Details

#resolve(size) ⇒ Object

Resolve this byte range against the selected representation size.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/protocol/http/header/range.rb', line 49

def resolve(size)
  if first
    if first < size
      return first..[last || size - 1, size - 1].min
    end
  elsif last > 0 && size > 0
    return [0, size - last].max..size - 1
  end
  
  return nil
end

#to_sObject

Convert this byte range to its wire representation.



63
64
65
66
67
68
69
# File 'lib/protocol/http/header/range.rb', line 63

def to_s
  if first
    "#{first}-#{last}"
  else
    "-#{last}"
  end
end