Class: GithubUrls::Parser
Instance Attribute Summary collapse
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #clean_url ⇒ Object
- #extractable_early? ⇒ Boolean
- #format_url ⇒ Object
- #github_website_url? ⇒ Boolean
-
#initialize(url) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
- #parseable? ⇒ Boolean
- #remove_anchors ⇒ Object
- #remove_auth_user ⇒ Object
- #remove_brackets ⇒ Object
- #remove_equals_sign ⇒ Object
- #remove_extra_segments ⇒ Object
- #remove_git_extension ⇒ Object
- #remove_git_scheme ⇒ Object
- #remove_github_domain ⇒ Object
- #remove_querystring ⇒ Object
- #remove_scheme ⇒ Object
- #remove_subdomain ⇒ Object
- #remove_whitespace ⇒ Object
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
#url ⇒ Object
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_url ⇒ Object
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
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_url ⇒ Object
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
102 103 104 |
# File 'lib/github_urls/parser.rb', line 102 def github_website_url? url.match(/www.github.(io|com|org)/i) end |
#parse ⇒ Object
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
50 51 52 |
# File 'lib/github_urls/parser.rb', line 50 def parseable? !url.nil? && url.include?('github') end |
#remove_anchors ⇒ Object
66 67 68 |
# File 'lib/github_urls/parser.rb', line 66 def remove_anchors url.gsub!(/(#\S*)$/i, '') end |
#remove_auth_user ⇒ Object
82 83 84 |
# File 'lib/github_urls/parser.rb', line 82 def remove_auth_user self.url = url.split('@')[-1] end |
#remove_brackets ⇒ Object
58 59 60 |
# File 'lib/github_urls/parser.rb', line 58 def remove_brackets url.gsub!(/>|<|\(|\)|\[|\]/, '') end |
#remove_equals_sign ⇒ Object
78 79 80 |
# File 'lib/github_urls/parser.rb', line 78 def remove_equals_sign self.url = url.split('=')[-1] end |
#remove_extra_segments ⇒ Object
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_extension ⇒ Object
74 75 76 |
# File 'lib/github_urls/parser.rb', line 74 def remove_git_extension url.gsub!(/(\.git|\/)$/i, '') end |
#remove_git_scheme ⇒ Object
70 71 72 |
# File 'lib/github_urls/parser.rb', line 70 def remove_git_scheme url.gsub!(/git\/\//i, '') end |
#remove_github_domain ⇒ Object
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_querystring ⇒ Object
62 63 64 |
# File 'lib/github_urls/parser.rb', line 62 def remove_querystring url.gsub!(/(\?\S*)$/i, '') end |
#remove_scheme ⇒ Object
90 91 92 |
# File 'lib/github_urls/parser.rb', line 90 def remove_scheme url.gsub!(/(((git|ssh|hg|svn|scm|http|https)+?:)+?)/i, '') end |
#remove_subdomain ⇒ Object
94 95 96 |
# File 'lib/github_urls/parser.rb', line 94 def remove_subdomain url.gsub!(/(www|ssh|raw|git|wiki)+?\./i, '') end |
#remove_whitespace ⇒ Object
86 87 88 |
# File 'lib/github_urls/parser.rb', line 86 def remove_whitespace url.gsub!(/\s/, '') end |