class Arachni::OptionGroups::Input

Holds options, and provides functionality, related to filling in inputs by name.

@author Tasos “Zapotek” Laskos <tasos.laskos@arachni-scanner.com>

Constants

DEFAULT
DEFAULT_VALUES

System default input values.

Attributes

default_values[RW]

@return [Hash<Regexp => String>]

Default values for {#values}.

@see DEFAULT_VALUES

force[RW]

@return [Bool]

Force {#fill} all inputs, not just the empty ones.
values[RW]

@return [Hash<Regexp => String, call>]

Patterns used to match input names and value to use to fill it in.
If the value is a callable object (like a `Proc`) its return value will
be used instead -- it will also be passed the name of the vector as an argument.
without_defaults[RW]

@return [Bool]

`true` if {#default_values} should be used, `false` otherwise.

Public Instance Methods

default_values=( v ) click to toggle source

@private

# File lib/arachni/option_groups/input.rb, line 131
def default_values=( v )
    @default_values = format_values( v ) || defaults[:default_values]
end
effective_values() click to toggle source

@return [Hash<Regexp => String>]

{#values}, merged with {#default_values} if {#without_defaults?}
# File lib/arachni/option_groups/input.rb, line 103
def effective_values
    without_defaults? ? @values : default_values.merge( @values )
end
fill( parameters ) click to toggle source

@note If {#force?} it will fill-in even non-empty inputs.

Tries to fill a hash with values of appropriate type based on the key of the parameter.

@param [Hash] parameters

Parameters hash.

@return [Hash]

# File lib/arachni/option_groups/input.rb, line 68
def fill( parameters )
    parameters = parameters.dup

    parameters.each do |k, v|
        next if !force? && !v.to_s.empty?

        value = value_for_name( k, false )

        # Don't overwrite the default values of the parameters unless we've
        # fot a value, even if #force? is in effect.
        if parameters[k].to_s.empty?
            parameters[k] = value || DEFAULT
        elsif value
            parameters[k] = value
        end
    end

    parameters
end
force?() click to toggle source

@return [Bool]

Force {#fill} all inputs, not just the empty ones.
# File lib/arachni/option_groups/input.rb, line 121
def force?
    !!@force
end
format_values( values ) click to toggle source

@private

# File lib/arachni/option_groups/input.rb, line 136
def format_values( values )
    return if !values

    values.inject({}) do |h, (regexp, value)|
        regexp = regexp.is_a?( Regexp ) ?
            regexp :
            Regexp.new( regexp.to_s, Regexp::IGNORECASE )
        h.merge!( regexp => value )
        h
    end
end
to_h() click to toggle source
Calls superclass method Arachni::OptionGroup#to_h
# File lib/arachni/option_groups/input.rb, line 148
def to_h
    h = super
    [:values, :default_values].each do |k|
        # We can't have blocks in there...
        h[k] = h[k].select{ |_, v| v.is_a? String }.
            inject({}) { |h2, (k2, v)| h2.merge k2.source => v }
    end
    h
end
update_values_from_file( location ) click to toggle source

@param [String] location

Location of a YAML file used to fill in {#values}.
# File lib/arachni/option_groups/input.rb, line 109
def update_values_from_file( location )
    @values.merge!( format_values( YAML.load_file( location ) ) )
end
value_for_name( name, use_default = true ) click to toggle source

@param [String] name

Input name to match against {#effective_values}.

@return [String, nil]

Value for the `name` or `nil` if none could be found.
# File lib/arachni/option_groups/input.rb, line 93
def value_for_name( name, use_default = true )
    effective_values.each do |k, v|
        return v.respond_to?( :call ) ? v.call( name ).to_s : v if name =~ k
    end

    use_default ? DEFAULT : nil
end
values=( v ) click to toggle source

@private

# File lib/arachni/option_groups/input.rb, line 126
def values=( v )
    @values = format_values( v ) || defaults[:values]
end
without_defaults?() click to toggle source

@return [Bool]

`true` if {#default_values} should be used, `false` otherwise.
# File lib/arachni/option_groups/input.rb, line 115
def without_defaults?
    !!@without_defaults
end