Class: RubyRabbitmqJanus::Tools::Replace
- Inherits:
-
Object
- Object
- RubyRabbitmqJanus::Tools::Replace
- Defined in:
- lib/rrj/tools/replaces.rb
Overview
Format message request with good data to HASH format
Direct Known Subclasses
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
private
Returns the value of attribute opts.
-
#request ⇒ Object
readonly
private
Returns the value of attribute request.
Instance Method Summary collapse
-
#add_other ⇒ Object
private
Adds other element to request.
-
#initialize(request, options = {}) ⇒ Replace
constructor
Initialize a tool replace.
-
#new_parent(key, parent) ⇒ Object
private
Return a key to hash parsing.
-
#replace_classic ⇒ Object
private
Replace classic elements.
-
#replace_handle ⇒ Object
private
Replace handle integer.
-
#replace_other ⇒ Object
private
Replace other element in request.
-
#replace_plugin ⇒ Object
private
Replace plugin string.
-
#replace_session ⇒ Object
private
Read option session and replace in request.
-
#replace_transaction ⇒ Object
private
Create an transaction string and replace in request field with an String format.
-
#rewrite_key_to_string(node) ⇒ Object
private
Rewrite key symbol to string.
-
#running_hash(hash, parent = 'body') ⇒ Object
private
Replace value in request Hash.
-
#test_presence?(presence_of_key) ⇒ Boolean
private
Test presence of key in many hash :reek:NilCheck.
-
#transform_request ⇒ Object
Replace element in hash request with information used for this transaction.
Constructor Details
#initialize(request, options = {}) ⇒ Replace
Initialize a tool replace
9 10 11 12 13 |
# File 'lib/rrj/tools/replaces.rb', line 9 def initialize(request, = {}) @request = request @opts = Tools::Log.instance.debug "Element to replace : #{@opts}" end |
Instance Attribute Details
#opts ⇒ Object (readonly, private)
Returns the value of attribute opts.
28 29 30 |
# File 'lib/rrj/tools/replaces.rb', line 28 def opts @opts end |
#request ⇒ Object (readonly, private)
Returns the value of attribute request.
28 29 30 |
# File 'lib/rrj/tools/replaces.rb', line 28 def request @request end |
Instance Method Details
#add_other ⇒ Object (private)
Adds other element to request
75 76 77 78 79 80 |
# File 'lib/rrj/tools/replaces.rb', line 75 def add_other values = @opts['add'] @request['body'].merge!(values) rescue => Tools::Log.instance.warn "Error ADD other field : #{message}" end |
#new_parent(key, parent) ⇒ Object (private)
Return a key to hash parsing
103 104 105 |
# File 'lib/rrj/tools/replaces.rb', line 103 def new_parent(key, parent) "#{parent}#{'.' unless parent.empty?}#{key}" end |
#replace_classic ⇒ Object (private)
Replace classic elements
31 32 33 34 35 36 |
# File 'lib/rrj/tools/replaces.rb', line 31 def replace_classic replace_transaction if @request.key?('transaction') replace_session if @request.key?('session_id') replace_plugin if @request.key?('plugin') replace_handle if @request.key?('handle_id') end |
#replace_handle ⇒ Object (private)
Replace handle integer
60 61 62 63 64 |
# File 'lib/rrj/tools/replaces.rb', line 60 def replace_handle @request['handle_id'] = @opts['handle_id'] rescue => Tools::Log.instance.warn "Error handle replace : #{message}" end |
#replace_other ⇒ Object (private)
Replace other element in request
67 68 69 70 71 72 |
# File 'lib/rrj/tools/replaces.rb', line 67 def replace_other values = @opts['replace'] running_hash(rewrite_key_to_string(values)) rescue => Tools::Log.instance.warn "Error REPLACE other field : #{message}" end |
#replace_plugin ⇒ Object (private)
Replace plugin string
53 54 55 56 57 |
# File 'lib/rrj/tools/replaces.rb', line 53 def replace_plugin @request['plugin'] = Tools::Config.instance.['janus']['plugins'][0] rescue => Tools::Log.instance.warn "Error plugin replace : #{message}" end |
#replace_session ⇒ Object (private)
Read option session and replace in request
46 47 48 49 50 |
# File 'lib/rrj/tools/replaces.rb', line 46 def replace_session @request['session_id'] = @opts['session_id'] rescue => Tools::Log.instance.warn "Error session replace : #{message}" end |
#replace_transaction ⇒ Object (private)
Create an transaction string and replace in request field with an String format
39 40 41 42 43 |
# File 'lib/rrj/tools/replaces.rb', line 39 def replace_transaction @request['transaction'].replace [*('A'..'Z'), *('0'..'9')].sample(10).join rescue => Tools::Log.instance.warn "Error transaction replace : #{message}" end |
#rewrite_key_to_string(node) ⇒ Object (private)
Rewrite key symbol to string
83 84 85 86 87 88 89 |
# File 'lib/rrj/tools/replaces.rb', line 83 def rewrite_key_to_string(node) Hash[ node.map do |key, value| [key.to_s, value.is_a?(Hash) ? rewrite_key_to_string(value) : value] end ] end |
#running_hash(hash, parent = 'body') ⇒ Object (private)
Replace value in request Hash
92 93 94 95 96 97 98 99 100 |
# File 'lib/rrj/tools/replaces.rb', line 92 def running_hash(hash, parent = 'body') hash.each do |key, value| if value.is_a?(Hash) running_hash(value, new_parent(key, parent)) else @request[parent][key] = value end end end |
#test_presence?(presence_of_key) ⇒ Boolean (private)
Test presence of key in many hash :reek:NilCheck
109 110 111 112 113 |
# File 'lib/rrj/tools/replaces.rb', line 109 def test_presence?(presence_of_key) @opts.key?(presence_of_key) && \ @request.key?('body') && \ !@opts[presence_of_key].nil? end |
#transform_request ⇒ Object
Replace element in hash request with information used for this transaction
17 18 19 20 21 22 23 24 |
# File 'lib/rrj/tools/replaces.rb', line 17 def transform_request replace_classic unless @opts.empty? replace_other if test_presence?('replace') add_other if test_presence?('add') end @request end |