Class: Geet::Github::User

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Shared::RepoPermissions
Defined in:
lib/geet/github/user.rb

Constant Summary

Constants included from Shared::RepoPermissions

Shared::RepoPermissions::ALL_PERMISSIONS, Shared::RepoPermissions::PERMISSION_ADMIN, Shared::RepoPermissions::PERMISSION_NONE, Shared::RepoPermissions::PERMISSION_READ, Shared::RepoPermissions::PERMISSION_WRITE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shared::RepoPermissions

#permission_greater_or_equal_to?

Constructor Details

#initialize(username, api_interface) ⇒ User

Returns a new instance of User.



19
20
21
22
# File 'lib/geet/github/user.rb', line 19

def initialize(username, api_interface)
  @username = username
  @api_interface = api_interface
end

Instance Attribute Details

#usernameObject (readonly)

Returns the value of attribute username.



11
12
13
# File 'lib/geet/github/user.rb', line 11

def username
  @username
end

Class Method Details

.authenticated(api_interface) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/geet/github/user.rb', line 65

def self.authenticated(api_interface)
  api_path = "/user"

  response = T.cast(api_interface.send_request(api_path), T::Hash[String, T.untyped])

   = T.cast(response.fetch("login"), String)

  new(, api_interface)
end

.list_collaborators(api_interface) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/geet/github/user.rb', line 80

def self.list_collaborators(api_interface)
  api_path = "collaborators"
  response = T.cast(api_interface.send_request(api_path, multipage: true), T::Array[T::Hash[String, T.untyped]])

  response.map do |user_entry|
     = T.cast(user_entry.fetch("login"), String)
    new(, api_interface)
  end
end

.repo_permission(api_interface) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/geet/github/user.rb', line 97

def self.repo_permission(api_interface)
  username = authenticated(api_interface).username
  api_path = "collaborators/#{username}/permission"

  response = T.cast(api_interface.send_request(api_path), T::Hash[String, T.untyped])

  permission = T.cast(response.fetch("permission"), String)

  check_permission!(permission)

  permission
end

Instance Method Details

#has_permission?(permission) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/geet/github/user.rb', line 31

def has_permission?(permission)
  user_permission = self.class.repo_permission(@api_interface)

  permission_greater_or_equal_to?(user_permission, permission)
end

#is_collaborator?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/geet/github/user.rb', line 40

def is_collaborator?
  api_path = "collaborators/#{@username}"

  begin
    @api_interface.send_request(api_path)

    # 204: user is a collaborator.
    true
  rescue Geet::Shared::HttpError => error
    # 404: not a collaborator.
    # Although the documentation mentions only 404, 403 is a valid response as well; it seems
    # that 404 is given on private repositories, while 403 on public ones ("Must have push
    # access to view repository collaborators.").
    #
    (error.code == 404 || error.code == 403) ? false : raise
  end
end