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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, detect_abuse: true) ⇒ Params

Returns a new instance of Params.



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

def initialize(params, detect_abuse: true)
  @raw_params      = params.is_a?(Hash) ? params.with_indifferent_access : params.dup
  @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.



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

def abuse_detection
  @abuse_detection
end

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

Returns the value of attribute query_string.



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

def query_string
  @query_string
end

#raw_paramsObject (readonly)

Returns the value of attribute raw_params.



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

def raw_params
  @raw_params
end

Instance Method Details

#[](key) ⇒ Object



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

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
  else
    raw_params[key]
  end
end

#abusive?Boolean

Returns:

  • (Boolean)


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

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

#email_lookup?Boolean

Returns:

  • (Boolean)


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

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

#valid?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
# File 'lib/gitlab/search/params.rb', line 69

def valid?
  if detect_abuse?
    abuse_detection.valid? && super
  else
    super
  end
end

#valid_query_length?Boolean

Returns:

  • (Boolean)


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

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)


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

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



61
62
63
64
65
66
67
# File 'lib/gitlab/search/params.rb', line 61

def validate
  if detect_abuse?
    abuse_detection.validate
  end

  super
end