Class: Octokit::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/octokit/repository.rb

Overview

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

Constant Summary collapse

NAME_WITH_OWNER_PATTERN =
%r{\A[\w.-]+/[\w.-]+\z}i.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo) ⇒ Repository

Returns a new instance of Repository.

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

Instance Attribute Details

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#nameObject Also known as: repo

Returns the value of attribute name.



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

def name
  @name
end

#ownerObject Also known as: user, username

Returns the value of attribute owner.



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

def owner
  @owner
end

Class Method Details

.from_url(url) ⇒ Repository

Instantiate from a GitHub repository URL

Returns:



14
15
16
17
18
19
# 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.



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

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

Instance Method Details

#id_api_pathString

Returns Api path for id identified repos.

Returns:

  • (String)

    Api path for id identified repos



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

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

#named_api_pathString

Returns Api path for owner/name identified repos.

Returns:

  • (String)

    Api path for owner/name identified repos



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

def named_api_path
  "repos/#{slug}"
end

#pathString

Returns Repository API path.

Returns:

  • (String)

    Repository API path



49
50
51
52
53
# File 'lib/octokit/repository.rb', line 49

def path
  return named_api_path if @owner && @name

  id_api_path if @id
end

#slugString Also known as: to_s

Repository owner/name

Returns:

  • (String)


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

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

#urlString

Repository URL based on Client#web_endpoint

Returns:

  • (String)


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

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