Class: Worklog::Github::Repository

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

Overview

Represents a GitHub repository with an owner and name.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



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

def name
  @name
end

#ownerObject

Returns the value of attribute owner

Returns:

  • (Object)

    the current value of owner



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

def owner
  @owner
end

Class Method Details

.from_url(url) ⇒ Hash?

Extracts the repository name and owner from a GitHub repository URL.

Examples:

repo = Worklog::Github::Repository.new
repo.repository_from_url('https://github.com/owner/repository')
# => <# owner: 'owner', repository: 'repository' }
repo.repository_from_url('owner/repository')
# => #<struct Worklog::Github::Repository owner="owner", name="repository">

Parameters:

  • url (String)

    The GitHub repository URL or owner/repository string.

Returns:

  • (Hash, nil)

    A hash with :owner and :repository keys, or nil if the URL is invalid.



18
19
20
21
22
23
24
# File 'lib/github/repository.rb', line 18

def self.from_url(url)
  match = url.match(%r{github\.com[:/](?<owner>[^/]+)/(?<repo>[^/]+)(?:\.git)?$})
  match = url.match(%r{^(?<owner>[^/]+)/(?<repo>[^/]+)$}) if match.nil?
  return nil if match.nil?

  Repository.new(owner: match[:owner], name: match[:repo])
end

Instance Method Details

#to_sString

Returns a string representation of the repository in the format “owner/name”.

Returns:

  • (String)

    The string representation of the repository.



28
29
30
# File 'lib/github/repository.rb', line 28

def to_s
  "#{owner}/#{name}"
end