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
38
# 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
  return nil unless github_domain?
  remove_subdomain
  remove_github_domain
  remove_git_extension
  remove_git_scheme
  remove_extra_segments
  format_url
end

#extractable_early?Boolean

Returns:

  • (Boolean)


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

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



111
112
113
114
# File 'lib/github_urls/parser.rb', line 111

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

#github_domain?Boolean

Returns:

  • (Boolean)


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

def github_domain?
  url.match(/github\.(io|com|org)/i)
end

#github_website_url?Boolean

Returns:

  • (Boolean)


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

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)


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

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

#remove_anchorsObject



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

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

#remove_auth_userObject



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

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

#remove_bracketsObject



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

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

#remove_equals_signObject



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

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

#remove_extra_segmentsObject



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

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

#remove_git_extensionObject



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

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

#remove_git_schemeObject



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

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

#remove_github_domainObject



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

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

#remove_querystringObject



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

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

#remove_schemeObject



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

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

#remove_subdomainObject



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

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

#remove_whitespaceObject



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

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