Class: ChainReactor::Reaction

Inherits:
Object
  • Object
show all
Defined in:
lib/chain-reactor/reaction.rb

Overview

Represents a single reaction block, defined in the chain file with the ‘react_to’ method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, block, logger) ⇒ Reaction

Create a new reaction, with the options and code block to run.



27
28
29
30
31
32
# File 'lib/chain-reactor/reaction.rb', line 27

def initialize(options,block,logger)
  @options = { :parser => :json, :required_keys => [], :keys_to_sym => true }.merge(options)
  @block = block
  @log = logger
  @log.debug { "Created reaction with options: #{options}" }
end

Instance Attribute Details

#optionsObject

Options used by this reaction.



24
25
26
# File 'lib/chain-reactor/reaction.rb', line 24

def options
  @options
end

#previous_dataObject

The previous data set sent to this reaction, if executed before.



22
23
24
# File 'lib/chain-reactor/reaction.rb', line 22

def previous_data
  @previous_data
end

#previous_resultObject

The previous return value from this reaction, if executed before.



20
21
22
# File 'lib/chain-reactor/reaction.rb', line 20

def previous_result
  @previous_result
end

Instance Method Details

#execute(data_string) ⇒ Object

Executes the block of code after parsing the data string.



35
36
37
38
39
40
41
42
43
44
# File 'lib/chain-reactor/reaction.rb', line 35

def execute(data_string)
  parser = ParserFactory.get_parser(@options[:parser],@log)
  data = parser.parse(data_string,@options[:required_keys],@options[:keys_to_sym])
  begin
    @previous_result = @block.call(data)
    @previous_data = data
  rescue StandardError => e
    raise ReactionError.new(e)
  end
end