Class: RubyRabbitmqJanus::Tools::Replace

Inherits:
Object
  • Object
show all
Defined in:
lib/rrj/tools/replaces.rb

Overview

Format message request with good data to HASH format

Author:

Direct Known Subclasses

AdminReplace

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @request = request
  @opts = options
  Tools::Log.instance.debug "Element to replace : #{@opts}"
end

Instance Attribute Details

#optsObject (readonly, private)

Returns the value of attribute opts.



28
29
30
# File 'lib/rrj/tools/replaces.rb', line 28

def opts
  @opts
end

#requestObject (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_otherObject (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 => message
  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_classicObject (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_handleObject (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 => message
  Tools::Log.instance.warn "Error handle replace : #{message}"
end

#replace_otherObject (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 => message
  Tools::Log.instance.warn "Error REPLACE other field : #{message}"
end

#replace_pluginObject (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.options['janus']['plugins'][0]
rescue => message
  Tools::Log.instance.warn "Error plugin replace : #{message}"
end

#replace_sessionObject (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 => message
  Tools::Log.instance.warn "Error session replace : #{message}"
end

#replace_transactionObject (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 => message
  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

Returns:

  • (Boolean)


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_requestObject

Replace element in hash request with information used for this transaction

Returns:

  • HASH request with element replace



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