coach_client 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +17 -0
- data/.rubocop.yml +1174 -0
- data/README.md +14 -13
- data/Rakefile +1 -1
- data/coach_client.gemspec +3 -1
- data/lib/coach_client/entry.rb +23 -42
- data/lib/coach_client/partnership.rb +65 -73
- data/lib/coach_client/partnership_subscription.rb +6 -6
- data/lib/coach_client/request.rb +5 -0
- data/lib/coach_client/sport.rb +1 -2
- data/lib/coach_client/subscription.rb +8 -24
- data/lib/coach_client/user.rb +11 -32
- data/lib/coach_client/version.rb +1 -1
- metadata +32 -2
data/README.md
CHANGED
@@ -1,10 +1,16 @@
|
|
1
1
|
# CoachClient
|
2
2
|
[![Gem Version](https://badge.fury.io/rb/coach_client.svg)](https://badge.fury.io/rb/coach_client)
|
3
|
+
[![Code Climate](https://codeclimate.com/github/jungomi/coach_client/badges/gpa.svg)](https://codeclimate.com/github/jungomi/coach_client)
|
4
|
+
[![Test Coverage](https://codeclimate.com/github/jungomi/coach_client/badges/coverage.svg)](https://codeclimate.com/github/jungomi/coach_client/coverage)
|
3
5
|
|
4
6
|
A wrapper around the
|
5
7
|
[CyberCoach](https://diuf.unifr.ch/drupal/softeng/teaching/studentprojects/cyber-coach-rest)
|
6
8
|
API of the University of Fribourg.
|
7
9
|
|
10
|
+
- [Rubygem website](https://rubygems.org/gems/coach_client)
|
11
|
+
- [API Documentation](http://www.rubydoc.info/gems/coach_client)
|
12
|
+
- [GitHub](https://github.com/jungomi/coach_client)
|
13
|
+
|
8
14
|
## Installation
|
9
15
|
|
10
16
|
Add this line to your application's Gemfile:
|
@@ -21,12 +27,6 @@ Or install it yourself as:
|
|
21
27
|
|
22
28
|
$ gem install coach_client
|
23
29
|
|
24
|
-
## Documentation
|
25
|
-
|
26
|
-
- [Rubygem website](https://rubygems.org/gems/coach_client)
|
27
|
-
- [Documentation](http://www.rubydoc.info/gems/coach_client/0.1.0)
|
28
|
-
- [GitHub](https://github.com/jungomi/coach_client)
|
29
|
-
|
30
30
|
## Usage
|
31
31
|
|
32
32
|
### Client
|
@@ -150,7 +150,8 @@ user.partnerships.all? { |partnership| partnership.user1 == user } #=> true
|
|
150
150
|
The partnership uses the following methods to modify its status:
|
151
151
|
- `propose` proposes a partnership by user1
|
152
152
|
- `confirm` confirms a partnership by user2
|
153
|
-
- `
|
153
|
+
- `cancel` cancels the confirmation of user1
|
154
|
+
- `invalidate` invalidates the confirmation of user2
|
154
155
|
|
155
156
|
When using `save` on a partnership that is not operational, it first tries to
|
156
157
|
propose and confirm it. Similarly `delete` will try to invalidate it before
|
@@ -187,9 +188,9 @@ used almost identically.
|
|
187
188
|
As for the other resources the user or partnership and the sport may be passed
|
188
189
|
as the corresponding object. When an argument is not the object, it tries to
|
189
190
|
create it. For partnerships a string representation is expected in that case,
|
190
|
-
which represents the two users involved separated by a semicolon.
|
191
|
-
Using already existing objects is recommended to preserve the
|
192
|
-
attributes.
|
191
|
+
which represents the two users involved separated by a semicolon (e.g.
|
192
|
+
`'user1;user2'`). Using already existing objects is recommended to preserve the
|
193
|
+
already assigned attributes.
|
193
194
|
|
194
195
|
```ruby
|
195
196
|
user_sub = CoachClient::UserSubscription.new(client, user, sport)
|
@@ -209,9 +210,9 @@ partnership.subscriptions
|
|
209
210
|
|
210
211
|
#### Entry
|
211
212
|
|
212
|
-
An entry corresponds to subscription. The entry provides the method `create` to
|
213
|
-
create a new entry. This is automatically invoked when trying to save
|
214
|
-
|
213
|
+
An entry corresponds to a subscription. The entry provides the method `create` to
|
214
|
+
create a new entry. This is automatically invoked when trying to save the entry,
|
215
|
+
if the it does not exist on the CyberCoach service. When create is used on an
|
215
216
|
already existing entry, it creates a new one with the same attributes (apart
|
216
217
|
from the id).
|
217
218
|
|
data/Rakefile
CHANGED
data/coach_client.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Michael Jungo", "Amanda Karavolia", "Andrea Liechti", "Jocelyn Thode", "Simon Brulhart"]
|
10
10
|
spec.email = ["michael.jungo@unifr.ch"]
|
11
11
|
|
12
|
-
spec.summary = %q{A wrapper around the CyberCoach API of
|
12
|
+
spec.summary = %q{A wrapper around the CyberCoach API of the University of Fribourg}
|
13
13
|
spec.homepage = "https://github.com/jungomi/coach_client"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
@@ -26,6 +26,8 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.add_development_dependency "rspec", "~> 3.4.0"
|
27
27
|
spec.add_development_dependency "webmock", "~> 1.22.3"
|
28
28
|
spec.add_development_dependency "vcr", "~> 2.9.3"
|
29
|
+
spec.add_development_dependency "simplecov", "~> 0.11.1"
|
30
|
+
spec.add_development_dependency "codeclimate-test-reporter"
|
29
31
|
|
30
32
|
spec.add_dependency "gyoku", "~> 1.3.1"
|
31
33
|
spec.add_dependency "rest-client", "~> 1.8"
|
data/lib/coach_client/entry.rb
CHANGED
@@ -61,6 +61,12 @@ module CoachClient
|
|
61
61
|
# @option info [Date] :entrydate
|
62
62
|
# @option info [Integer] :entryduration
|
63
63
|
# @option info [String] :entrylocation
|
64
|
+
# @option info [Integer] :roundduration for boxing
|
65
|
+
# @option info [Integer] :numberofrounds except for soccer
|
66
|
+
# @option info [Integer] :courselength for cycling and running
|
67
|
+
# @option info [String] :coursetype for cycling and running
|
68
|
+
# @option info [String] :track for cycling and running
|
69
|
+
# @option info [String] :bicycletype for cycling
|
64
70
|
# @return [CoachClient::Entry]
|
65
71
|
def initialize(client, subscription, info = {})
|
66
72
|
super(client)
|
@@ -90,13 +96,9 @@ module CoachClient
|
|
90
96
|
# @raise [CoachClient::NotFound] if the entry does not exist
|
91
97
|
# @return [CoachClient::User] the updated user
|
92
98
|
def update
|
93
|
-
|
94
|
-
response =
|
95
|
-
|
96
|
-
password: user.password)
|
97
|
-
else
|
98
|
-
CoachClient::Request.get(url)
|
99
|
-
end
|
99
|
+
fail CoachClient::NotFound.new(self), 'Entry not found' if @id.nil?
|
100
|
+
response = CoachClient::Request.get(url, username: user.username,
|
101
|
+
password: user.password)
|
100
102
|
tag = "entry#{@subscription.sport}"
|
101
103
|
response = response.to_h[tag.to_sym]
|
102
104
|
@datecreated = response[:datecreated]
|
@@ -129,20 +131,13 @@ module CoachClient
|
|
129
131
|
# @raise [CoachClient::NotSaved] if the entry could not be saved
|
130
132
|
# @return [CoachClient::Entry] the created entry
|
131
133
|
def create
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
username: user.username,
|
138
|
-
password: user.password,
|
139
|
-
payload: payload,
|
140
|
-
content_type: :xml)
|
141
|
-
rescue RestClient::Conflict
|
142
|
-
raise CoachClient::IncompleteInformation.new(self), 'Incomplete information'
|
143
|
-
end
|
134
|
+
response = CoachClient::Request.post(@subscription.url,
|
135
|
+
username: user.username,
|
136
|
+
password: user.password,
|
137
|
+
payload: payload,
|
138
|
+
content_type: :xml)
|
144
139
|
unless response.code == 200 || response.code == 201
|
145
|
-
|
140
|
+
fail CoachClient::NotSaved.new(self), 'Could not create entry'
|
146
141
|
end
|
147
142
|
@id = self.class.extract_id_from_uri(response.header[:location])
|
148
143
|
self
|
@@ -160,19 +155,12 @@ module CoachClient
|
|
160
155
|
# @return [CoachClient::Entry] the created entry
|
161
156
|
def save
|
162
157
|
return create unless @id
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
response = CoachClient::Request.put(url, username: user.username,
|
168
|
-
password: user.password,
|
169
|
-
payload: payload,
|
170
|
-
content_type: :xml)
|
171
|
-
rescue RestClient::Conflict
|
172
|
-
raise CoachClient::IncompleteInformation.new(self), 'Incomplete information'
|
173
|
-
end
|
158
|
+
response = CoachClient::Request.put(url, username: user.username,
|
159
|
+
password: user.password,
|
160
|
+
payload: payload,
|
161
|
+
content_type: :xml)
|
174
162
|
unless response.code == 200 || response.code == 201
|
175
|
-
|
163
|
+
fail CoachClient::NotSaved.new(self), 'Could not save entry'
|
176
164
|
end
|
177
165
|
self
|
178
166
|
end
|
@@ -199,10 +187,7 @@ module CoachClient
|
|
199
187
|
# @raise [CoachClient::Unauthorized] if not authorized
|
200
188
|
# @return [true]
|
201
189
|
def delete
|
202
|
-
|
203
|
-
unless user.authenticated?
|
204
|
-
raise CoachClient::Unauthorized.new(user), 'Unauthorized'
|
205
|
-
end
|
190
|
+
fail CoachClient::NotFound.new(self), 'Entry not found' unless exist?
|
206
191
|
CoachClient::Request.delete(url, username: user.username,
|
207
192
|
password: user.password)
|
208
193
|
true
|
@@ -213,11 +198,7 @@ module CoachClient
|
|
213
198
|
# @return [Boolean]
|
214
199
|
def exist?
|
215
200
|
return false unless @id
|
216
|
-
|
217
|
-
super(username: user.username, password: user.password)
|
218
|
-
else
|
219
|
-
super
|
220
|
-
end
|
201
|
+
super(username: user.username, password: user.password)
|
221
202
|
end
|
222
203
|
|
223
204
|
# Returns the URL of the entry.
|
@@ -237,7 +218,7 @@ module CoachClient
|
|
237
218
|
private
|
238
219
|
|
239
220
|
def payload
|
240
|
-
vals =
|
221
|
+
vals = to_h
|
241
222
|
vals.delete(:subscription)
|
242
223
|
vals.delete_if { |_k, v| v.nil? || v.to_s.empty? }
|
243
224
|
tag = "entry#{@subscription.sport}"
|
@@ -58,7 +58,7 @@ module CoachClient
|
|
58
58
|
# @yieldreturn [Boolean] whether the partnership should be added to the list
|
59
59
|
# @return [Array<CoachClient::Partnership>] the list of partnerships
|
60
60
|
def self.list(client, size: 20, start: 0, all: false)
|
61
|
-
list
|
61
|
+
list = []
|
62
62
|
if all
|
63
63
|
total = self.total(client)
|
64
64
|
start = 0
|
@@ -69,7 +69,7 @@ module CoachClient
|
|
69
69
|
params: { start: start, size: size })
|
70
70
|
response.to_h[:partnerships].each do |p|
|
71
71
|
user1, user2 = extract_users_from_uri(p[:uri])
|
72
|
-
partnership =
|
72
|
+
partnership = new(client, user1, user2)
|
73
73
|
list << partnership if !block_given? || yield(partnership)
|
74
74
|
end
|
75
75
|
break unless all
|
@@ -106,22 +106,18 @@ module CoachClient
|
|
106
106
|
# @raise [CoachClient::NotFound] if the partnership does not exist
|
107
107
|
# @return [CoachClient::Partnership] the updated partnership
|
108
108
|
def update
|
109
|
-
|
110
|
-
response = if @user1.authenticated?
|
109
|
+
response = begin
|
111
110
|
CoachClient::Request.get(url, username: @user1.username,
|
112
111
|
password: @user1.password)
|
113
|
-
|
114
|
-
CoachClient::Request.get(url, username
|
112
|
+
rescue CoachClient::Exception
|
113
|
+
CoachClient::Request.get(url, username: @user2.username,
|
115
114
|
password: @user2.password)
|
116
|
-
else
|
117
|
-
CoachClient::Request.get(url)
|
118
115
|
end
|
119
116
|
response = response.to_h
|
120
117
|
@id = response[:id]
|
121
118
|
@datecreated = response[:datecreated]
|
122
119
|
@publicvisible = response[:publicvisible]
|
123
|
-
|
124
|
-
@user2_confirmed = response[:userconfirmed2]
|
120
|
+
set_user_confirmed(response)
|
125
121
|
@subscriptions = []
|
126
122
|
unless response[:subscriptions].nil?
|
127
123
|
response[:subscriptions].each do |s|
|
@@ -148,28 +144,19 @@ module CoachClient
|
|
148
144
|
propose unless @user1_confirmed
|
149
145
|
return confirm
|
150
146
|
end
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
else
|
163
|
-
CoachClient::Request.put(url, username: @user2.username,
|
164
|
-
password: @user2.password,
|
165
|
-
payload: payload,
|
166
|
-
content_type: :xml)
|
167
|
-
end
|
168
|
-
rescue RestClient::Conflict
|
169
|
-
raise CoachClient::IncompleteInformation.new(self), 'Incomplete information'
|
170
|
-
end
|
147
|
+
response = begin
|
148
|
+
CoachClient::Request.put(url, username: @user1.username,
|
149
|
+
password: @user1.password,
|
150
|
+
payload: payload,
|
151
|
+
content_type: :xml)
|
152
|
+
rescue CoachClient::Exception
|
153
|
+
CoachClient::Request.put(url, username: @user2.username,
|
154
|
+
password: @user2.password,
|
155
|
+
payload: payload,
|
156
|
+
content_type: :xml)
|
157
|
+
end
|
171
158
|
unless response.code == 200 || response.code == 201
|
172
|
-
|
159
|
+
fail CoachClient::NotSaved.new(self), 'Could not save partnership'
|
173
160
|
end
|
174
161
|
self
|
175
162
|
end
|
@@ -182,21 +169,14 @@ module CoachClient
|
|
182
169
|
# @raise [CoachClient::NotProposed] if the partnership could not be proposed
|
183
170
|
# @return [CoachClient::Partnership] the proposed partnership
|
184
171
|
def propose
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
response = CoachClient::Request.put(url, username: @user1.username,
|
190
|
-
password: @user1.password,
|
191
|
-
payload: payload,
|
192
|
-
content_type: :xml)
|
193
|
-
rescue RestClient::Conflict
|
194
|
-
raise CoachClient::IncompleteInformation.new(self), 'Incomplete information'
|
195
|
-
end
|
172
|
+
response = CoachClient::Request.put(url, username: @user1.username,
|
173
|
+
password: @user1.password,
|
174
|
+
payload: payload,
|
175
|
+
content_type: :xml)
|
196
176
|
unless response.code == 200 || response.code == 201
|
197
|
-
|
177
|
+
fail CoachClient::NotProposed.new(self), 'Could not propose partnership'
|
198
178
|
end
|
199
|
-
|
179
|
+
set_user_confirmed(response.to_h)
|
200
180
|
self
|
201
181
|
end
|
202
182
|
|
@@ -208,35 +188,40 @@ module CoachClient
|
|
208
188
|
# @raise [CoachClient::NotConfirmed] if the partnership could not be proposed
|
209
189
|
# @return [CoachClient::Partnership] the confirmed partnership
|
210
190
|
def confirm
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
response = CoachClient::Request.put(url, username: @user2.username,
|
216
|
-
password: @user2.password,
|
217
|
-
payload: payload,
|
218
|
-
content_type: :xml)
|
219
|
-
rescue RestClient::Conflict
|
220
|
-
raise CoachClient::IncompleteInformation.new(self), 'Incomplete information'
|
221
|
-
end
|
191
|
+
response = CoachClient::Request.put(url, username: @user2.username,
|
192
|
+
password: @user2.password,
|
193
|
+
payload: payload,
|
194
|
+
content_type: :xml)
|
222
195
|
unless response.code == 200 || response.code == 201
|
223
|
-
|
196
|
+
fail CoachClient::NotConfirmed.new(self), 'Could not confirm partnership'
|
224
197
|
end
|
225
|
-
|
198
|
+
set_user_confirmed(response.to_h)
|
199
|
+
self
|
200
|
+
end
|
201
|
+
|
202
|
+
# Cancels the partnership on the CyberCoach service.
|
203
|
+
#
|
204
|
+
# This sets the confirmation status of user1 to false.
|
205
|
+
#
|
206
|
+
# @raise [CoachClient::Unauthorized] if not authorized
|
207
|
+
# @return [CoachClient::Partnership] the invalidated partnership
|
208
|
+
def cancel
|
209
|
+
response = CoachClient::Request.delete(url, username: @user1.username,
|
210
|
+
password: @user1.password)
|
211
|
+
set_user_confirmed(response.to_h)
|
226
212
|
self
|
227
213
|
end
|
228
214
|
|
229
215
|
# Invalidates the partnership on the CyberCoach service.
|
230
216
|
#
|
217
|
+
# This sets the confirmation status of user2 to false.
|
218
|
+
#
|
231
219
|
# @raise [CoachClient::Unauthorized] if not authorized
|
232
220
|
# @return [CoachClient::Partnership] the invalidated partnership
|
233
221
|
def invalidate
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
CoachClient::Request.delete(url, username: @user2.username,
|
238
|
-
password: @user2.password)
|
239
|
-
@user2_confirmed = false
|
222
|
+
response = CoachClient::Request.delete(url, username: @user2.username,
|
223
|
+
password: @user2.password)
|
224
|
+
set_user_confirmed(response.to_h)
|
240
225
|
self
|
241
226
|
end
|
242
227
|
|
@@ -246,16 +231,13 @@ module CoachClient
|
|
246
231
|
# @raise [CoachClient::Unauthorized] if not authorized
|
247
232
|
# @return [true]
|
248
233
|
def delete
|
249
|
-
|
250
|
-
invalidate if user2_confirmed
|
251
|
-
if user1_confirmed
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
CoachClient::Request.delete(url, username: @user1.username,
|
256
|
-
password: @user1.password)
|
234
|
+
fail CoachClient::NotFound unless exist?
|
235
|
+
invalidate if @user2_confirmed
|
236
|
+
if @user1_confirmed
|
237
|
+
response = CoachClient::Request.delete(url, username: @user1.username,
|
238
|
+
password: @user1.password)
|
239
|
+
set_user_confirmed(response.to_h)
|
257
240
|
end
|
258
|
-
@user1_confirmed = false
|
259
241
|
true
|
260
242
|
end
|
261
243
|
|
@@ -283,12 +265,22 @@ module CoachClient
|
|
283
265
|
private
|
284
266
|
|
285
267
|
def payload
|
286
|
-
vals =
|
268
|
+
vals = to_h
|
287
269
|
vals.delete(:user1)
|
288
270
|
vals.delete(:user2)
|
289
271
|
vals.delete_if { |_k, v| v.nil? || v.to_s.empty? }
|
290
272
|
Gyoku.xml(partnership: vals)
|
291
273
|
end
|
274
|
+
|
275
|
+
def set_user_confirmed(response)
|
276
|
+
if response[:user1][:username] == @user1.username
|
277
|
+
@user1_confirmed = response[:userconfirmed1]
|
278
|
+
@user2_confirmed = response[:userconfirmed2]
|
279
|
+
else
|
280
|
+
@user1_confirmed = response[:userconfirmed2]
|
281
|
+
@user2_confirmed = response[:userconfirmed1]
|
282
|
+
end
|
283
|
+
end
|
292
284
|
end
|
293
285
|
end
|
294
286
|
|
@@ -40,9 +40,9 @@ module CoachClient
|
|
40
40
|
# @return [CoachClient::PartnershipSubscription] the updated partnership
|
41
41
|
# subscription
|
42
42
|
def update
|
43
|
-
|
43
|
+
begin
|
44
44
|
super(@partnership.user1)
|
45
|
-
|
45
|
+
rescue CoachClient::Exception
|
46
46
|
super(@partnership.user2)
|
47
47
|
end
|
48
48
|
end
|
@@ -60,9 +60,9 @@ module CoachClient
|
|
60
60
|
# @return [CoachClient::PartnershipSubscription] the saved partnership
|
61
61
|
# subscription
|
62
62
|
def save
|
63
|
-
|
63
|
+
begin
|
64
64
|
super(@partnership.user1)
|
65
|
-
|
65
|
+
rescue CoachClient::Exception
|
66
66
|
super(@partnership.user2)
|
67
67
|
end
|
68
68
|
end
|
@@ -74,9 +74,9 @@ module CoachClient
|
|
74
74
|
# @raise [CoachClient::Unauthorized] if not authorized
|
75
75
|
# @return [true]
|
76
76
|
def delete
|
77
|
-
|
77
|
+
begin
|
78
78
|
super(@partnership.user1)
|
79
|
-
|
79
|
+
rescue CoachClient::Exception
|
80
80
|
super(@partnership.user2)
|
81
81
|
end
|
82
82
|
end
|
data/lib/coach_client/request.rb
CHANGED
@@ -46,6 +46,8 @@ module CoachClient
|
|
46
46
|
raise CoachClient::NotFound, e.message
|
47
47
|
rescue RestClient::Unauthorized => e
|
48
48
|
raise CoachClient::Unauthorized, e.message
|
49
|
+
rescue RestClient::Conflict
|
50
|
+
raise CoachClient::IncompleteInformation, 'Incomplete Information'
|
49
51
|
end
|
50
52
|
CoachClient::Response.new(response.headers, response.body, response.code)
|
51
53
|
end
|
@@ -70,6 +72,8 @@ module CoachClient
|
|
70
72
|
raise CoachClient::NotFound, e.message
|
71
73
|
rescue RestClient::Unauthorized => e
|
72
74
|
raise CoachClient::Unauthorized, e.message
|
75
|
+
rescue RestClient::Conflict
|
76
|
+
raise CoachClient::IncompleteInformation, 'Incomplete Information'
|
73
77
|
end
|
74
78
|
CoachClient::Response.new(response.headers, response.body, response.code)
|
75
79
|
end
|
@@ -82,6 +86,7 @@ module CoachClient
|
|
82
86
|
# @param [Hash] header
|
83
87
|
# @return [CoachClient::Response]
|
84
88
|
def self.delete(url, username: nil, password: nil, **header)
|
89
|
+
header.merge!(DEFAULT_HEADER)
|
85
90
|
begin
|
86
91
|
response = RestClient::Request.execute(method: :delete, url: url,
|
87
92
|
user: username,
|
data/lib/coach_client/sport.rb
CHANGED
@@ -40,7 +40,7 @@ module CoachClient
|
|
40
40
|
sportlist = []
|
41
41
|
response = CoachClient::Request.get(client.url + path)
|
42
42
|
response.to_h[:sports].each do |s|
|
43
|
-
sport =
|
43
|
+
sport = new(client, s[:name])
|
44
44
|
sportlist << sport if !block_given? || yield(sport)
|
45
45
|
end
|
46
46
|
sportlist
|
@@ -61,7 +61,6 @@ module CoachClient
|
|
61
61
|
# @raise [CoachClient::NotFound] if the sport does not exist
|
62
62
|
# @return [CoachClient::Sport] the updated sport
|
63
63
|
def update
|
64
|
-
raise CoachClient::NotFound, 'Sport not found' unless exist?
|
65
64
|
response = CoachClient::Request.get(url)
|
66
65
|
response = response.to_h
|
67
66
|
@id = response[:id]
|
@@ -39,13 +39,8 @@ module CoachClient
|
|
39
39
|
# @param [CoachClient::User] user
|
40
40
|
# @return [CoachClient::Subscription] the updated subscription
|
41
41
|
def update(user)
|
42
|
-
|
43
|
-
|
44
|
-
CoachClient::Request.get(url, username: user.username,
|
45
|
-
password: user.password)
|
46
|
-
else
|
47
|
-
CoachClient::Request.get(url)
|
48
|
-
end
|
42
|
+
response = CoachClient::Request.get(url, username: user.username,
|
43
|
+
password: user.password)
|
49
44
|
response = response.to_h
|
50
45
|
@id = response[:id]
|
51
46
|
@datesubscribed = response[:datesubscribed]
|
@@ -73,25 +68,18 @@ module CoachClient
|
|
73
68
|
# @param [CoachClient::User] user
|
74
69
|
# @return [CoachClient::Subscription] the saved subscription
|
75
70
|
def save(user)
|
76
|
-
vals =
|
71
|
+
vals = to_h
|
77
72
|
vals.delete(:user)
|
78
73
|
vals.delete(:partnership)
|
79
74
|
vals.delete(:sport)
|
80
75
|
vals.delete_if { |_k, v| v.nil? || v.to_s.empty? }
|
81
76
|
payload = Gyoku.xml(subscription: vals)
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
response = CoachClient::Request.put(url, username: user.username,
|
87
|
-
password: user.password,
|
88
|
-
payload: payload,
|
89
|
-
content_type: :xml)
|
90
|
-
rescue RestClient::Conflict
|
91
|
-
raise CoachClient::IncompleteInformation.new(self), 'Incomplete information'
|
92
|
-
end
|
77
|
+
response = CoachClient::Request.put(url, username: user.username,
|
78
|
+
password: user.password,
|
79
|
+
payload: payload,
|
80
|
+
content_type: :xml)
|
93
81
|
unless response.code == 200 || response.code == 201
|
94
|
-
|
82
|
+
fail CoachClient::NotSaved.new(self), 'Could not save subscription'
|
95
83
|
end
|
96
84
|
self
|
97
85
|
end
|
@@ -103,10 +91,6 @@ module CoachClient
|
|
103
91
|
# @param [CoachClient::User] user
|
104
92
|
# @return [true]
|
105
93
|
def delete(user)
|
106
|
-
raise CoachClient::NotFound unless exist?
|
107
|
-
unless user.authenticated?
|
108
|
-
raise CoachClient::Unauthorized.new(user), 'Unauthorized'
|
109
|
-
end
|
110
94
|
CoachClient::Request.delete(url, username: user.username,
|
111
95
|
password: user.password)
|
112
96
|
true
|
data/lib/coach_client/user.rb
CHANGED
@@ -52,7 +52,7 @@ module CoachClient
|
|
52
52
|
# @yieldreturn [Boolean] whether the user should be added to the list
|
53
53
|
# @return [Array<CoachClient::User>] the list of users
|
54
54
|
def self.list(client, size: 20, start: 0, all: false)
|
55
|
-
userlist
|
55
|
+
userlist = []
|
56
56
|
if all
|
57
57
|
total = self.total(client)
|
58
58
|
start = 0
|
@@ -62,7 +62,7 @@ module CoachClient
|
|
62
62
|
response = CoachClient::Request.get(client.url + path,
|
63
63
|
params: { start: start, size: size })
|
64
64
|
response.to_h[:users].each do |u|
|
65
|
-
user =
|
65
|
+
user = new(client, u[:username])
|
66
66
|
userlist << user if !block_given? || yield(user)
|
67
67
|
end
|
68
68
|
break unless all
|
@@ -96,13 +96,8 @@ module CoachClient
|
|
96
96
|
# @raise [CoachClient::NotFound] if the user does not exist
|
97
97
|
# @return [CoachClient::User] the updated user
|
98
98
|
def update
|
99
|
-
|
100
|
-
|
101
|
-
CoachClient::Request.get(url, username: @username,
|
102
|
-
password: @password)
|
103
|
-
else
|
104
|
-
CoachClient::Request.get(url)
|
105
|
-
end
|
99
|
+
response = CoachClient::Request.get(url, username: @username,
|
100
|
+
password: @password)
|
106
101
|
response = response.to_h
|
107
102
|
@realname = response[:realname]
|
108
103
|
@email = response[:email]
|
@@ -137,30 +132,17 @@ module CoachClient
|
|
137
132
|
# @raise [CoachClient::NotSaved] if the user could not be saved
|
138
133
|
# @return [CoachClient::User] the saved user
|
139
134
|
def save
|
140
|
-
vals =
|
135
|
+
vals = to_h
|
141
136
|
vals.delete(:username)
|
142
137
|
vals.delete_if { |_k, v| v.nil? || v.to_s.empty? }
|
143
138
|
vals[:password] = vals.delete(:newpassword) if vals[:newpassword]
|
144
139
|
payload = Gyoku.xml(user: vals)
|
145
|
-
response =
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
CoachClient::Request.put(url, username: @username,
|
150
|
-
password: @password,
|
151
|
-
payload: payload,
|
152
|
-
content_type: :xml)
|
153
|
-
else
|
154
|
-
begin
|
155
|
-
CoachClient::Request.put(url, payload: payload,
|
156
|
-
content_type: :xml)
|
157
|
-
rescue RestClient::Conflict
|
158
|
-
raise CoachClient::IncompleteInformation.new(self),
|
159
|
-
'Incomplete user information'
|
160
|
-
end
|
161
|
-
end
|
140
|
+
response = CoachClient::Request.put(url, username: @username,
|
141
|
+
password: @password,
|
142
|
+
payload: payload,
|
143
|
+
content_type: :xml)
|
162
144
|
unless response.code == 200 || response.code == 201
|
163
|
-
|
145
|
+
fail CoachClient::NotSaved.new(self), 'Could not save user'
|
164
146
|
end
|
165
147
|
@password = vals[:password]
|
166
148
|
@newpassword = nil
|
@@ -173,10 +155,7 @@ module CoachClient
|
|
173
155
|
# @raise [CoachClient::Unauthorized] if the user is not authorized
|
174
156
|
# @return [true]
|
175
157
|
def delete
|
176
|
-
|
177
|
-
unless authenticated?
|
178
|
-
raise CoachClient::Unauthorized.new(self), 'Unauthorized'
|
179
|
-
end
|
158
|
+
fail CoachClient::NotFound.new(self), 'User not found' unless exist?
|
180
159
|
CoachClient::Request.delete(url, username: @username, password: @password)
|
181
160
|
true
|
182
161
|
end
|
data/lib/coach_client/version.rb
CHANGED