Class: Mushy::Filter

Inherits:
Flux
  • Object
show all
Defined in:
lib/mushy/fluxs/filter.rb

Instance Attribute Summary

Attributes inherited from Flux

#config, #flow, #id, #masher, #parent_fluxs, #subscribed_to, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Flux

#convert_this_to_an_array, #convert_to_symbolized_hash, #execute, #execute_single_event, #group_these_results, #guard, #ignore_these_results, inherited, #initialize, #join_these_results, #limit_these_results, #merge_these_results, #model_these_results, #outgoing_split_these_results, #shape_these, #sort_these_results, #standardize_these

Constructor Details

This class inherits a constructor from Mushy::Flux

Class Method Details

.detailsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mushy/fluxs/filter.rb', line 5

def self.details
  {
    name: 'Filter',
    title: 'Filter events',
    description: 'Filters events based on criteria.',
    fluxGroup: { name: 'Flows' },
    config: {
      equal: {
             description: 'Provide key/value pairs that must match in the event.',
             shrink:      true,
             label:       'Equal To',
             type:        'keyvalue',
             value:       {},
           },
      notequal: {
             description: 'Provide key/value pairs that must NOT match in the event.',
             shrink:      true,
             label:       'Not Equal To',
             type:        'keyvalue',
             value:       {},
           },
      contains: {
             description: 'Provide key/value pairs that must be contained.',
             shrink:      true,
             label:       'Contains',
             type:        'keyvalue',
             value:       {},
           },
      notcontains: {
             description: 'Provide key/value pairs that must NOT be contained.',
             shrink:      true,
             label:       'Not Contains',
             type:        'keyvalue',
             value:       {},
           },
    },
    examples: {
      "Match On A Value" => {
                              description: 'The input is returned if it matches on a value.',
                              input: {
                                       name: "John",
                                     },
                              config: {
                                matches: { name: "John" }
                              },
                              result: {
                                        name: "John",
                                      }
                            },
      "Contains A Value" => {
                              description: 'The input is returned if it contains a value.',
                              input: {
                                       name: "John",
                                     },
                              config: {
                                contains: { name: "H" }
                              },
                              result: {
                                        name: "John",
                                      }
                            },
      }
  }
end

Instance Method Details

#contains(a, b) ⇒ Object



96
97
98
99
# File 'lib/mushy/fluxs/filter.rb', line 96

def contains a, b
  return false unless b
  nice_string(b).include? a.downcase
end

#equal(a, b) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/mushy/fluxs/filter.rb', line 84

def equal a, b
  [a, b]
    .map { |x| numeric?(x) ? x.to_f : x }
    .map { |x| nice_string x }
    .group_by { |x| x }
    .count == 1
end

#nice_string(value) ⇒ Object



109
110
111
# File 'lib/mushy/fluxs/filter.rb', line 109

def nice_string value
  value.to_s.strip.downcase
end

#notcontains(a, b) ⇒ Object



101
102
103
# File 'lib/mushy/fluxs/filter.rb', line 101

def notcontains a, b
  contains(a, b) == false
end

#notequal(a, b) ⇒ Object



92
93
94
# File 'lib/mushy/fluxs/filter.rb', line 92

def notequal a, b
  equal(a, b) == false
end

#numeric?(value) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/mushy/fluxs/filter.rb', line 105

def numeric? value
  Float(value) != nil rescue false
end

#process(event, config) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mushy/fluxs/filter.rb', line 70

def process event, config

  differences = [:equal, :notequal, :contains, :notcontains]
    .select { |x| config[x].is_a? Hash }
    .map { |x| config[x].map { |k, v| { m: x, k: k, v1: v } } }
    .flatten
    .map { |x| x[:v2] = event[x[:k]] ; x }
    .map { |x| [x[:m], x[:v1], x[:v2]] }
    .reject { |x| self.send x[0], x[1], x[2] }

  differences.count == 0 ? event : nil

end