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_domain? ⇒ Boolean
- #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 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
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_url ⇒ Object
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
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
103 104 105 |
# File 'lib/github_urls/parser.rb', line 103 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
51 52 53 |
# File 'lib/github_urls/parser.rb', line 51 def parseable? !url.nil? && url.include?('github') end |
#remove_anchors ⇒ Object
67 68 69 |
# File 'lib/github_urls/parser.rb', line 67 def remove_anchors url.gsub!(/(#\S*)$/i, '') end |
#remove_auth_user ⇒ Object
83 84 85 |
# File 'lib/github_urls/parser.rb', line 83 def remove_auth_user self.url = url.split('@')[-1] end |
#remove_brackets ⇒ Object
59 60 61 |
# File 'lib/github_urls/parser.rb', line 59 def remove_brackets url.gsub!(/>|<|\(|\)|\[|\]/, '') end |
#remove_equals_sign ⇒ Object
79 80 81 |
# File 'lib/github_urls/parser.rb', line 79 def remove_equals_sign self.url = url.split('=')[-1] end |
#remove_extra_segments ⇒ Object
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_extension ⇒ Object
75 76 77 |
# File 'lib/github_urls/parser.rb', line 75 def remove_git_extension url.gsub!(/(\.git|\/)$/i, '') end |
#remove_git_scheme ⇒ Object
71 72 73 |
# File 'lib/github_urls/parser.rb', line 71 def remove_git_scheme url.gsub!(/git\/\//i, '') end |
#remove_github_domain ⇒ Object
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_querystring ⇒ Object
63 64 65 |
# File 'lib/github_urls/parser.rb', line 63 def remove_querystring url.gsub!(/(\?\S*)$/i, '') end |
#remove_scheme ⇒ Object
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_subdomain ⇒ Object
95 96 97 |
# File 'lib/github_urls/parser.rb', line 95 def remove_subdomain url.gsub!(/(www|ssh|raw|git|wiki)+?\./i, '') end |
#remove_whitespace ⇒ Object
87 88 89 |
# File 'lib/github_urls/parser.rb', line 87 def remove_whitespace url.gsub!(/\s/, '') end |