Class: Browser::HTTP::Binary

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
opal/browser/http/binary.rb

Overview

Represents a binary result from a HTTP response.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Binary

Create a binary from a value.

Parameters:

  • value (String, Buffer)

    the binary


12
13
14
15
16
17
18
19
20
# File 'opal/browser/http/binary.rb', line 12

def initialize(value)
  if String === value
    @type = :string
    @data = value
  else
    @type = :buffer
    @data = value.to_a
  end
end

Instance Attribute Details

#lengthInteger (readonly)

Returns the length of the binary.

Returns:

  • (Integer)

    the length of the binary


53
54
55
# File 'opal/browser/http/binary.rb', line 53

def length
  @data.length
end

#type:string, :buffer (readonly)

Returns the type of binary.

Returns:

  • (:string, :buffer)

    the type of binary


7
8
9
# File 'opal/browser/http/binary.rb', line 7

def type
  @type
end

Instance Method Details

#[](index) ⇒ Integer

Access a byte from the binary.

Returns:

  • (Integer)

    a byte


47
48
49
# File 'opal/browser/http/binary.rb', line 47

def [](index)
  @type == :string ? `#@data.charCodeAt(index) & 0xff` : @data[index]
end

#each {|byte| ... } ⇒ self

Iterate over each byte in the binary.

Yields:

  • (byte)

    the byte

Returns:

  • (self)

29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'opal/browser/http/binary.rb', line 29

def each(&block)
  return enum_for :each unless block

  index  = 0
  length = self.length

  while index < length
    block.call(self[index])

    index += 1
  end

  self
end