Class: Octopi::User

Inherits:
Base
  • Object
show all
Includes:
Resource
Defined in:
lib/octopi/user.rb

Constant Summary

Constants inherited from Base

Base::VALID

Instance Attribute Summary

Attributes inherited from Base

#api

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource

for, included

Methods inherited from Base

#initialize, #property, #save

Constructor Details

This class inherits a constructor from Octopi::Base

Class Method Details

.find(username) ⇒ Object

Finds a single user identified by the given username

Example:

user = User.find("fcoury")
puts user. # should return 'fcoury'


14
15
16
17
# File 'lib/octopi/user.rb', line 14

def self.find(username)
  self.validate_args(username => :user)
  super username
end

.find_all(username) ⇒ Object

Finds all users whose username matches a given string

Example:

User.find_all("oe") # Matches joe, moe and monroe


25
26
27
28
# File 'lib/octopi/user.rb', line 25

def self.find_all(username)
  self.validate_args(username => :user)
  super username
end

Instance Method Details

#add_key(title, key) ⇒ Object

Adds an SSH Public Key to the user. Requires authentication.

Raises:



55
56
57
58
59
60
61
62
63
64
# File 'lib/octopi/user.rb', line 55

def add_key(title, key)
  raise APIError, 
    "To add a key, you must be authenticated" if @api.read_only?

  result = @api.post("/user/key/add", :title => title, :key => key)
  return if !result["public_keys"]
  key_params = result["public_keys"].select { |k| k["title"] == title }
  return if !key_params or key_params.empty?
  Key.new(@api, key_params.first, self)
end

#create_repository(name, opts = {}) ⇒ Object



48
49
50
51
# File 'lib/octopi/user.rb', line 48

def create_repository(name, opts = {})
  self.class.validate_args(name => :repo)
  Repository.create(self, name, opts)
end

#keysObject

Returns a list of Key objects containing all SSH Public Keys this user currently has. Requires authentication.

Raises:



68
69
70
71
72
73
74
75
# File 'lib/octopi/user.rb', line 68

def keys
  raise APIError, 
    "To add a key, you must be authenticated" if @api.read_only?

  result = @api.get("/user/keys")
  return unless result and result["public_keys"]
  result["public_keys"].inject([]) { |result, element| result << Key.new(@api, element) }
end

#repositoriesObject

Returns a collection of Repository objects, containing all repositories of the user.

If user is the current authenticated user, some additional information will be provided for the Repositories.



36
37
38
39
# File 'lib/octopi/user.rb', line 36

def repositories
  api = self.api || ANONYMOUS_API
  Repository.find_by_user(,api)
end

#repository(name) ⇒ Object

Searches for user Repository identified by name



43
44
45
46
# File 'lib/octopi/user.rb', line 43

def repository(name)
  self.class.validate_args(name => :repo)
  Repository.find(, name)
end

#user_property(property, deep) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/octopi/user.rb', line 88

def user_property(property, deep)
  users = []
  property(property, ).each_pair do |k,v|
    return v unless deep
    
    v.each { |u| users << User.find(u) } 
  end
  
  users
end