Class: Orator::MiddleGround

Inherits:
Object
  • Object
show all
Defined in:
lib/orator/middle_ground.rb

Overview

This is used as a context for the client execution.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, clients) ⇒ MiddleGround

Initialize the class.



17
18
19
20
21
# File 'lib/orator/middle_ground.rb', line 17

def initialize(client, clients)
  @client  = client
  @clients = clients
  @struct  = ::Orator::OpenStruct.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

This delegates the rest of the requests to the struct.



48
49
50
51
52
# File 'lib/orator/middle_ground.rb', line 48

def method_missing(method, *args, &block)
  super unless respond_to_missing?(method)

  @struct.public_send(method, *args, &block)
end

Instance Attribute Details

#clientClient (readonly)

Provides access to the client.

Returns:



9
10
11
# File 'lib/orator/middle_ground.rb', line 9

def client
  @client
end

#clientsSet<Client> (readonly)

Provides access to the list of clients that are connected.

Returns:



14
15
16
# File 'lib/orator/middle_ground.rb', line 14

def clients
  @clients
end

Instance Method Details

#message(event, data) ⇒ Hash

This builds a message from a hash and an event.

Parameters:

  • event (String, Symbol)

    the event for the message.

  • data (Hash)

    the data for the message.

Returns:

  • (Hash)


37
38
39
40
41
42
43
44
45
# File 'lib/orator/middle_ground.rb', line 37

def message(event, data)
  new_data = { "event" => event.to_s }

  data.each do |k, v|
    new_data[k.to_s] = v
  end

  new_data
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Lets ruby know we’re doing some #method_missing magic.

Returns:

  • (Boolean)


55
56
57
# File 'lib/orator/middle_ground.rb', line 55

def respond_to_missing?(method, include_private = false)
  @struct.respond_to?(method, include_private)
end

#send(data) ⇒ void

This method returns an undefined value.

This sends a message to the client.

Parameters:

  • data (Hash)

    the data to be sent to the client.



27
28
29
# File 'lib/orator/middle_ground.rb', line 27

def send(data)
  @client.socket.send Oj.dump(data, :mode => :compat)
end