Class: CoachClient::User

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

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

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)


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

#datecreatedInteger (readonly)

Returns:

  • (Integer)


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

def datecreated
  @datecreated
end

#emailString

Returns:

  • (String)


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

def email
  @email
end

#newpasswordString

Returns:

  • (String)


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

def newpassword
  @newpassword
end

#partnershipsArray<CoachClient::Partnership> (readonly)

Returns:



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

def partnerships
  @partnerships
end

#passwordString

Returns:

  • (String)


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

def password
  @password
end

#publicvisibleInteger

Returns:

  • (Integer)


23
24
25
# File 'lib/coach_client/user.rb', line 23

def publicvisible
  @publicvisible
end

#realnameString

Returns:

  • (String)


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

def realname
  @realname
end

#subscriptionsArray<CoachClient::UserSubscription> (readonly)

Returns:



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

def subscriptions
  @subscriptions
end

#usernameString (readonly)

Returns:

  • (String)


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.

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:



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

.pathString

Returns the relative path to the user resource.

Returns:

  • (String)

    the relative path



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.

Parameters:

Returns:

  • (Integer)

    the total number of users



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.

Returns:

  • (Boolean)


166
167
168
169
# File 'lib/coach_client/user.rb', line 166

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

#deletetrue

Deletes the user on the CyberCoach service.

Returns:

  • (true)

Raises:



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

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



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_sString

Returns the string representation of the user.

Returns:

  • (String)


181
182
183
# File 'lib/coach_client/user.rb', line 181

def to_s
  @username.to_s
end

#updateCoachClient::User

Updates the user with the data from the CyberCoach service.

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
# 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

#urlString

Returns the URL of the user.

Returns:

  • (String)

    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