class RubyRabbitmqJanus::Tools::Replaces::Replace

# Prepare request

Tools for replace elements in request sending to Rabbitmq. It's a basic class. Manage just transaction element.

@author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>

Attributes

opts[R]
request[R]
type[R]

Public Class Methods

new(request, options = {}) click to toggle source

Initialize tool replace.

@param [Hash] request Request parsing before sending to RabbitMQ/Janus @param [Hash] options Elements to be replaced in request

# File lib/rrj/tools/replaces/replace.rb, line 20
def initialize(request, options = {})
  @request = request
  @opts = options
  @type = Tools::Type.new(@request)
end

Public Instance Methods

transform_request() click to toggle source

Replace element in hash request with information used for this transaction.

@return [Hash] request with element replace

# File lib/rrj/tools/replaces/replace.rb, line 30
def transform_request
  replace_element_classic
  unless @opts.empty?
    replace_other if test_presence?('replace')
    add_other if test_presence?('add')
  end
  @request
end

Private Instance Methods

add_other() click to toggle source
# File lib/rrj/tools/replaces/replace.rb, line 52
def add_other
  values = @opts['add']
  @request['body'].merge!(values)
end
new_parent(key, parent) click to toggle source
# File lib/rrj/tools/replaces/replace.rb, line 79
def new_parent(key, parent)
  "#{parent}#{'.' unless parent.empty?}#{key}"
end
replace_element_classic() click to toggle source
# File lib/rrj/tools/replaces/replace.rb, line 83
def replace_element_classic
  replace_transaction if @request.key?('transaction')
end
replace_other() click to toggle source
# File lib/rrj/tools/replaces/replace.rb, line 48
def replace_other
  running_hash(rewrite_key_to_string(@opts['replace'])[0].to_h)
end
replace_transaction() click to toggle source
# File lib/rrj/tools/replaces/replace.rb, line 87
def replace_transaction
  @request['transaction'] = @type.convert('transaction')
end
rewrite_key_to_string(node) click to toggle source
# File lib/rrj/tools/replaces/replace.rb, line 57
def rewrite_key_to_string(node)
  [
    node.map do |key, value|
      [key.to_s, value?(value)]
    end
  ]
end
running_hash(hash, parent = 'body') click to toggle source
# File lib/rrj/tools/replaces/replace.rb, line 69
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 unless key.eql? 'sdp'
    end
  end
end
test_presence?(presence_of_key) click to toggle source
# File lib/rrj/tools/replaces/replace.rb, line 43
def test_presence?(presence_of_key)
  @opts.key?(presence_of_key) && @request.key?('body') &&
    !@opts[presence_of_key].blank?
end
value?(value) click to toggle source
# File lib/rrj/tools/replaces/replace.rb, line 65
def value?(value)
  value.is_a?(Hash) ? rewrite_key_to_string(value) : value
end