Class: CoachClient::User

Inherits:
Resource show all
Defined in:
lib/coach_client/user.rb

Overview

A user resource of the CyberCoach service.

Instance Attribute Summary collapse

Attributes inherited from Resource

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#exist?, #to_h

Constructor Details

#initialize(client, username, info = {}) ⇒ CoachClient::User

Creates a new user.

Parameters:

  • client (CoachClient::Client)
  • username (String)
  • info (Hash) (defaults to: {})

    additional user informations

Options Hash (info):

  • :password (String)
  • :realname (String)
  • :email (String)
  • :publicvisible (Integer)


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

#datecreatedInteger (readonly)

Returns:

  • (Integer)


8
9
10
# File 'lib/coach_client/user.rb', line 8

def datecreated
  @datecreated
end

#emailString

Returns:

  • (String)


17
18
19
# File 'lib/coach_client/user.rb', line 17

def email
  @email
end

#newpasswordString

Returns:

  • (String)


17
18
19
# File 'lib/coach_client/user.rb', line 17

def newpassword
  @newpassword
end

#partnershipsArray<CoachClient::Partnership> (readonly)

Returns:



11
12
13
# File 'lib/coach_client/user.rb', line 11

def partnerships
  @partnerships
end

#passwordString

Returns:

  • (String)


17
18
19
# File 'lib/coach_client/user.rb', line 17

def password
  @password
end

#publicvisibleInteger

Returns:

  • (Integer)


20
21
22
# File 'lib/coach_client/user.rb', line 20

def publicvisible
  @publicvisible
end

#realnameString

Returns:

  • (String)


17
18
19
# File 'lib/coach_client/user.rb', line 17

def realname
  @realname
end

#subscriptionsArray<CoachClient::UserSubscription> (readonly)

Returns:



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

def subscriptions
  @subscriptions
end

#usernameString (readonly)

Returns:

  • (String)


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.

Parameters:

  • client (CoachClient::Client)
  • size (Integer) (defaults to: 20)
  • start (Integer) (defaults to: 0)
  • all (Boolean) (defaults to: false)

Yield Parameters:

Yield Returns:

  • (Boolean)

    whether the user should be added to the list

Returns:



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

.pathString

Returns the relative path to the user resource.

Returns:

  • (String)

    the relative path



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.

Parameters:

Returns:

  • (Integer)

    the total number of users



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.

Returns:

  • (Boolean)


175
176
177
178
# File 'lib/coach_client/user.rb', line 175

def authenticated?
  false if @password.nil?
  @client.authenticated?(@username, @password)
end

#deletetrue

Deletes the user on the CyberCoach service.

Returns:

  • (true)

Raises:



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

#saveCoachClient::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.

Returns:

Raises:



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_sString

Returns the string representation of the user.

Returns:

  • (String)


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.

Parameters:

  • size (Integer) (defaults to: 20)

    the number of partnerships

  • start (Integer) (defaults to: 0)

    the start of partnerships list

  • all (Boolean) (defaults to: false)

    whether all partnerships are retrieved

Returns:

Raises:



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

#urlString

Returns the URL of the user.

Returns:

  • (String)

    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