class Fluent::Counter::Validator

Constants

VALID_METHODS
VALID_NAME
VALID_SCOPE_NAME

Public Class Methods

new(*types) click to toggle source
# File lib/fluent/counter/validator.rb, line 45
def initialize(*types)
  @types = types.map(&:to_s)
  @empty = @types.delete('empty')
end
request(data) click to toggle source
# File lib/fluent/counter/validator.rb, line 26
def self.request(data)
  errors = []
  raise "Received data is not Hash: #{data}" unless data.is_a?(Hash)

  unless data['id']
    errors << Fluent::Counter::InvalidRequest.new('Request should include `id`')
  end

  if !data['method']
    errors << Fluent::Counter::InvalidRequest.new('Request should include `method`')
  elsif !(VALID_NAME =~ data['method'])
    errors << Fluent::Counter::InvalidRequest.new('`method` is the invalid format')
  elsif !VALID_METHODS.include?(data['method'])
    errors << Fluent::Counter::MethodNotFound.new("Unknown method name passed: #{data['method']}")
  end

  errors.map(&:to_hash)
end

Public Instance Methods

call(data) click to toggle source
# File lib/fluent/counter/validator.rb, line 50
def call(data)
  success = []
  errors = []

  if @empty && data.empty?
    errors << Fluent::Counter::InvalidParams.new('One or more `params` are required')
  else
    data.each do |d|
      begin
        @types.each { |type| dispatch(type, d) }
        success << d
      rescue => e
        errors << e
      end
    end
  end

  [success, errors]
end

Private Instance Methods

dispatch(type, data) click to toggle source
# File lib/fluent/counter/validator.rb, line 72
def dispatch(type, data)
  send("validate_#{type}!", data)
rescue NoMethodError => e
  raise Fluent::Counter::InternalServerError.new(e)
end