Class: Eso::Filter::FilterItem
- Inherits:
-
Object
- Object
- Eso::Filter::FilterItem
- Defined in:
- lib/eso/filter.rb
Instance Attribute Summary collapse
-
#operands ⇒ Object
Array containing the values to filter on.
-
#operator ⇒ Object
Examples are “OR”, “IN”, “CONTAINS”.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(type:, operator:, operands:) ⇒ FilterItem
constructor
A new instance of FilterItem.
- #process_operands(operands) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(type:, operator:, operands:) ⇒ FilterItem
Returns a new instance of FilterItem.
47 48 49 50 51 |
# File 'lib/eso/filter.rb', line 47 def initialize(type:, operator:, operands:) @type = "#{type}_ITEM" @operator = operator process_operands(operands) end |
Instance Attribute Details
#operands ⇒ Object
Array containing the values to filter on
45 46 47 |
# File 'lib/eso/filter.rb', line 45 def operands @operands end |
#operator ⇒ Object
Examples are “OR”, “IN”, “CONTAINS”. These should probably be constantized somewhere.
42 43 44 |
# File 'lib/eso/filter.rb', line 42 def operator @operator end |
#type ⇒ Object
Returns the value of attribute type.
40 41 42 |
# File 'lib/eso/filter.rb', line 40 def type @type end |
Instance Method Details
#process_operands(operands) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/eso/filter.rb', line 53 def process_operands(operands) @operands = if ["IS_EMPTY", "IS_NOT_EMPTY"].include? @operator nil elsif @type == "#{Eso::Filters::IP_ADDRESS}_ITEM" || @type == "#{Eso::Filters::IP_RANGE}_ITEM" || @type == "#{Eso::Filters::OPEN_PORT}_ITEM" || @type == "#{Eso::Filters::RISK_SCORE}_ITEM" || @type == "#{Eso::Filters::CVSS_SCORE}_ITEM" operands.first.split('-') else operands end if @operands == nil return end @operands.map! do |value| # This regex is used to determine if the string is actually a float. # http://stackoverflow.com/questions/1034418/determine-if-a-string-is-a-valid-float-value if value =~ /^\s*[+-]?((\d+_?)*\d+(\.(\d+_?)*\d+)?|\.(\d+_?)*\d+)(\s*|([eE][+-]?(\d+_?)*\d+)\s*)$/ if (@type == "#{Eso::Filters::OPEN_PORT}_ITEM") value.to_i else value.to_f end # If it's not a float, let's see if it's an integer. elsif value.to_i.to_s == value value.to_i # Guess not, so lets keep the original value. else value end end end |
#to_hash ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/eso/filter.rb', line 89 def to_hash hash = { valueClass: "Object", objectType: @type, properties: { operator: { valueClass: "String", value: @operator } } } # Currently there are no standards that say how many operands a filter can have operand_hash = {} operand_counter = 1 unless @operands.nil? @operands.each do |operand| label = "operand#{operand_counter}".to_sym # A correct value class is required because Jackson expects it. # A Jackson processor for Ruby would probably make this much nicer # Also, defaulting to Number is probably a bad idea, but based on current possible values in ESO this works. case operand.class.to_s when "String" value_class = "String" when "Array" value_class = "Array" when "Fixnum" value_class = "Integer" else value_class = "Number" end operand_hash[label] = { valueClass: value_class, value: operand } operand_counter += 1 end hash[:properties].merge! operand_hash end hash end |