123456789_123456789_123456789_123456789_123456789_

Class: Octokit::Repository

Relationships & Source Files
Inherits: Object
Defined in: lib/octokit/repository.rb

Overview

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

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(repo) ⇒ Repository

Raises:

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 23

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
  validate_owner_and_name!(repo) if @owner && @name
end

Class Method Details

.from_url(url) ⇒ Repository

Instantiate from a GitHub repository URL

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 14

def self.from_url(url)
  new URI.parse(url).path[1..]
         .gsub(%r{^repos/}, '')
         .split('/', 3)[0..1]
         .join('/')
end

.path(repo) ⇒ String

Get the api path for a repo

Parameters:

  • repo (Integer, String, Hash, Repository)

    A GitHub repository.

Returns:

  • (String)

    Api path.

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 58

def self.path(repo)
  new(repo).path
end

Instance Attribute Details

#id (rw)

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 7

attr_accessor :owner, :name, :id

#name (rw) Also known as: #repo

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 7

attr_accessor :owner, :name, :id

#owner (rw) Also known as: #user, #username

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 7

attr_accessor :owner, :name, :id

#repo (readonly)

Alias for #name.

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 80

alias repo name

#user (readonly)

Alias for #owner.

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 78

alias user owner

#username (readonly)

Alias for #owner.

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 79

alias username owner

Instance Method Details

#id_api_pathString

Returns:

  • (String)

    Api path for id identified repos

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 68

def id_api_path
  "repositories/#{@id}"
end

#named_api_pathString

Returns:

  • (String)

    Api path for owner/name identified repos

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 63

def named_api_path
  "repos/#{slug}"
end

#pathString

Returns:

  • (String)

    Repository API path

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 49

def path
  return named_api_path if @owner && @name

  id_api_path if @id
end

#raise_invalid_repository!(repo) (private)

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 96

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

#slugString Also known as: #to_s

Repository owner/name

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 43

def slug
  "#{@owner}/#{@name}"
end

#to_s

Alias for #slug.

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 46

alias to_s slug

#urlString

Repository URL based on Octokit::Client#web_endpoint

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 74

def url
  "#{Octokit.web_endpoint}#{slug}"
end

#validate_owner_and_name!(repo) (private)

[ GitHub ]

  
# File 'lib/octokit/repository.rb', line 90

def validate_owner_and_name!(repo)
  if @owner.include?('/') || @name.include?('/') || !url.match?(ABS_URI_REGEXP)
    raise_invalid_repository!(repo)
  end
end