Class: RobotArmy::Messenger

Inherits:
Object show all
Defined in:
lib/robot-army/messenger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, output) ⇒ Messenger

Returns a new instance of Messenger.



5
6
7
# File 'lib/robot-army/messenger.rb', line 5

def initialize(input, output)
  @input, @output = input, output
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



3
4
5
# File 'lib/robot-army/messenger.rb', line 3

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



3
4
5
# File 'lib/robot-army/messenger.rb', line 3

def output
  @output
end

Instance Method Details

#getObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/robot-army/messenger.rb', line 15

def get
  data = nil
  loop do
    case data = input.gets('|')
    when nil, ''
      return nil
    when /^\s*$/
      # again!
    else
      break
    end
  end
  data = Base64.decode64(data.chop)
  Marshal.load(data)
end

#post(response) ⇒ Object



9
10
11
12
13
# File 'lib/robot-army/messenger.rb', line 9

def post(response)
  dump = Marshal.dump(response)
  dump = Base64.encode64(dump) + '|'
  output << dump
end