|
| 1 | +require 'ostruct' |
| 2 | + |
| 3 | +# Module DataModels provides JSON format based serialized and deserialized |
| 4 | +# objects that ensures tight coupling |
| 5 | +# @author Piyush Wani <[email protected]> |
| 6 | +# |
| 7 | +module DataModels |
| 8 | + # Returns a data model for an acknowledgment event |
| 9 | + # |
| 10 | + # @param [String] error An acknowledgment event |
| 11 | + # @param [String] data A data object |
| 12 | + # @param [Integer] cid A remote counter id |
| 13 | + # |
| 14 | + # @return [Hash] An acknowledgement object |
| 15 | + # |
| 16 | + def get_ack_object(error, data, cid) |
| 17 | + OpenStruct.new( |
| 18 | + cid: cid, |
| 19 | + data: data, |
| 20 | + error: error |
| 21 | + ).to_h |
| 22 | + end |
| 23 | + |
| 24 | + # Returns data model for an emitter event |
| 25 | + # |
| 26 | + # @param [String] event An emit event |
| 27 | + # @param [Hash] object The data to be sent |
| 28 | + # |
| 29 | + # @return [Hash] An emit object |
| 30 | + # |
| 31 | + def get_emit_object(event, object) |
| 32 | + OpenStruct.new( |
| 33 | + event: event, |
| 34 | + data: object |
| 35 | + ).to_h |
| 36 | + end |
| 37 | + |
| 38 | + # Returns data model for emitter acknowledgment event |
| 39 | + # |
| 40 | + # @param [String] event An emitter acknowledgment event |
| 41 | + # @param [Hash] object The data to be sent |
| 42 | + # @param [Integer] counter A counter |
| 43 | + # |
| 44 | + # @return [Hash] An emitter acknowledgment object |
| 45 | + # |
| 46 | + def get_emit_ack_object(event, object, counter) |
| 47 | + OpenStruct.new( |
| 48 | + event: event, |
| 49 | + data: object, |
| 50 | + c_id: counter |
| 51 | + ).to_h |
| 52 | + end |
| 53 | + |
| 54 | + # Returns data model for handshake event |
| 55 | + # |
| 56 | + # @param [Integer] counter A counter for event |
| 57 | + # |
| 58 | + # @return [Hash] A handshake object |
| 59 | + # |
| 60 | + def get_handshake_object(counter) |
| 61 | + OpenStruct.new( |
| 62 | + cid: counter, |
| 63 | + data: OpenStruct.new( |
| 64 | + authToken: @auth_token |
| 65 | + ).to_h, |
| 66 | + event: '#handshake' |
| 67 | + ).to_h |
| 68 | + end |
| 69 | + |
| 70 | + # Returns data model for publish and publish with acknowledgment event |
| 71 | + # |
| 72 | + # @param [String] channel A channel where data should be published |
| 73 | + # @param [String] data The data to be published |
| 74 | + # @param [Integer] counter A counter for event |
| 75 | + # |
| 76 | + # @return [Hash] A publisher object |
| 77 | + # |
| 78 | + def get_publisher_object(channel, data, counter) |
| 79 | + OpenStruct.new( |
| 80 | + cid: counter, |
| 81 | + data: OpenStruct.new( |
| 82 | + channel: channel, |
| 83 | + data: data |
| 84 | + ).to_h, |
| 85 | + event: '#publish' |
| 86 | + ).to_h |
| 87 | + end |
| 88 | + |
| 89 | + # Returns data model for subscribe and subscribe with acknowledgment event |
| 90 | + # |
| 91 | + # @param [String] channel A channel to subscribe |
| 92 | + # @param [Integer] counter A counter for event |
| 93 | + # |
| 94 | + # @return [Hash] A subscribe object |
| 95 | + # |
| 96 | + def get_subscribe_object(channel, counter) |
| 97 | + OpenStruct.new( |
| 98 | + event: '#subscribe', |
| 99 | + data: OpenStruct.new( |
| 100 | + channel: channel |
| 101 | + ).to_h, |
| 102 | + cid: counter |
| 103 | + ).to_h |
| 104 | + end |
| 105 | + |
| 106 | + # Returns data model for unsubscribe and unsubscribe with acknowledgment event |
| 107 | + # |
| 108 | + # @param [String] channel A channel to unsubscribe |
| 109 | + # @param [Integer] counter A counter for event |
| 110 | + # |
| 111 | + # @return [Hash] A unsubscribe object |
| 112 | + # |
| 113 | + def get_unsubscribe_object(channel, counter) |
| 114 | + OpenStruct.new( |
| 115 | + event: '#unsubscribe', |
| 116 | + data: channel, |
| 117 | + cid: counter |
| 118 | + ).to_h |
| 119 | + end |
| 120 | +end |
0 commit comments