Class: Mooncell::Router Private

Inherits:
Object
  • Object
show all
Defined in:
lib/mooncell/router.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Command Dispatcher

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Router

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create rotuer instance

Since:

  • 0.1.0



15
16
17
18
# File 'lib/mooncell/router.rb', line 15

def initialize(app)
  @app = app
  @routes = Concurrent::Hash.new
end

Instance Method Details

#call(connection, params) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Execute to command

Parameters:

  • params (Hash)

    the request parameters

Since:

  • 0.1.0



45
46
47
48
49
50
51
52
53
54
# File 'lib/mooncell/router.rb', line 45

def call(connection, params)
  command_name = params.delete('command')
  command_class = @routes[command_name]
  return if command_class.nil?

  # TODO: Need refactor
  command = Kernel.const_get(command_class).new(connection)
  command.call(params)
  command.respond.send
end

#command(name, command) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add command

Parameters:

Since:

  • 0.1.0



35
36
37
# File 'lib/mooncell/router.rb', line 35

def command(name, command)
  @routes[name] = command
end

#load(code) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Load routes

Since:

  • 0.1.0



24
25
26
# File 'lib/mooncell/router.rb', line 24

def load(code)
  instance_eval(code)
end