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) ⇒ Protocol

Returns a new instance of Protocol.



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

def initialize(name, namespace=nil, types=nil, messages=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)
end

Instance Attribute Details

#md5Object (readonly)

Returns the value of attribute md5.



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

def md5
  @md5
end

#messagesObject (readonly)

Returns the value of attribute messages.



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

def messages
  @messages
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

#typesObject (readonly)

Returns the value of attribute types.



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

def types
  @types
end

Class Method Details

.parse(protocol_string) ⇒ Object



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

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']
    Protocol.new(name, namespace, types, messages)
  else
    raise ProtocolParseError, "Not a JSON object: #{json_data}"
  end
end

Instance Method Details

#==(other) ⇒ Object



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

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

#to_sObject



60
61
62
# File 'lib/avro/protocol.rb', line 60

def to_s
  MultiJson.dump to_avro
end