class CloneGitFile::GithubRepoParser

Public Class Methods

new(file) click to toggle source
# File lib/clone_git_file/github_repo_parser.rb, line 5
def initialize(file)
  @file = file
end

Public Instance Methods

parse() click to toggle source
# File lib/clone_git_file/github_repo_parser.rb, line 9
def parse
  abort("Error: That url is not valid") unless valid_url?
  data                    = OpenStruct.new
  data.repo_url           = parse_repo_url
  data.repo_name          = parse_repo_name
  data.github_username    = parse_github_username
  data.file_relative_path = parse_file_relative_path
  data.branch_name        = parse_branch_name
  data
end
valid_url?() click to toggle source
# File lib/clone_git_file/github_repo_parser.rb, line 20
def valid_url?
  !!@file.match(%r{.+github\.com/[^/]+/[^/]+})
end

Private Instance Methods

has_branch?() click to toggle source
# File lib/clone_git_file/github_repo_parser.rb, line 48
def has_branch?
  is_branch? || is_tree?
end
is_branch?() click to toggle source
# File lib/clone_git_file/github_repo_parser.rb, line 58
def is_branch?
  @file.match(%r{#{Regexp.escape(parse_repo_url)}/(blob|tree)/[^/]+/.+})
end
is_tree?() click to toggle source
# File lib/clone_git_file/github_repo_parser.rb, line 62
def is_tree?
  @file.match(%r{#{Regexp.escape(parse_repo_url)}/tree/[^/]+$})
end
parse_branch_name() click to toggle source
# File lib/clone_git_file/github_repo_parser.rb, line 52
def parse_branch_name
  return nil unless has_branch?

  @file.match(%r{#{Regexp.escape(parse_repo_url)}/(?:blob|tree)/([^/]+)})[1]
end
parse_file_relative_path() click to toggle source
# File lib/clone_git_file/github_repo_parser.rb, line 38
def parse_file_relative_path
  path = @file.match(%r{#{Regexp.escape(parse_repo_url)}/(.+)$})
  path = path.nil? ? "" : path[1]
  path.sub!(%r{^(?:blob|tree)/[^/]+}, '') if has_branch?
  path.sub!(%r{^/}, '')
  path == "" ? nil : path
rescue NoMethodError
  ''
end
parse_github_username() click to toggle source
# File lib/clone_git_file/github_repo_parser.rb, line 26
def parse_github_username
  @file.match(%r{.+github\.com/([^/]+)})[1]
end
parse_repo_name() click to toggle source
# File lib/clone_git_file/github_repo_parser.rb, line 34
def parse_repo_name
  @file.match(%r{.+github\.com/[^/]+/([^/]+)})[1]
end
parse_repo_url() click to toggle source
# File lib/clone_git_file/github_repo_parser.rb, line 30
def parse_repo_url
  @file.match(%r{.+github\.com/[^/]+/[^/]+})[0]
end