module Dronejob::Modules::Params

Public Instance Methods

convert_param(value, config) click to toggle source
# File lib/dronejob/modules/params.rb, line 51
def convert_param(value, config)
  return nil if value.nil?

  return value
end
param(key, options) click to toggle source
# File lib/dronejob/modules/params.rb, line 7
def param(key, options)
  @params ||= {}
  @params[key] = options
  @params[key][:argument_type] = @params[key][:type]
  if @params[key][:type] == :base64 or @params[key][:type] == :json64
    @params[key][:argument_type] = :string
  end
end
param_config(key) click to toggle source
# File lib/dronejob/modules/params.rb, line 16
def param_config(key)
  @params[key.to_sym]
end
params() click to toggle source
# File lib/dronejob/modules/params.rb, line 20
def params
  @params ||= {}
end
params=(params) click to toggle source
# File lib/dronejob/modules/params.rb, line 25
def params=(params)
  @params = params.map { |name, value| [name, transform_parameter(name, value)] }.to_h
end
transform_parameter(name, value) click to toggle source
# File lib/dronejob/modules/params.rb, line 29
def transform_parameter(name, value)
  if (config = self.class.param_config(name))
    return Base64.strict_decode64(value) if config[:type] == :base64
    return JSON.parse(Base64.strict_decode64(value)) if config[:type] == :json64

    if config[:type] == :numeric
      return Integer(value) if Integer(value) rescue false
      return Float(value) if Float(value) rescue false
    end
    if config[:type] == :boolean
      return true if value == "true"
      return false if value == "false"
    end
  end
  value
end
validate_parameters!() click to toggle source
# File lib/dronejob/modules/params.rb, line 61
def validate_parameters!
  # Fill default parameters
  self.class.params.each do |param_name, param_config|
    param_value = @params[param_name.to_s]
    if param_value.nil? and param_config[:default]
      @params[param_name.to_s] = param_config[:default]
    end
  end

  # Validate parameters
  type_map = { string: ["String"], numeric: ["Fixnum", "Integer", "Float"], array: ["Array"], boolean: ["TrueClass", "FalseClass"], base64: ["String"], json64: ["Hash", "Array"] }
  self.class.params.each do |param_name, param_config|
    param_value = @params[param_name.to_s]
    if param_value.nil?
      fail ArgumentError, "Missing required parameter '#{param_name}' (#{param_config[:type]})" if param_config[:required]
    else
      allowed_classes = type_map[param_config[:type]]
      unless allowed_classes.include?(param_value.class.name)
        fail ArgumentError, "Invalid value '#{param_value}' for parameter '#{param_name}' (expected #{param_config[:type]}, got #{param_value.class.name})"
      end
    end
  end
end