Class: CoachClient::User
Overview
A user resource of the CyberCoach service.
Constant Summary collapse
- LIST_ALL_SIZE =
The size of the requests for the list with all = true
1000
Instance Attribute Summary collapse
- #datecreated ⇒ Integer readonly
- #email ⇒ String
- #newpassword ⇒ String
- #partnerships ⇒ Array<CoachClient::Partnership> readonly
- #password ⇒ String
- #publicvisible ⇒ Integer
- #realname ⇒ String
- #subscriptions ⇒ Array<CoachClient::UserSubscription> readonly
- #username ⇒ String readonly
Attributes inherited from Resource
Class Method Summary collapse
-
.list(client, size: 20, start: 0, all: false) {|user| ... } ⇒ Array<CoachClient::User>
Returns a list of users from the CyberCoach service for which the given block returns a true value.
-
.path ⇒ String
Returns the relative path to the user resource.
-
.total(client) ⇒ Integer
Returns the total number of users present on the CyberCoach service.
Instance Method Summary collapse
-
#authenticated? ⇒ Boolean
Returns whether the user is authenticated.
-
#delete ⇒ true
Deletes the user on the CyberCoach service.
-
#initialize(client, username, info = {}) ⇒ CoachClient::User
constructor
Creates a new user.
-
#save ⇒ CoachClient::User
Saves the user to the CyberCoach service.
-
#to_s ⇒ String
Returns the string representation of the user.
-
#update ⇒ CoachClient::User
Updates the user with the data from the CyberCoach service.
-
#url ⇒ String
Returns the URL of the user.
Methods inherited from Resource
Constructor Details
#initialize(client, username, info = {}) ⇒ CoachClient::User
Creates a new user.
85 86 87 88 89 90 91 92 |
# File 'lib/coach_client/user.rb', line 85 def initialize(client, username, info = {}) super(client) @username = username @password = info[:password] @realname = info[:realname] @email = info[:email] @publicvisible = info[:publicvisible] end |
Instance Attribute Details
#datecreated ⇒ Integer (readonly)
11 12 13 |
# File 'lib/coach_client/user.rb', line 11 def datecreated @datecreated end |
#email ⇒ String
20 21 22 |
# File 'lib/coach_client/user.rb', line 20 def email @email end |
#newpassword ⇒ String
20 21 22 |
# File 'lib/coach_client/user.rb', line 20 def newpassword @newpassword end |
#partnerships ⇒ Array<CoachClient::Partnership> (readonly)
14 15 16 |
# File 'lib/coach_client/user.rb', line 14 def partnerships @partnerships end |
#password ⇒ String
20 21 22 |
# File 'lib/coach_client/user.rb', line 20 def password @password end |
#publicvisible ⇒ Integer
23 24 25 |
# File 'lib/coach_client/user.rb', line 23 def publicvisible @publicvisible end |
#realname ⇒ String
20 21 22 |
# File 'lib/coach_client/user.rb', line 20 def realname @realname end |
#subscriptions ⇒ Array<CoachClient::UserSubscription> (readonly)
17 18 19 |
# File 'lib/coach_client/user.rb', line 17 def subscriptions @subscriptions end |
#username ⇒ String (readonly)
8 9 10 |
# File 'lib/coach_client/user.rb', line 8 def username @username end |
Class Method Details
.list(client, size: 20, start: 0, all: false) {|user| ... } ⇒ Array<CoachClient::User>
Returns a list of users from the CyberCoach service for which the given block returns a true value.
If no block is given, the whole list is returned.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/coach_client/user.rb', line 54 def self.list(client, size: 20, start: 0, all: false) userlist = [] if all total = self.total(client) start = 0 size = LIST_ALL_SIZE end loop do response = CoachClient::Request.get(client.url + path, params: { start: start, size: size }) response.to_h[:users].each do |u| user = new(client, u[:username]) userlist << user if !block_given? || yield(user) end break unless all start += size break if start >= total end userlist end |
.path ⇒ String
Returns the relative path to the user resource.
28 29 30 |
# File 'lib/coach_client/user.rb', line 28 def self.path 'users/' end |
.total(client) ⇒ Integer
Returns the total number of users present on the CyberCoach service.
36 37 38 39 40 |
# File 'lib/coach_client/user.rb', line 36 def self.total(client) response = CoachClient::Request.get(client.url + path, params: { size: 0 }) response.to_h[:available] end |
Instance Method Details
#authenticated? ⇒ Boolean
Returns whether the user is authenticated.
166 167 168 169 |
# File 'lib/coach_client/user.rb', line 166 def authenticated? false if @password.nil? @client.authenticated?(@username, @password) end |
#delete ⇒ true
Deletes the user on the CyberCoach service.
157 158 159 160 161 |
# File 'lib/coach_client/user.rb', line 157 def delete fail CoachClient::NotFound.new(self), 'User not found' unless exist? CoachClient::Request.delete(url, username: @username, password: @password) true end |
#save ⇒ CoachClient::User
Saves the user to the CyberCoach service.
The user is created if it does not exist on the CyberCoach service, otherwise it tries to overwrite it.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/coach_client/user.rb', line 134 def save vals = to_h vals.delete(:username) vals.delete_if { |_k, v| v.nil? || v.to_s.empty? } vals[:password] = vals.delete(:newpassword) if vals[:newpassword] payload = Gyoku.xml(user: vals) response = CoachClient::Request.put(url, username: @username, password: @password, payload: payload, content_type: :xml) unless response.code == 200 || response.code == 201 fail CoachClient::NotSaved.new(self), 'Could not save user' end @password = vals[:password] @newpassword = nil self end |
#to_s ⇒ String
Returns the string representation of the user.
181 182 183 |
# File 'lib/coach_client/user.rb', line 181 def to_s @username.to_s end |
#update ⇒ CoachClient::User
Updates the user with the data from the CyberCoach service.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/coach_client/user.rb', line 98 def update response = CoachClient::Request.get(url, username: @username, password: @password) response = response.to_h @realname = response[:realname] @email = response[:email] @publicvisible = response[:publicvisible] @datecreated = response[:datecreated] @partnerships = [] unless response[:partnerships].nil? response[:partnerships].each do |p| users = CoachClient::Partnership.extract_users_from_uri(p[:uri]) users.reject! { |username| username == @username } @partnerships << CoachClient::Partnership.new(client, self, users.first) end end @subscriptions = [] unless response[:subscriptions].nil? response[:subscriptions].each do |s| sport = s[:uri].match(/\/(\w+)\/\z/).captures.first @subscriptions << CoachClient::UserSubscription.new(client, self, sport) end end self end |
#url ⇒ String
Returns the URL of the user.
174 175 176 |
# File 'lib/coach_client/user.rb', line 174 def url @client.url + self.class.path + @username end |