Class: Gitlab::Slug::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/slug/path.rb

Constant Summary collapse

LEADING_DASHES =
/\A-+/
EXTRACT_LOCAL_EMAIL_PART =

Eextract local email part if given an email. Will remove @ sign and everything following it.

/@.*\z/
FORBIDDEN_CHARACTERS =
/[^a-zA-Z0-9_\-.]/
PATH_TRAILING_VIOLATIONS =
%w[.git .atom .].freeze
DEFAULT_SLUG =
'blank'

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Path

Returns a new instance of Path.



13
14
15
# File 'lib/gitlab/slug/path.rb', line 13

def initialize(input)
  @input = input.dup.to_s
end

Instance Method Details

#generateObject Also known as: to_s



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gitlab/slug/path.rb', line 17

def generate
  slug = input.gsub(EXTRACT_LOCAL_EMAIL_PART, "")
  slug = slug.gsub(FORBIDDEN_CHARACTERS, "")

  # Remove trailing violations ('.atom', '.git', or '.')
  loop do
    orig = slug
    PATH_TRAILING_VIOLATIONS.each { |extension| slug = slug.chomp extension }
    break if orig == slug
  end
  slug = slug.sub(LEADING_DASHES, "")

  # If all characters were of forbidden nature and filtered out we use this
  # fallback to avoid empty paths
  slug = DEFAULT_SLUG if slug.blank?

  slug
end