Module: Plum::FrameFactory

Included in:
Frame
Defined in:
lib/plum/frame_factory.rb

Instance Method Summary collapse

Instance Method Details

#continuation(stream_id, payload, *flags) ⇒ Object

Creates a CONTINUATION frame.

Parameters:

  • stream_id (Integer)

    The stream ID.

  • payload (String)

    Payload.

  • flags (Array<Symbol>)

    Flags.



83
84
85
# File 'lib/plum/frame_factory.rb', line 83

def continuation(stream_id, payload, *flags)
  Frame.new(type: :continuation, stream_id: stream_id, flags: flags, payload: payload)
end

#data(stream_id, payload, *flags) ⇒ Object

Creates a DATA frame.

Parameters:

  • stream_id (Integer)

    The stream ID.

  • payload (String)

    Payload.

  • flags (Array<Symbol>)

    Flags.



56
57
58
# File 'lib/plum/frame_factory.rb', line 56

def data(stream_id, payload, *flags)
  Frame.new(type: :data, stream_id: stream_id, flags: flags, payload: payload)
end

#goaway(last_id, error_type, message = "") ⇒ Object

Creates a GOAWAY frame.

Parameters:

  • last_id (Integer)

    The biggest processed stream ID.

  • error_type (Symbol)

    The error type defined in RFC 7540 Section 7.

  • message (String) (defaults to: "")

    Additional debug data.

See Also:

  • 7540 Section 6.8


18
19
20
21
22
23
# File 'lib/plum/frame_factory.rb', line 18

def goaway(last_id, error_type, message = "")
  payload = "".push_uint32((last_id || 0) | (0 << 31))
              .push_uint32(HTTPError::ERROR_CODES[error_type])
              .push(message)
  Frame.new(type: :goaway, stream_id: 0, payload: payload)
end

#headers(stream_id, encoded, *flags) ⇒ Object

Creates a DATA frame.

Parameters:

  • stream_id (Integer)

    The stream ID.

  • encoded (String)

    Headers.

  • flags (Array<Symbol>)

    Flags.



64
65
66
# File 'lib/plum/frame_factory.rb', line 64

def headers(stream_id, encoded, *flags)
  Frame.new(type: :headers, stream_id: stream_id, flags: flags, payload: encoded)
end

#ping(ack, payload) ⇒ Object #ping(payload = "plum\x00\x00\x00\x00") ⇒ Object

Creates a PING frame.

Overloads:

  • #ping(ack, payload) ⇒ Object

    Parameters:

    • ack (Symbol)

      Pass :ack to create an ACK frame.

    • payload (String)

      8 bytes length data to send.

  • #ping(payload = "plum\x00\x00\x00\x00") ⇒ Object

    Parameters:

    • payload (String) (defaults to: "plum\x00\x00\x00\x00")

      8 bytes length data to send.



43
44
45
46
47
48
49
50
# File 'lib/plum/frame_factory.rb', line 43

def ping(arg1 = "plum\x00\x00\x00\x00", arg2 = nil)
  if !arg2
    raise ArgumentError.new("data must be 8 octets") if arg1.bytesize != 8
    Frame.new(type: :ping, stream_id: 0, payload: arg1)
  else
    Frame.new(type: :ping, stream_id: 0, flags: [:ack], payload: arg2)
  end
end

#push_promise(stream_id, new_id, encoded, *flags) ⇒ Object

Creates a PUSH_PROMISE frame.

Parameters:

  • stream_id (Integer)

    The stream ID.

  • new_id (Integer)

    The stream ID to create.

  • encoded (String)

    Request headers.

  • flags (Array<Symbol>)

    Flags.



73
74
75
76
77
# File 'lib/plum/frame_factory.rb', line 73

def push_promise(stream_id, new_id, encoded, *flags)
  payload = "".push_uint32(0 << 31 | new_id)
              .push(encoded)
  Frame.new(type: :push_promise, stream_id: stream_id, flags: flags, payload: payload)
end

#rst_stream(stream_id, error_type) ⇒ Object

Creates a RST_STREAM frame.

Parameters:

  • stream_id (Integer)

    The stream ID.

  • error_type (Symbol)

    The error type defined in RFC 7540 Section 7.



8
9
10
11
# File 'lib/plum/frame_factory.rb', line 8

def rst_stream(stream_id, error_type)
  payload = "".push_uint32(HTTPError::ERROR_CODES[error_type])
  Frame.new(type: :rst_stream, stream_id: stream_id, payload: payload)
end

#settings(ack = nil, **args) ⇒ Object

Creates a SETTINGS frame.

Parameters:

  • ack (Symbol) (defaults to: nil)

    Pass :ack to create an ACK frame.

  • args (Hash<Symbol, Integer>)

    The settings values to send.



28
29
30
31
32
33
34
35
# File 'lib/plum/frame_factory.rb', line 28

def settings(ack = nil, **args)
  payload = args.inject("") {|payload, (key, value)|
    id = Frame::SETTINGS_TYPE[key] or raise ArgumentError.new("invalid settings type")
    payload.push_uint16(id)
    payload.push_uint32(value)
  }
  Frame.new(type: :settings, stream_id: 0, flags: [ack], payload: payload)
end