Class: GithubUrls::Parser

Inherits:
Object show all
Defined in:
lib/github_urls/parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Parser

Returns a new instance of Parser.



9
10
11
# File 'lib/github_urls/parser.rb', line 9

def initialize(url)
  @url = url
end

Instance Attribute Details

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/github_urls/parser.rb', line 7

def url
  @url
end

Class Method Details

.parse(url) ⇒ Object



3
4
5
# File 'lib/github_urls/parser.rb', line 3

def self.parse(url)
  new(url).parse
end

Instance Method Details

#clean_urlObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/github_urls/parser.rb', line 23

def clean_url
  remove_whitespace
  remove_brackets
  remove_anchors
  remove_querystring
  remove_auth_user
  remove_equals_sign
  remove_scheme
  remove_subdomain
  remove_github_domain
  remove_git_extension
  remove_git_scheme
  remove_extra_segments
  format_url
end

#extractable_early?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
# File 'lib/github_urls/parser.rb', line 39

def extractable_early?
  return false if github_website_url?

  match = url.match(/([\w\.@\:\-_~]+)\.github\.(io|com|org)\/([\w\.@\:\-\_\~]+)/i)
  if match && match.length == 4
    return "#{match[1]}/#{match[3]}"
  end

  nil
end

#format_urlObject



106
107
108
109
# File 'lib/github_urls/parser.rb', line 106

def format_url
  return nil unless url.length == 2
  url.join('/')
end

#github_website_url?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/github_urls/parser.rb', line 102

def github_website_url?
  url.match(/www.github.(io|com|org)/i)
end

#parseObject



13
14
15
16
17
18
19
20
21
# File 'lib/github_urls/parser.rb', line 13

def parse
  return nil unless parseable?

  if url = extractable_early?
    url
  else
    clean_url
  end
end

#parseable?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/github_urls/parser.rb', line 50

def parseable?
  !url.nil? && url.include?('github')
end

#remove_anchorsObject



66
67
68
# File 'lib/github_urls/parser.rb', line 66

def remove_anchors
  url.gsub!(/(#\S*)$/i, '')
end

#remove_auth_userObject



82
83
84
# File 'lib/github_urls/parser.rb', line 82

def remove_auth_user
  self.url = url.split('@')[-1]
end

#remove_bracketsObject



58
59
60
# File 'lib/github_urls/parser.rb', line 58

def remove_brackets
  url.gsub!(/>|<|\(|\)|\[|\]/, '')
end

#remove_equals_signObject



78
79
80
# File 'lib/github_urls/parser.rb', line 78

def remove_equals_sign
  self.url = url.split('=')[-1]
end

#remove_extra_segmentsObject



54
55
56
# File 'lib/github_urls/parser.rb', line 54

def remove_extra_segments
  self.url = url.split('/').reject(&:blank?)[0..1]
end

#remove_git_extensionObject



74
75
76
# File 'lib/github_urls/parser.rb', line 74

def remove_git_extension
  url.gsub!(/(\.git|\/)$/i, '')
end

#remove_git_schemeObject



70
71
72
# File 'lib/github_urls/parser.rb', line 70

def remove_git_scheme
  url.gsub!(/git\/\//i, '')
end

#remove_github_domainObject



98
99
100
# File 'lib/github_urls/parser.rb', line 98

def remove_github_domain
  url.gsub!(/(github.io|github.com|github.org|raw.githubusercontent.com)+?(:|\/)?/i, '')
end

#remove_querystringObject



62
63
64
# File 'lib/github_urls/parser.rb', line 62

def remove_querystring
  url.gsub!(/(\?\S*)$/i, '')
end

#remove_schemeObject



90
91
92
# File 'lib/github_urls/parser.rb', line 90

def remove_scheme
  url.gsub!(/(((git\+https|git|ssh|hg|svn|scm|http|https)+?:)+?)/i, '')
end

#remove_subdomainObject



94
95
96
# File 'lib/github_urls/parser.rb', line 94

def remove_subdomain
  url.gsub!(/(www|ssh|raw|git|wiki)+?\./i, '')
end

#remove_whitespaceObject



86
87
88
# File 'lib/github_urls/parser.rb', line 86

def remove_whitespace
  url.gsub!(/\s/, '')
end