Class: Gitlab::Search::Params

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/gitlab/search/params.rb

Constant Summary collapse

SEARCH_CHAR_LIMIT =
4096
SEARCH_TERM_LIMIT =
64
MIN_TERM_LENGTH =
2
BOOLEAN_PARAMS =
%i[confidential exclude_forks include_archived].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, detect_abuse: true) ⇒ Params

Returns a new instance of Params.



21
22
23
24
25
26
27
28
# File 'lib/gitlab/search/params.rb', line 21

def initialize(params, detect_abuse: true)
  @raw_params      = process_params(params)
  @query_string    = strip_surrounding_whitespace(@raw_params[:search] || @raw_params[:term])
  @detect_abuse    = detect_abuse
  @abuse_detection = AbuseDetection.new(self) if @detect_abuse

  validate
end

Instance Attribute Details

#abuse_detectionObject (readonly)

Returns the value of attribute abuse_detection.



17
18
19
# File 'lib/gitlab/search/params.rb', line 17

def abuse_detection
  @abuse_detection
end

#query_stringObject (readonly) Also known as: search, term

Returns the value of attribute query_string.



17
18
19
# File 'lib/gitlab/search/params.rb', line 17

def query_string
  @query_string
end

#raw_paramsObject (readonly)

Returns the value of attribute raw_params.



17
18
19
# File 'lib/gitlab/search/params.rb', line 17

def raw_params
  @raw_params
end

Instance Method Details

#[](key) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gitlab/search/params.rb', line 30

def [](key)
  if respond_to? key
    # We have this logic here to support reading custom attributes
    # like @query_string
    #
    # This takes precedence over values in @raw_params
    public_send(key) # rubocop:disable GitlabSecurity/PublicSend -- metaprogramming is needed
  else
    raw_params[key]
  end
end

#abusive?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/gitlab/search/params.rb', line 46

def abusive?
  detect_abuse? && abuse_detection.errors.any?
end

#email_lookup?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/gitlab/search/params.rb', line 62

def email_lookup?
  search_terms.any? { |term| term =~ URI::MailTo::EMAIL_REGEXP }
end

#slice(*keys) ⇒ Object



42
43
44
# File 'lib/gitlab/search/params.rb', line 42

def slice(*keys)
  keys.index_with { |key| self[key] }.with_indifferent_access
end

#valid_query_length?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/gitlab/search/params.rb', line 50

def valid_query_length?
  return true unless errors.has_key? :query_string

  errors[:query_string].none? { |msg| msg.include? SEARCH_CHAR_LIMIT.to_s }
end

#valid_terms_count?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/gitlab/search/params.rb', line 56

def valid_terms_count?
  return true unless errors.has_key? :query_string

  errors[:query_string].none? { |msg| msg.include? SEARCH_TERM_LIMIT.to_s }
end

#validateObject



66
67
68
69
70
# File 'lib/gitlab/search/params.rb', line 66

def validate
  abuse_detection.validate if detect_abuse?

  super
end