Class: Avro::Protocol

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

Defined Under Namespace

Classes: Message, ProtocolParseError

Constant Summary collapse

VALID_TYPE_SCHEMA_TYPES =
Set.new(%w[enum record error fixed])
VALID_TYPE_SCHEMA_TYPES_SYM =
Set.new(VALID_TYPE_SCHEMA_TYPES.map(&:to_sym))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, namespace = nil, types = nil, messages = nil, doc = nil) ⇒ Protocol

Returns a new instance of Protocol.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/avro/protocol.rb', line 40

def initialize(name, namespace=nil, types=nil, messages=nil, doc=nil)
  # Ensure valid ctor args
  if !name
    raise ProtocolParseError, 'Protocols must have a non-empty name.'
  elsif !name.is_a?(String)
    raise ProtocolParseError, 'The name property must be a string.'
  elsif !namespace.is_a?(String)
    raise ProtocolParseError, 'The namespace property must be a string.'
  elsif !types.is_a?(Array)
    raise ProtocolParseError, 'The types property must be a list.'
  elsif !messages.is_a?(Hash)
    raise ProtocolParseError, 'The messages property must be a JSON object.'
  end

  @name = name
  @namespace = namespace
  type_names = {}
  @types = parse_types(types, type_names)
  @messages = parse_messages(messages, type_names)
  @md5 = Digest::MD5.digest(to_s)
  @doc = doc
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



24
25
26
# File 'lib/avro/protocol.rb', line 24

def doc
  @doc
end

#md5Object (readonly)

Returns the value of attribute md5.



24
25
26
# File 'lib/avro/protocol.rb', line 24

def md5
  @md5
end

#messagesObject (readonly)

Returns the value of attribute messages.



24
25
26
# File 'lib/avro/protocol.rb', line 24

def messages
  @messages
end

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/avro/protocol.rb', line 24

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



24
25
26
# File 'lib/avro/protocol.rb', line 24

def namespace
  @namespace
end

#typesObject (readonly)

Returns the value of attribute types.



24
25
26
# File 'lib/avro/protocol.rb', line 24

def types
  @types
end

Class Method Details

.parse(protocol_string) ⇒ Object



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

def self.parse(protocol_string)
  json_data = MultiJson.load(protocol_string)

  if json_data.is_a? Hash
    name = json_data['protocol']
    namespace = json_data['namespace']
    types = json_data['types']
    messages = json_data['messages']
    doc = json_data['doc']
    Protocol.new(name, namespace, types, messages, doc)
  else
    raise ProtocolParseError, "Not a JSON object: #{json_data}"
  end
end

Instance Method Details

#==(other) ⇒ Object



67
68
69
# File 'lib/avro/protocol.rb', line 67

def ==(other)
  to_avro == other.to_avro
end

#to_sObject



63
64
65
# File 'lib/avro/protocol.rb', line 63

def to_s
  MultiJson.dump to_avro
end