class GithubUrls::Parser

Attributes

url[RW]

Public Class Methods

new(url) click to toggle source
# File lib/github_urls/parser.rb, line 9
def initialize(url)
  @url = url
end
parse(url) click to toggle source
# File lib/github_urls/parser.rb, line 3
def self.parse(url)
  new(url).parse
end

Public Instance Methods

clean_url() click to toggle source
# 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?() click to toggle source
# 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() click to toggle source
# File lib/github_urls/parser.rb, line 111
def format_url
  return nil unless url.length == 2
  url.join('/')
end
github_domain?() click to toggle source
# File lib/github_urls/parser.rb, line 107
def github_domain?
  url.match(/github\.(io|com|org)/i)
end
github_website_url?() click to toggle source
# File lib/github_urls/parser.rb, line 103
def github_website_url?
  url.match(/www.github.(io|com|org)/i)
end
parse() click to toggle source
# 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?() click to toggle source
# File lib/github_urls/parser.rb, line 51
def parseable?
  !url.nil? && url.include?('github')
end
remove_anchors() click to toggle source
# File lib/github_urls/parser.rb, line 67
def remove_anchors
  url.gsub!(/(#\S*)$/i, '')
end
remove_auth_user() click to toggle source
# File lib/github_urls/parser.rb, line 83
def remove_auth_user
  self.url = url.split('@')[-1]
end
remove_brackets() click to toggle source
# File lib/github_urls/parser.rb, line 59
def remove_brackets
  url.gsub!(/>|<|\(|\)|\[|\]/, '')
end
remove_equals_sign() click to toggle source
# File lib/github_urls/parser.rb, line 79
def remove_equals_sign
  self.url = url.split('=')[-1]
end
remove_extra_segments() click to toggle source
# File lib/github_urls/parser.rb, line 55
def remove_extra_segments
  self.url = url.split('/').reject(&:blank?)[0..1]
end
remove_git_extension() click to toggle source
# File lib/github_urls/parser.rb, line 75
def remove_git_extension
  url.gsub!(/(\.git|\/)$/i, '')
end
remove_git_scheme() click to toggle source
# File lib/github_urls/parser.rb, line 71
def remove_git_scheme
  url.gsub!(/git\/\//i, '')
end
remove_github_domain() click to toggle source
# 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() click to toggle source
# File lib/github_urls/parser.rb, line 63
def remove_querystring
  url.gsub!(/(\?\S*)$/i, '')
end
remove_scheme() click to toggle source
# 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() click to toggle source
# File lib/github_urls/parser.rb, line 95
def remove_subdomain
  url.gsub!(/(www|ssh|raw|git|wiki)+?\./i, '')
end
remove_whitespace() click to toggle source
# File lib/github_urls/parser.rb, line 87
def remove_whitespace
  url.gsub!(/\s/, '')
end