Module: DataModels

Included in:
ScClient
Defined in:
lib/socketclusterclient/data_models.rb

Overview

Module DataModels provides JSON format based serialized and deserialized objects that ensures tight coupling

Author:

Instance Method Summary collapse

Instance Method Details

#get_ack_object(error, data, cid) ⇒ Object

Returns a data model for an acknowledgment event

Parameters:

  • error (String)

    An acknowledgment event

  • data (String)

    A data object

  • cid (Integer)

    A remote counter id

Returns:

  • An acknowledgement object



16
17
18
19
20
21
22
# File 'lib/socketclusterclient/data_models.rb', line 16

def get_ack_object(error, data, cid)
  OpenStruct.new(
    cid: cid,
    data: data,
    error: error
  ).to_h
end

#get_emit_ack_object(event, object, counter) ⇒ Object

Returns a data model for an emitter acknowledgment event

Parameters:

  • event (String)

    An emitter acknowledgment event

  • object (Hash)

    A data object

  • counter (Integer)

    A counter for a particular event

Returns:

  • An emitter acknowledgment object



46
47
48
49
50
51
52
# File 'lib/socketclusterclient/data_models.rb', line 46

def get_emit_ack_object(event, object, counter)
  OpenStruct.new(
    event: event,
    data: object,
    c_id: counter
  ).to_h
end

#get_emit_object(event, object) ⇒ Object

Returns a data model for an emitter event

Parameters:

  • event (String)

    An emit event

  • object (Hash)

    A data object

Returns:

  • An emit object



31
32
33
34
35
36
# File 'lib/socketclusterclient/data_models.rb', line 31

def get_emit_object(event, object)
  OpenStruct.new(
    event: event,
    data: object
  ).to_h
end

#get_handshake_object(counter) ⇒ Object

Returns a data model for a handshake event

Parameters:

  • counter (Integer)

    A counter for a particular event

Returns:

  • A handshake object



60
61
62
63
64
65
66
67
68
# File 'lib/socketclusterclient/data_models.rb', line 60

def get_handshake_object(counter)
  OpenStruct.new(
    cid: counter,
    data: OpenStruct.new(
      authToken: @auth_token
    ).to_h,
    event: '#handshake'
  ).to_h
end

#get_publish_object(channel, data, counter) ⇒ Object

Returns a data model for publish and publish with acknowledgment event

Parameters:

  • channel (String)

    A channel for publishing data

  • data (String)

    A data object

  • counter (Integer)

    A counter for a particular event

Returns:

  • A publish object



78
79
80
81
82
83
84
85
86
87
# File 'lib/socketclusterclient/data_models.rb', line 78

def get_publish_object(channel, data, counter)
  OpenStruct.new(
    cid: counter,
    data: OpenStruct.new(
      channel: channel,
      data: data
    ).to_h,
    event: '#publish'
  ).to_h
end

#get_subscribe_object(channel, counter) ⇒ Object

Returns a data model for subscribe and subscribe with acknowledgment event

Parameters:

  • channel (String)

    A channel to subscribe

  • counter (Integer)

    A counter for a particular event

Returns:

  • A subscribe object



96
97
98
99
100
101
102
103
104
# File 'lib/socketclusterclient/data_models.rb', line 96

def get_subscribe_object(channel, counter)
  OpenStruct.new(
    event: '#subscribe',
    data: OpenStruct.new(
      channel: channel
    ).to_h,
    cid: counter
  ).to_h
end

#get_unsubscribe_object(channel, counter) ⇒ Object

Returns a data model for unsubscribe and unsubscribe with acknowledgment event

Parameters:

  • channel (String)

    A channel to unsubscribe

  • counter (Integer)

    A counter for a particular event

Returns:

  • An unsubscribe object



113
114
115
116
117
118
119
# File 'lib/socketclusterclient/data_models.rb', line 113

def get_unsubscribe_object(channel, counter)
  OpenStruct.new(
    event: '#unsubscribe',
    data: channel,
    cid: counter
  ).to_h
end