Class: CARPS::MessageParser

Inherits:
Object
  • Object
show all
Defined in:
lib/carps/protocol/message.rb

Overview

Parse a message from a block of unformatted text

Instance Method Summary collapse

Constructor Details

#initialize(choices) ⇒ MessageParser

Create a new parser from a list of choices



32
33
34
# File 'lib/carps/protocol/message.rb', line 32

def initialize choices
   @choices = choices
end

Instance Method Details

#choose_parser(blob) ⇒ Object

Parse, choosing from a number of alternative messages, return the first one that suceeds

Raises:



37
38
39
40
41
42
43
44
45
46
# File 'lib/carps/protocol/message.rb', line 37

def choose_parser blob 
   @choices.each do |message|
      begin
         result, text = message.parse blob
         return [result, text] 
      rescue Expected
      end
   end
   raise Expected
end

#parse(text) ⇒ Object

Parse the text into a message



49
50
51
52
53
54
55
56
57
58
# File 'lib/carps/protocol/message.rb', line 49

def parse text
   input = text
   begin
      msg, blob = choose_parser text
      return msg
   rescue Expected
      UI::warn "An invalid email was received:", input
      return nil
   end
end