class RubyRabbitmqJanus::Tools::Type

Class for converting elements given by apps to this gem an type conform to request sending

Public Class Methods

new(request) click to toggle source

Initialize an object for cast a type to data given by app

@param [Hash] request Request parsing before sending to RabbitMQ/Janus

# File lib/rrj/tools/replaces/type.rb, line 16
def initialize(request)
  @request = request
  @key = @data = nil
end

Public Instance Methods

convert(key, option = {}) click to toggle source

Return an data with a type corresponding to string in request

@param [String] key Key testing @param [Hash] option Datas sending by user and adding/replace in request

@return data with good type for JSON format

# File lib/rrj/tools/replaces/type.rb, line 27
def convert(key, option = {})
  @key = key
  @data = option[@key] if option.key?(@key)
  convert_data
end

Private Instance Methods

array_alone() click to toggle source
# File lib/rrj/tools/replaces/type.rb, line 99
def array_alone
  if @data.is_a?(Array)
    @data.count.eql?(1) ? @data[0] : @data
  else
    @data
  end
end
convert_data() click to toggle source
# File lib/rrj/tools/replaces/type.rb, line 35
def convert_data
  case search_key
  when '<string>'               then convert_to_type_string
  when '<number>', '<integer>'  then convert_to_type_number
  when '<boolean>'              then convert_to_type_boolean
  when '<array>'                then convert_to_type_array
  when '<plugins>'              then convert_to_type_plugins
  when '<transaction>'          then convert_to_type_transaction
  when /<plugin\[[0-9]\]>/      then convert_to_type_plugin
  end
end
convert_to_type_array() click to toggle source
# File lib/rrj/tools/replaces/type.rb, line 84
def convert_to_type_array
  data = array_alone
  key = data.is_a?(Hash) ? @key : @key.pluralize
  [key, data]
end
convert_to_type_boolean() click to toggle source
# File lib/rrj/tools/replaces/type.rb, line 71
def convert_to_type_boolean
  if test_boolean('TRUE', TrueClass)
    true
  elsif test_boolean('FALSE', FalseClass)
    false
  end
end
convert_to_type_number() click to toggle source
# File lib/rrj/tools/replaces/type.rb, line 67
def convert_to_type_number
  @data.to_i
end
convert_to_type_plugin() click to toggle source
# File lib/rrj/tools/replaces/type.rb, line 79
def convert_to_type_plugin
  index = @request[@key].gsub('<plugin[', '').gsub(']>', ']').to_i
  Config.instance.plugin_at(index)
end
convert_to_type_plugins() click to toggle source
# File lib/rrj/tools/replaces/type.rb, line 90
def convert_to_type_plugins
  @data.is_a?(String) ? [@data] : @data
end
convert_to_type_string() click to toggle source
# File lib/rrj/tools/replaces/type.rb, line 63
def convert_to_type_string
  @data.to_s
end
convert_to_type_transaction() click to toggle source
# File lib/rrj/tools/replaces/type.rb, line 59
def convert_to_type_transaction
  [*('A'..'Z'), *('0'..'9')].sample(10).join
end
search_key() click to toggle source
# File lib/rrj/tools/replaces/type.rb, line 47
def search_key
  field = @request[@key]
  if field.blank?
    @request.each do |key, value|
      test = @request[key]
      field = test[@key] \
        if value.is_a?(Hash) && test.key?(@key)
    end
  end
  field
end
test_boolean(boolean_string, boolean_class) click to toggle source
# File lib/rrj/tools/replaces/type.rb, line 94
def test_boolean(boolean_string, boolean_class)
  @data.is_a?(boolean_class) ||
    (@data.is_a?(String) && @data.casecmp(boolean_string).eql?(0))
end