Class: Geet::Github::User

Inherits:
Object
  • Object
show all
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.



13
14
15
16
# File 'lib/geet/github/user.rb', line 13

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



48
49
50
51
52
53
54
# File 'lib/geet/github/user.rb', line 48

def self.authenticated(api_interface, **_)
  api_path = '/user'

  response = api_interface.send_request(api_path)

  new(response.fetch('login'), api_interface)
end

.list_collaborators(api_interface) ⇒ Object

Returns an array of User instances



58
59
60
61
62
63
# File 'lib/geet/github/user.rb', line 58

def self.list_collaborators(api_interface, **)
  api_path = 'collaborators'
  response = api_interface.send_request(api_path, multipage: true)

  response.map { |user_entry| new(user_entry.fetch('login'), api_interface) }
end

.repo_permission(api_interface) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/geet/github/user.rb', line 67

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

  response = api_interface.send_request(api_path)

  permission = response.fetch('permission')

  check_permission!(permission)

  permission
end

Instance Method Details

#has_permission?(permission) ⇒ Boolean

See #repo_permission.

Returns:

  • (Boolean)


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

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)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/geet/github/user.rb', line 28

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