Class: Cosmos::Processor

Inherits:
Object show all
Defined in:
lib/cosmos/processors/processor.rb

Direct Known Subclasses

StatisticsProcessor, WatermarkProcessor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value_type = :CONVERTED) ⇒ Processor

Create a new Processor

Parameters:

  • value_type (Symbol or String) (defaults to: :CONVERTED)

    the value type to process

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
# File 'lib/cosmos/processors/processor.rb', line 33

def initialize(value_type = :CONVERTED)
  @name = self.class.to_s.upcase
  value_type = value_type.to_s.upcase.intern
  @value_type = value_type
  raise ArgumentError, "value_type must be RAW, CONVERTED, FORMATTED, or WITH_UNITS. Is #{@value_type}" unless Packet::VALUE_TYPES.include?(@value_type)

  @results = {}
end

Instance Attribute Details

#nameString

Returns The processor name.

Returns:

  • (String)

    The processor name



26
27
28
# File 'lib/cosmos/processors/processor.rb', line 26

def name
  @name
end

#resultsHash

Returns The results of the most recent execution of the processor.

Returns:

  • (Hash)

    The results of the most recent execution of the processor



29
30
31
# File 'lib/cosmos/processors/processor.rb', line 29

def results
  @results
end

#value_typeSymbol (readonly)

Returns The value type for the processor.

Returns:

  • (Symbol)

    The value type for the processor



23
24
25
# File 'lib/cosmos/processors/processor.rb', line 23

def value_type
  @value_type
end

Instance Method Details

#as_jsonObject



82
83
84
# File 'lib/cosmos/processors/processor.rb', line 82

def as_json
  { 'name' => @name, 'class' => self.class.name, 'params' => [@value_type.to_s] }
end

#call(packet, buffer) ⇒ Object

Perform processing on the packet.

Parameters:

  • packet (Packet)

    The packet which contains the value. This can be useful to reach into the packet and use other values in the conversion.

  • buffer (String)

    The packet buffer

Returns:

  • The processed result



53
54
55
# File 'lib/cosmos/processors/processor.rb', line 53

def call(packet, buffer)
  raise "call method must be defined by subclass"
end

#cloneProcessor Also known as: dup

Make a light weight clone of this processor. This only creates a new hash of results

Returns:

  • (Processor)

    A copy of the processor with a new hash of results



70
71
72
73
74
# File 'lib/cosmos/processors/processor.rb', line 70

def clone
  processor = super()
  processor.results = processor.results.clone
  processor
end

#resetObject

Reset any state



63
64
65
# File 'lib/cosmos/processors/processor.rb', line 63

def reset
  # By default do nothing
end

#to_configObject

Convert to configuration file string



78
79
80
# File 'lib/cosmos/processors/processor.rb', line 78

def to_config
  "  PROCESSOR #{@name} #{self.class.name.to_s.class_name_to_filename} #{@value_type}\n"
end

#to_sString

Returns The processor class.

Returns:

  • (String)

    The processor class



58
59
60
# File 'lib/cosmos/processors/processor.rb', line 58

def to_s
  self.class.to_s.split('::')[-1]
end