Class: Avro::Protocol::Message

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, request, response, errors = nil, names = nil, default_namespace = nil) ⇒ Message

Returns a new instance of Message.



116
117
118
119
120
121
122
# File 'lib/avro/protocol.rb', line 116

def initialize(name, request, response, errors=nil, names=nil, default_namespace=nil)
  @name = name
  @default_namespace = default_namespace
  @request = parse_request(request, names)
  @response = parse_response(response, names)
  @errors = parse_errors(errors, names) if errors
end

Instance Attribute Details

#default_namespaceObject (readonly)

Returns the value of attribute default_namespace.



114
115
116
# File 'lib/avro/protocol.rb', line 114

def default_namespace
  @default_namespace
end

#errorsObject (readonly)

Returns the value of attribute errors.



114
115
116
# File 'lib/avro/protocol.rb', line 114

def errors
  @errors
end

#nameObject (readonly)

Returns the value of attribute name.



114
115
116
# File 'lib/avro/protocol.rb', line 114

def name
  @name
end

#requestObject (readonly)

Returns the value of attribute request.



114
115
116
# File 'lib/avro/protocol.rb', line 114

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



114
115
116
# File 'lib/avro/protocol.rb', line 114

def response
  @response
end

Instance Method Details

#parse_errors(errors, names) ⇒ Object



153
154
155
156
157
158
# File 'lib/avro/protocol.rb', line 153

def parse_errors(errors, names)
  unless errors.is_a?(Array)
    raise ProtocolParseError, "Errors property not an Array: #{errors}"
  end
  Schema.real_parse(errors, names, default_namespace)
end

#parse_request(request, names) ⇒ Object



137
138
139
140
141
142
# File 'lib/avro/protocol.rb', line 137

def parse_request(request, names)
  unless request.is_a?(Array)
    raise ProtocolParseError, "Request property not an Array: #{request.inspect}"
  end
  Schema::RecordSchema.new(nil, default_namespace, request, names, :request)
end

#parse_response(response, names) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/avro/protocol.rb', line 144

def parse_response(response, names)
  if response.is_a?(String) && names
    fullname = Name.make_fullname(response, default_namespace)
    return names[fullname] if names.include?(fullname)
  end

  Schema.real_parse(response, names, default_namespace)
end

#to_avro(names = Set.new) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/avro/protocol.rb', line 124

def to_avro(names=Set.new)
  {
    'request' => request.to_avro(names),
    'response' => response.to_avro(names)
  }.tap do |hash|
    hash['errors'] = errors.to_avro(names) if errors
  end
end

#to_sObject



133
134
135
# File 'lib/avro/protocol.rb', line 133

def to_s
  Yajl.dump to_avro
end