Class: Crowd::Client::User
- Inherits:
-
Object
- Object
- Crowd::Client::User
- Defined in:
- lib/crowd-client/user.rb
Instance Attribute Summary collapse
-
#new_record ⇒ Object
Returns the value of attribute new_record.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #active ⇒ Object
- #authenticate?(password) ⇒ Boolean
- #change_password(password) ⇒ Object
- #destroy ⇒ Object
- #display_name ⇒ Object
- #email ⇒ Object
- #first_name ⇒ Object
- #groups(reload = false) ⇒ Object
-
#initialize(username, attributes = nil) ⇒ User
constructor
A new instance of User.
- #last_name ⇒ Object
- #reload! ⇒ Object
- #save ⇒ Object
- #update_attributes(attributes) ⇒ Object
Constructor Details
#initialize(username, attributes = nil) ⇒ User
Returns a new instance of User.
6 7 8 9 10 |
# File 'lib/crowd-client/user.rb', line 6 def initialize(username, attributes=nil) self.username = username self.new_record = true assign_attributes attributes, true end |
Instance Attribute Details
#new_record ⇒ Object
Returns the value of attribute new_record.
4 5 6 |
# File 'lib/crowd-client/user.rb', line 4 def new_record @new_record end |
#username ⇒ Object
Returns the value of attribute username.
4 5 6 |
# File 'lib/crowd-client/user.rb', line 4 def username @username end |
Class Method Details
.create(attributes) ⇒ Object
12 13 14 |
# File 'lib/crowd-client/user.rb', line 12 def self.create(attributes) new('', attributes).tap {|user| user.save } end |
Instance Method Details
#==(other) ⇒ Object
16 17 18 |
# File 'lib/crowd-client/user.rb', line 16 def ==(other) username == (other && other.username) end |
#active ⇒ Object
36 37 38 |
# File 'lib/crowd-client/user.rb', line 36 def active user_attributes['active'] end |
#authenticate?(password) ⇒ Boolean
83 84 85 86 87 88 |
# File 'lib/crowd-client/user.rb', line 83 def authenticate?(password) response = connection.post('authentication', :value => password) do |request| request.params[:username] = username end response.status == 200 end |
#change_password(password) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/crowd-client/user.rb', line 90 def change_password(password) response = connection.put('user/password', :value => password) do |request| request.params[:username] = username end raise ::Crowd::Client::Exception::NotFound.new("User '#{user.username}' was not found") if response.status == 404 raise ::Crowd::Client::Exception::UnknownError.new(response.body) if response.status != 204 end |
#destroy ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/crowd-client/user.rb', line 75 def destroy response = connection.delete('user') do |request| request.params[:username] = username end raise ::Crowd::Client::Exception::NotFound.new("User '#{user.username}' was not found") if response.status == 404 raise ::Crowd::Client::Exception::UnknownError.new(response.body) if response.status != 204 end |
#display_name ⇒ Object
28 29 30 |
# File 'lib/crowd-client/user.rb', line 28 def display_name user_attributes['display-name'] end |
#email ⇒ Object
32 33 34 |
# File 'lib/crowd-client/user.rb', line 32 def email user_attributes['email'] end |
#first_name ⇒ Object
20 21 22 |
# File 'lib/crowd-client/user.rb', line 20 def first_name user_attributes['first-name'] end |
#groups(reload = false) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/crowd-client/user.rb', line 40 def groups(reload=false) return @groups if @groups && !reload response = connection.get("user/group/nested") do |request| request.params[:username] = username end raise ::Crowd::Client::Exception::NotFound.new("User '#{user.username}' was not found") if response.status == 404 @groups = response.body['groups'].collect do |group| ::Crowd::Client::Group.new(group['name']) end end |
#last_name ⇒ Object
24 25 26 |
# File 'lib/crowd-client/user.rb', line 24 def last_name user_attributes['last-name'] end |
#reload! ⇒ Object
51 52 53 54 |
# File 'lib/crowd-client/user.rb', line 51 def reload! @groups = nil user_attributes(true) end |
#save ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/crowd-client/user.rb', line 61 def save if new_record response = connection.post('user', {'name' => username, 'active' => true}.merge(@attributes)) self.new_record = false raise ::Crowd::Client::Exception::UnknownError.new(response.body) if response.status != 201 else response = connection.put('user', {'name' => username}.merge(@attributes)) do |request| request.params[:username] = username end raise ::Crowd::Client::Exception::NotFound.new("User with username '#{username}' was not found.") if response.status == 404 raise ::Crowd::Client::Exception::UnknownError.new(response.body) if response.status != 204 end end |
#update_attributes(attributes) ⇒ Object
56 57 58 59 |
# File 'lib/crowd-client/user.rb', line 56 def update_attributes(attributes) assign_attributes attributes save end |