class Octokit::Repository

Class to parse GitHub repository owner and name from URLs and to generate URLs

Constants

NAME_WITH_OWNER_PATTERN

Attributes

id[RW]
name[RW]
owner[RW]
repo[RW]
user[RW]
username[RW]

Public Class Methods

from_url(url) click to toggle source

Instantiate from a GitHub repository URL

@return [Repository]

# File lib/octokit/repository.rb, line 12
def self.from_url(url)
  new URI.parse(url).path[1..-1].
    gsub(/^repos\//,'').
    split('/', 3)[0..1].
    join('/')
end
new(repo) click to toggle source

@raise [Octokit::InvalidRepository] if the repository

has an invalid format
# File lib/octokit/repository.rb, line 21
def initialize(repo)
  case repo
  when Integer
    @id = repo
  when NAME_WITH_OWNER_PATTERN
    @owner, @name = repo.split("/")
  when Repository
    @owner = repo.owner
    @name = repo.name
  when Hash
    @name = repo[:repo] || repo[:name]
    @owner = repo[:owner] || repo[:user] || repo[:username]
  else
    raise_invalid_repository!(repo)
  end
  if @owner && @name
    validate_owner_and_name!(repo)
  end
end
path(repo) click to toggle source

Get the api path for a repo @param repo [Integer, String, Hash, Repository] A GitHub repository. @return [String] Api path.

# File lib/octokit/repository.rb, line 57
def self.path repo
  new(repo).path
end

Public Instance Methods

id_api_path() click to toggle source

@return [String] Api path for id identified repos

# File lib/octokit/repository.rb, line 67
def id_api_path
  "repositories/#{@id}"
end
named_api_path() click to toggle source

@return [String] Api path for owner/name identified repos

# File lib/octokit/repository.rb, line 62
def named_api_path
  "repos/#{slug}"
end
path() click to toggle source

@return [String] Repository API path

# File lib/octokit/repository.rb, line 49
def path
  return named_api_path if @owner && @name
  return id_api_path if @id
end
slug() click to toggle source

Repository owner/name @return [String]

# File lib/octokit/repository.rb, line 43
def slug
  "#{@owner}/#{@name}"
end
Also aliased as: to_s
to_s()
Alias for: slug
url() click to toggle source

Repository URL based on {Octokit::Client#web_endpoint} @return [String]

# File lib/octokit/repository.rb, line 73
def url
  "#{Octokit.web_endpoint}#{slug}"
end

Private Instance Methods

raise_invalid_repository!(repo) click to toggle source
# File lib/octokit/repository.rb, line 89
def raise_invalid_repository!(repo)
  msg = "#{repo.inspect} is invalid as a repository identifier. " +
        "Use the user/repo (String) format, or the repository ID (Integer), or a hash containing :repo and :user keys."
  raise Octokit::InvalidRepository, msg
end
validate_owner_and_name!(repo) click to toggle source
# File lib/octokit/repository.rb, line 83
def validate_owner_and_name!(repo)
  if @owner.include?('/') || @name.include?('/') || !url.match(URI::ABS_URI)
    raise_invalid_repository!(repo)
  end
end