File: data_packet.rb

package info (click to toggle)
ruby-cucumber-wire 0.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 224 kB
  • sloc: ruby: 754; makefile: 3
file content (34 lines) | stat: -rw-r--r-- 779 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
require 'multi_json'

module Cucumber
  module Wire
    # Represents the packet of data sent over the wire as JSON data, containing
    # a message and a hash of arguments
    class DataPacket
      class << self
        def parse(raw)
          attributes = MultiJson.load(raw.strip)
          message = attributes[0]
          params  = attributes[1]
          new(message, params)
        end
      end

      attr_reader :message, :params

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

      def to_json
        packet = [@message]
        packet << @params if @params
        MultiJson.dump(packet)
      end

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