class Angus::ParamsValidator

Constants

DEFAULT_DATETIME_FORMAT
DEFAULT_DATE_FORMAT

Public Class Methods

new(operation) click to toggle source
# File lib/angus/utils/params_validator.rb, line 10
def initialize(operation)
  @operation        = operation
  @request_elements = operation.request_elements
end

Public Instance Methods

all_required_fields?(params) click to toggle source
# File lib/angus/utils/params_validator.rb, line 20
def all_required_fields?(params)
  required_fields = @request_elements.select(&:required)

  no_found_parameters = required_fields.select {|rf| !params.include?(rf.name.to_sym) }

  unless no_found_parameters.empty?
    raise RequiredParameterNotFound.new(no_found_parameters)
  end
end
all_valid_types?(params) click to toggle source
# File lib/angus/utils/params_validator.rb, line 30
def all_valid_types?(params)
  @request_elements.each do |field|
    type = field.type
    method_name = "valid_#{type}?"

    if self.respond_to?(method_name)
      param = params[field.name.to_sym]

      valid = self.send(method_name, field, param)

      raise InvalidParameterType.new(field, param) unless valid
    else
      # TODO handle complex types
    end
  end
end
valid?(params) click to toggle source
# File lib/angus/utils/params_validator.rb, line 15
def valid?(params)
  all_required_fields?(params)
  all_valid_types?(params)
end
valid_date?(field, param) click to toggle source
# File lib/angus/utils/params_validator.rb, line 60
def valid_date?(field, param)
  begin
    Date.strptime(param, DEFAULT_DATE_FORMAT)

    true
  rescue ArgumentError
    false
  end
end
valid_datetime?(field, param) click to toggle source
# File lib/angus/utils/params_validator.rb, line 70
def valid_datetime?(field, param)
  begin
    DateTime.strptime(param, DEFAULT_DATETIME_FORMAT)

    true
  rescue ArgumentError
    false
  end
end
valid_decimal?(field, param) click to toggle source
# File lib/angus/utils/params_validator.rb, line 56
def valid_decimal?(field, param)
  !!(param =~/^[-+]?([0-9]+(\.[0-9]+)?$)/)
end
valid_integer?(field, param) click to toggle source
# File lib/angus/utils/params_validator.rb, line 52
def valid_integer?(field, param)
  !!(param =~ /^[-+]?[0-9]+$/)
end
valid_string?(field, param) click to toggle source

String are all ways valid

# File lib/angus/utils/params_validator.rb, line 48
def valid_string?(field, param)
  true
end