Class: Fluent::Counter::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/counter/validator.rb

Direct Known Subclasses

ArrayValidator, HashValidator

Constant Summary collapse

VALID_NAME =
/\A[a-z][a-zA-Z0-9\-_]*\Z/
VALID_SCOPE_NAME =
/\A[a-z][\ta-zA-Z0-9\-_]*\Z/
VALID_METHODS =
%w(establish init delete inc get reset)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*types) ⇒ Validator

Returns a new instance of Validator.



45
46
47
48
# File 'lib/fluent/counter/validator.rb', line 45

def initialize(*types)
  @types = types.map(&:to_s)
  @empty = @types.delete('empty')
end

Class Method Details

.request(data) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

Instance Method Details

#call(data) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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