class Tros::Protocol

Constants

VALID_TYPE_SCHEMA_TYPES
VALID_TYPE_SCHEMA_TYPES_SYM

Attributes

md5[R]
messages[R]
name[R]
namespace[R]
types[R]

Public Class Methods

new(name, namespace=nil, types=nil, messages=nil) click to toggle source
   # File lib/tros/protocol.rb
38 def initialize(name, namespace=nil, types=nil, messages=nil)
39   # Ensure valid ctor args
40   if !name
41     raise ProtocolParseError, 'Protocols must have a non-empty name.'
42   elsif !name.is_a?(String)
43     raise ProtocolParseError, 'The name property must be a string.'
44   elsif !namespace.is_a?(String)
45     raise ProtocolParseError, 'The namespace property must be a string.'
46   elsif !types.is_a?(Array)
47     raise ProtocolParseError, 'The types property must be a list.'
48   elsif !messages.is_a?(Hash)
49     raise ProtocolParseError, 'The messages property must be a JSON object.'
50   end
51 
52   @name = name
53   @namespace = namespace
54   type_names = {}
55   @types = parse_types(types, type_names)
56   @messages = parse_messages(messages, type_names)
57   @md5 = Digest::MD5.digest(to_s)
58 end
parse(protocol_string) click to toggle source
   # File lib/tros/protocol.rb
24 def self.parse(protocol_string)
25   json_data = JSON.load(protocol_string)
26 
27   if json_data.is_a? Hash
28     name = json_data['protocol']
29     namespace = json_data['namespace']
30     types = json_data['types']
31     messages = json_data['messages']
32     Protocol.new(name, namespace, types, messages)
33   else
34     raise ProtocolParseError, "Not a JSON object: #{json_data}"
35   end
36 end

Public Instance Methods

==(other) click to toggle source
   # File lib/tros/protocol.rb
64 def ==(other)
65   to_avro == other.to_avro
66 end
to_s() click to toggle source
   # File lib/tros/protocol.rb
60 def to_s
61   JSON.dump(to_avro)
62 end

Protected Instance Methods

to_avro(names=Set.new) click to toggle source
    # File lib/tros/protocol.rb
101 def to_avro(names=Set.new)
102   hsh = {'protocol' => name}
103   hsh['namespace'] = namespace if namespace
104   hsh['types'] = types.map{|t| t.to_avro(names) } if types
105 
106   if messages
107     hsh['messages'] = messages.inject({}) { |carry, (k,t)| carry[k] = t.to_avro(names); carry }
108   end
109 
110   hsh
111 end

Private Instance Methods

parse_messages(messages, names) click to toggle source
   # File lib/tros/protocol.rb
83 def parse_messages(messages, names)
84   message_objects = {}
85   messages.each do |name, body|
86     if message_objects.has_key?(name)
87       raise ProtocolParseError, "Message name \"#{name}\" repeated."
88     elsif !body.is_a?(Hash)
89       raise ProtocolParseError, "Message name \"#{name}\" has non-object body #{body.inspect}"
90     end
91 
92     request  = body['request']
93     response = body['response']
94     errors   = body['errors']
95     message_objects[name] = Message.new(name, request, response, errors, names, namespace)
96   end
97   message_objects
98 end
parse_types(types, type_names) click to toggle source
   # File lib/tros/protocol.rb
69 def parse_types(types, type_names)
70   type_objects = []
71   types.collect do |type|
72     # FIXME adding type.name to type_names is not defined in the
73     # spec. Possible bug in the python impl and the spec.
74     type_object = Schema.real_parse(type, type_names, namespace)
75     unless VALID_TYPE_SCHEMA_TYPES_SYM.include?(type_object.type_sym)
76       msg = "Type #{type} not an enum, record, fixed or error."
77       raise ProtocolParseError, msg
78     end
79     type_object
80   end
81 end