Class: HelloGoodbye::Console

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/hello_goodbye/console.rb

Overview

Commands shared by all consoles:

  • ‘hello’ => Used to ping the server.

    Responds with 'hello'.
    
  • ‘goodbye’ => Used to close a connection.

    Responds with 'goodbye'.
    

Direct Known Subclasses

ForemanConsole, ManagerConsole

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#foremanObject

:foreman

A reference to the Foreman object that instantiated the console, so that
the console can serve as an interface to this object.


19
20
21
# File 'lib/hello_goodbye/console.rb', line 19

def foreman
  @foreman
end

Class Method Details

.get(type) ⇒ Object

Returns a standard console of a given type. I’m aware this logic is dumb. Parameters:

  • type

** :manager => ManagerConsole



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hello_goodbye/console.rb', line 25

def self.get(type)
  case type
  when :manager
    ManagerConsole
  when :foreman
    ForemanConsole
  else
    if (obj = HelloGoodbye.const_get("#{type}_console".split('_').collect(&:capitalize).join))
        obj
    else
      raise ArgumentError, "What type of console is #{type}?"
    end
  end
end

Instance Method Details

#receive_command(command) ⇒ Object

Returns:

true if data was handled, false if not.


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hello_goodbye/console.rb', line 47

def receive_command(command)
  case command 
  when "hello"
    send_response :success => true,
      :message => "hello"
    true
  when "goodbye"
    send_response :success => true,
      :message => "goodbye"
    close_connection
    true
  else
    send_response :success => false,
      :message => "unknown command"
    false
  end
end

#receive_data(data) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hello_goodbye/console.rb', line 74

def receive_data(data)
  data = data.strip
  return false if data == ""

  begin
    json = Request.new(data)
  rescue JSON::ParserError => e
    send_response :success => false,
      :message => "invalid json"
    return false
  end

  receive_command(json.command)
end

#send_response(hash) ⇒ Object

Parameters:

hash
* success => true or false
* message => "Your message"
* results => []


70
71
72
# File 'lib/hello_goodbye/console.rb', line 70

def send_response(hash)
  send_data Response.new(hash).to_json
end