Class: Carnivore::Utils::MessageRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/carnivore/utils/message_registry.rb

Overview

Registry used for preventing duplicate message processing

Instance Method Summary (collapse)

Constructor Details

- (MessageRegistry) initialize

Create new instance



8
9
10
11
# File 'lib/carnivore/utils/message_registry.rb', line 8

def initialize
  @store = []
  @size = 100
end

Instance Method Details

- (self) push(item)

Register checksum into registry

Parameters:

  • item (String)

    checksum

Returns:

  • (self)


30
31
32
33
34
35
36
# File 'lib/carnivore/utils/message_registry.rb', line 30

def push(item)
  @store.push(item)
  if(@store.size > @size)
    @store.shift
  end
  self
end

- (String) sha(thing)

Generate checksum for given item

Parameters:

  • thing (Object)

Returns:

  • (String)

    checksum



42
43
44
45
46
47
# File 'lib/carnivore/utils/message_registry.rb', line 42

def sha(thing)
  unless(thing.is_a?(String))
    thing = MultiJson.dump(thing)
  end
  (Digest::SHA512.new << thing).hexdigest
end

- (TrueClass, FalseClass) valid?(message)

Validity of message (not found within registry)

Parameters:

Returns:

  • (TrueClass, FalseClass)


17
18
19
20
21
22
23
24
# File 'lib/carnivore/utils/message_registry.rb', line 17

def valid?(message)
  checksum = sha(message)
  found = @store.include?(checksum)
  unless(found)
    push(checksum)
  end
  !found
end