Class: Schemata::ParsedMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/schemata/common/parsed_msg.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ ParsedMessage

Returns a new instance of ParsedMessage.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/schemata/common/parsed_msg.rb', line 10

def initialize(json)
  @contents = Yajl::Parser.parse(json)

  @min_version = @contents['min_version']
  if !@min_version
    raise DecodeError.new("Field 'min_version' abset from message")
  end

  versions = []
  @contents.keys.each do |k|
    next if k == 'min_version'
    unless k =~ /^V[0-9]+$/
      raise DecodeError.new("Invalid key: #{k}")
    end
    versions << k[1..-1].to_i
  end

  if versions.empty?
    raise DecodeError.new("Message contains no versioned hashes")
  end

  if Set.new(versions.min..versions.max) != Set.new(versions)
    raise DecodeError.new("There are versions missing between\
 #{versions.min} and #{versions.max}")
  end

  @version = versions.max
end

Instance Attribute Details

#min_versionObject (readonly)

Returns the value of attribute min_version.



8
9
10
# File 'lib/schemata/common/parsed_msg.rb', line 8

def min_version
  @min_version
end

#versionObject (readonly)

Returns the value of attribute version.



8
9
10
# File 'lib/schemata/common/parsed_msg.rb', line 8

def version
  @version
end

Instance Method Details

#contentsObject



39
40
41
# File 'lib/schemata/common/parsed_msg.rb', line 39

def contents
  Schemata::HashCopyHelpers.deep_copy(@contents)
end