Class: Cucumber::Wire::DataPacket

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/wire/data_packet.rb

Overview

Represents the packet of data sent over the wire as JSON data, containing a message and a hash of arguments

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, params = nil) ⇒ DataPacket

Returns a new instance of DataPacket.



21
22
23
24
# File 'lib/cucumber/wire/data_packet.rb', line 21

def initialize(message, params = nil)
  @message = message
  @params = params
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



19
20
21
# File 'lib/cucumber/wire/data_packet.rb', line 19

def message
  @message
end

#paramsObject (readonly)

Returns the value of attribute params.



19
20
21
# File 'lib/cucumber/wire/data_packet.rb', line 19

def params
  @params
end

Class Method Details

.parse(raw) ⇒ Object



11
12
13
14
15
16
# File 'lib/cucumber/wire/data_packet.rb', line 11

def parse(raw)
  attributes = JSON.parse(raw.strip)
  message = attributes[0]
  params  = attributes[1]
  new(message, params)
end

Instance Method Details

#handle_with(handler) ⇒ Object



32
33
34
# File 'lib/cucumber/wire/data_packet.rb', line 32

def handle_with(handler)
  handler.send(:"handle_#{@message}", @params)
end

#to_json(*_args) ⇒ Object



26
27
28
29
30
# File 'lib/cucumber/wire/data_packet.rb', line 26

def to_json(*_args)
  packet = [@message]
  packet << @params if @params
  JSON.generate(packet)
end