Class: CoachClient::User
Overview
A user resource of the CyberCoach service.
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(size: 20, start: 0, all: false) ⇒ 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.
82 83 84 85 86 87 88 89 |
# File 'lib/coach_client/user.rb', line 82 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)
8 9 10 |
# File 'lib/coach_client/user.rb', line 8 def datecreated @datecreated end |
#email ⇒ String
17 18 19 |
# File 'lib/coach_client/user.rb', line 17 def email @email end |
#newpassword ⇒ String
17 18 19 |
# File 'lib/coach_client/user.rb', line 17 def newpassword @newpassword end |
#partnerships ⇒ Array<CoachClient::Partnership> (readonly)
11 12 13 |
# File 'lib/coach_client/user.rb', line 11 def partnerships @partnerships end |
#password ⇒ String
17 18 19 |
# File 'lib/coach_client/user.rb', line 17 def password @password end |
#publicvisible ⇒ Integer
20 21 22 |
# File 'lib/coach_client/user.rb', line 20 def publicvisible @publicvisible end |
#realname ⇒ String
17 18 19 |
# File 'lib/coach_client/user.rb', line 17 def realname @realname end |
#subscriptions ⇒ Array<CoachClient::UserSubscription> (readonly)
14 15 16 |
# File 'lib/coach_client/user.rb', line 14 def subscriptions @subscriptions end |
#username ⇒ String (readonly)
5 6 7 |
# File 'lib/coach_client/user.rb', line 5 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.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/coach_client/user.rb', line 51 def self.list(client, size: 20, start: 0, all: false) userlist = [] if all total = self.total(client) start = 0 size = client.max_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.
25 26 27 |
# File 'lib/coach_client/user.rb', line 25 def self.path 'users/' end |
.total(client) ⇒ Integer
Returns the total number of users present on the CyberCoach service.
33 34 35 36 37 |
# File 'lib/coach_client/user.rb', line 33 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.
175 176 177 178 |
# File 'lib/coach_client/user.rb', line 175 def authenticated? false if @password.nil? @client.authenticated?(@username, @password) end |
#delete ⇒ true
Deletes the user on the CyberCoach service.
166 167 168 169 170 |
# File 'lib/coach_client/user.rb', line 166 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.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/coach_client/user.rb', line 143 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.
190 191 192 |
# File 'lib/coach_client/user.rb', line 190 def to_s @username.to_s end |
#update(size: 20, start: 0, all: false) ⇒ 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 123 124 125 126 127 128 129 130 131 |
# File 'lib/coach_client/user.rb', line 98 def update(size: 20, start: 0, all: false) response = {} if all start = 0 size = @client.max_size end @partnerships = [] loop do response = CoachClient::Request.get(url, username: @username, password: @password, params: { start: start, size: size }) response = response.to_h break if 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 break unless all && next?(response[:links]) start += size end @realname = response[:realname] @email = response[:email] @publicvisible = response[:publicvisible] @datecreated = response[:datecreated] @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.
183 184 185 |
# File 'lib/coach_client/user.rb', line 183 def url @client.url + self.class.path + @username end |