coach4rb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +16 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +31 -0
  6. data/Rakefile +8 -0
  7. data/coach4rb.gemspec +29 -0
  8. data/lib/coach4rb.rb +64 -0
  9. data/lib/coach4rb/builder.rb +142 -0
  10. data/lib/coach4rb/client.rb +116 -0
  11. data/lib/coach4rb/coach.rb +836 -0
  12. data/lib/coach4rb/json_response_parser.rb +11 -0
  13. data/lib/coach4rb/mixin/as_hash.rb +34 -0
  14. data/lib/coach4rb/mixin/auto_constructor.rb +71 -0
  15. data/lib/coach4rb/mixin/basic_auth.rb +23 -0
  16. data/lib/coach4rb/mixin/iterable.rb +40 -0
  17. data/lib/coach4rb/mixin/track_reader.rb +24 -0
  18. data/lib/coach4rb/mixin/track_writer.rb +19 -0
  19. data/lib/coach4rb/privacy.rb +11 -0
  20. data/lib/coach4rb/proxy.rb +143 -0
  21. data/lib/coach4rb/resource/entity.rb +47 -0
  22. data/lib/coach4rb/resource/entry.rb +126 -0
  23. data/lib/coach4rb/resource/page.rb +74 -0
  24. data/lib/coach4rb/resource/partnership.rb +37 -0
  25. data/lib/coach4rb/resource/sport.rb +19 -0
  26. data/lib/coach4rb/resource/subscription.rb +47 -0
  27. data/lib/coach4rb/resource/user.rb +49 -0
  28. data/lib/coach4rb/response_parser.rb +11 -0
  29. data/lib/coach4rb/version.rb +3 -0
  30. data/test/test_access_proxy.rb +58 -0
  31. data/test/test_client.rb +41 -0
  32. data/test/test_coach_client.rb +21 -0
  33. data/test/test_coach_entry.rb +141 -0
  34. data/test/test_coach_partnership.rb +115 -0
  35. data/test/test_coach_subscription.rb +110 -0
  36. data/test/test_coach_user.rb +191 -0
  37. data/test/test_entity.rb +63 -0
  38. data/test/test_entry_resource.rb +36 -0
  39. data/test/test_helper.rb +3 -0
  40. data/test/test_page.rb +82 -0
  41. data/test/test_partnership_resource.rb +69 -0
  42. data/test/test_sport_resource.rb +33 -0
  43. data/test/test_subscription_resource.rb +74 -0
  44. data/test/test_user_resource.rb +104 -0
  45. data/tools/.keep +0 -0
  46. metadata +190 -0
@@ -0,0 +1,836 @@
1
+ module Coach4rb
2
+
3
+ class Coach
4
+ include Mixin::BasicAuth
5
+
6
+ # Creates a Coach client for the cyber coach webservcie.
7
+ #
8
+ # @param [Client] client
9
+ # @param [ResponseParser] response_parser
10
+ # @param [Boolean] debug
11
+ # @return [Coach]
12
+ #
13
+ def initialize(client, response_parser, debug)
14
+ @client = client
15
+ @response_parser = response_parser
16
+ @debug = debug
17
+ end
18
+
19
+
20
+ # Authenticates a user against the Cyber Coach Webservice.
21
+ #
22
+ # @param username
23
+ # @param password
24
+ # @return [Boolean]
25
+ #
26
+ # ====Example
27
+ #
28
+ # @coach.authenticate('arueedlinger', 'test')
29
+ #
30
+ def authenticate(username, password)
31
+ begin
32
+ options = {authorization: basic_auth_encryption(username, password)}
33
+ url = url_for_path('/authenticateduser/')
34
+ client.get(url, options) do |response|
35
+ response.code == 200
36
+ end
37
+ rescue
38
+ raise 'Error: Could not authenticate user!'
39
+ end
40
+ end
41
+
42
+
43
+ # Tests if the given username is available.
44
+ #
45
+ # @param username
46
+ # @return [Boolean]
47
+ #
48
+ # ====Example
49
+ #
50
+ # @coach.username_available('arueedlinger')
51
+ #
52
+ def username_available?(username)
53
+ # check if username is alphanumeric and that it contains at least one letter
54
+ return false unless /^[a-zA-Z0-9]{1,}$/ =~ username
55
+ !user_exists?(username) rescue raise 'Error: Could not test username availability!'
56
+ end
57
+
58
+
59
+ # Tests if the user given its username exists..
60
+ #
61
+ # @param username
62
+ # @return [Boolean]
63
+ #
64
+ # ====Example
65
+ #
66
+ # @coach.user_exsists?('arueedlinger')
67
+ #
68
+ def user_exists?(username)
69
+ begin
70
+ url = url_for_path(user_path(username))
71
+ client.get(url) { |response| response.code == 200 }
72
+ rescue
73
+ raise 'Error: Could not test user existence!'
74
+ end
75
+ end
76
+
77
+
78
+ # Checks if the Cyber Coach Webservice is available.
79
+ #
80
+ # @return [Boolean]
81
+ #
82
+ # ====Example
83
+ #
84
+ # @coach.available?
85
+ #
86
+ def available?
87
+ client.get(url_for_path('/')) { |response| response.code == 200 } rescue false
88
+ end
89
+
90
+
91
+ # Creates a user with public visibility as default.
92
+ #
93
+ # @param [Hash] options
94
+ # @param [Block] block
95
+ # @return [User]
96
+ #
97
+ # ====Examples
98
+ #
99
+ # @coach.create_user do |user|
100
+ # user.real_name= 'the hoff'
101
+ # user.username= 'wantsomemoney'
102
+ # user.password= 'test'
103
+ # user.email= 'test@test.com'
104
+ # user.public_visible= 2
105
+ # end
106
+ #
107
+ def create_user(options={}, &block)
108
+ builder = Builder::User.new(public_visible: Privacy::Public)
109
+ block.call(builder)
110
+ url = url_for_path(user_path(builder.username))
111
+
112
+ begin
113
+ client.put(url, builder.to_xml, options) do |response|
114
+ a_hash = parse response
115
+ Resource::User.from_coach a_hash
116
+ end
117
+ rescue =>e
118
+ raise e if debug
119
+ false
120
+ end
121
+ end
122
+
123
+
124
+ # Updates a user.
125
+ #
126
+ # @param [User|String] user
127
+ # @param [Hash] options
128
+ # @param [Block] block
129
+ # @return [User]
130
+ #
131
+ # ====Examples
132
+ #
133
+ # @coach.update_user(user) do |user
134
+ # user.real_name= 'the hoff'
135
+ # user.password= 'test'
136
+ # user.email= 'test@test.com'
137
+ # user.public_visible= 2
138
+ # end
139
+ #
140
+ def update_user(user, options={}, &block)
141
+ raise 'Error: Param user is nil!' if user.nil?
142
+
143
+ builder = Builder::User.new
144
+ block.call(builder)
145
+ url = if user.is_a?(Resource::User) && user.uri
146
+ url_for_resource(user)
147
+ else
148
+ url_for_uri(user)
149
+ end
150
+
151
+ begin
152
+ client.put(url, builder.to_xml, options) do |response|
153
+ a_hash = parse response
154
+ Resource::User.from_coach a_hash
155
+ end
156
+ rescue => e
157
+ raise e if debug
158
+ false
159
+ end
160
+ end
161
+
162
+
163
+ # Deletes a user.
164
+ #
165
+ # @param [User\String] user
166
+ # @param [Hash] options
167
+ # @return [Boolean]
168
+ #
169
+ # ====Examples
170
+ #
171
+ # @coach.delete_user(user)
172
+ # @coach.delete_user('arueedlinger')
173
+ #
174
+ def delete_user(user, options={})
175
+ raise 'Error: Param user is nil!' if user.nil?
176
+
177
+ url = if user.is_a?(Resource::User)
178
+ url_for_resource(user)
179
+ elsif user.is_a?(String)
180
+ url_for_path(user_path(user))
181
+ else
182
+ raise 'Error: Invalid parameters!'
183
+ end
184
+ begin
185
+ client.delete(url, options) do |response|
186
+ response.code == 200
187
+ end
188
+ rescue => e
189
+ raise e if debug
190
+ end
191
+ end
192
+
193
+
194
+ # Creates a partnership with public visibility as default.
195
+ #
196
+ # @param [User|String] first_user
197
+ # @param [User|String] second_user
198
+ # @param [Hash] options
199
+ # @return [Partnership]
200
+ #
201
+ # ====Examples
202
+ #
203
+ # @coach.create_partnership('arueedlinger','wanze2')
204
+ #
205
+ # @coach.create_partnership('arueedlinger','wanze2') do |p|
206
+ # p.public_visible = Coach4rb::Private
207
+ # end
208
+ #
209
+ def create_partnership(first_user, second_user, options={}, &block)
210
+ raise 'Error: Param first_user is nil!' if first_user.nil?
211
+ raise 'Error: Param second_user is nil!' if second_user.nil?
212
+
213
+ path = if first_user.is_a?(Resource::User) && second_user.is_a?(Resource::User)
214
+ partnership_path(first_user.username, second_user.username)
215
+ elsif first_user.is_a?(String) && second_user.is_a?(String)
216
+ partnership_path(first_user, second_user)
217
+ else
218
+ raise 'Error: Invalid parameters!'
219
+ end
220
+ url = url_for_path(path)
221
+ builder = Builder::Partnership.new(public_visible: Privacy::Public)
222
+ block.call(builder) if block_given?
223
+ begin
224
+ client.put(url, builder.to_xml, options) do |response|
225
+ a_hash = parse response
226
+ Resource::Partnership.from_coach a_hash
227
+ end
228
+ rescue => e
229
+ raise e if debug
230
+ false
231
+ end
232
+ end
233
+
234
+
235
+ # Deletes a partnership
236
+ #
237
+ # @param [Partnership] partnership
238
+ # @param [Hash] options
239
+ # @return [Boolean]
240
+ #
241
+ def delete_partnership(partnership, options={})
242
+ raise 'Error: Param partnership is nil!' if partnership.nil?
243
+
244
+ url = url_for_resource(partnership)
245
+ begin
246
+ client.delete(url, options) do |response|
247
+ response.code == 200
248
+ end
249
+ rescue => e
250
+ raise e if debug
251
+ false
252
+ end
253
+ end
254
+
255
+
256
+ # Breaks up a partnership between two users.
257
+ #
258
+ # @param [User|String] first_user
259
+ # @param [User|String] second_user
260
+ # @param [Hash] options
261
+ # @return [Boolean]
262
+ #
263
+ def breakup_between(first_user, second_user, options={})
264
+ raise 'Error: Param first_user is nil!' if first_user.nil?
265
+ raise 'Error: Param second_user is nil!' if second_user.nil?
266
+
267
+ path = if first_user.is_a?(Resource::User) && second_user.is_a?(Resource::User)
268
+ partnership_path(first_user.username, second_user.username)
269
+ elsif first_user.is_a?(String) && second_user.is_a?(String)
270
+ partnership_path(first_user, second_user)
271
+ else
272
+ raise 'Error: Invalid parameters!'
273
+ end
274
+ url = url_for_path(path)
275
+ begin
276
+ client.delete(url, options) do |response|
277
+ response.code == 200
278
+ end
279
+ rescue => e
280
+ raise e if debug
281
+ false
282
+ end
283
+ end
284
+
285
+
286
+ # Creates a subscription with public visibility as default.
287
+ #
288
+ # @param [User|Partnership|String] user_partnership
289
+ # @param [String] sport
290
+ # @param [Hash] options
291
+ # @param [Block] block
292
+ # @return [Subscription]
293
+ #
294
+ # ====Examples
295
+ #
296
+ # @coach.create_subscription(user, :boxing) do |subscription|
297
+ # subscription.public_visible = Coach4rb::Privacy::Public
298
+ # end
299
+ #
300
+ # @coach.create_subscription(user, :boxing)
301
+ #
302
+ # partnership = @coach.partnership 'arueedlinger', 'asarteam5'
303
+ # @coach.subscribe(partnership, :running)
304
+ #
305
+ def create_subscription(user_partnership, sport, options={}, &block)
306
+ raise 'Error: Param user_partnership is nil!' if user_partnership.nil?
307
+ raise 'Error: Param sport is nil!' if sport.nil?
308
+
309
+ url = if user_partnership.is_a?(Resource::User)
310
+ url_for_path(subscription_user_path(user_partnership.username, sport))
311
+ elsif user_partnership.is_a?(Resource::Partnership)
312
+ first_username = user_partnership.first_user.username
313
+ second_username = user_partnership.second_user.username
314
+ url_for_path(subscription_partnership_path(first_username, second_username, sport))
315
+ elsif user_partnership.is_a?(String)
316
+ url_for_uri(user_partnership)
317
+ else
318
+ raise 'Error: Invalid parameter!'
319
+ end
320
+
321
+ builder = Builder::Subscription.new(public_visible: Privacy::Public)
322
+ block.call(builder) if block_given?
323
+
324
+ begin
325
+ client.put(url, builder.to_xml, options) do |response|
326
+ a_hash = parse response
327
+ Resource::Subscription.from_coach a_hash
328
+ end
329
+ rescue => e
330
+ raise e if debug
331
+ false
332
+ end
333
+ end
334
+
335
+ alias_method :update_subscription, :create_subscription
336
+ alias_method :subscribe, :create_subscription
337
+
338
+
339
+ # Deletes a subscription.
340
+ #
341
+ # @param [Subscription|String] subscription
342
+ # @param [Hash] options
343
+ # @return [Boolean]
344
+ #
345
+ # ====Examples
346
+ #
347
+ # user = @coach.user 'arueedlinger'
348
+ # @coach.unsubscribe(user, :boxing)
349
+ #
350
+ # partnership = @coach.partnership 'arueedlinger', 'asarteam5'
351
+ # @coach.unsubscribe(partnership, :running)
352
+ #
353
+ def unsubscribe(user_partnership, sport, options={})
354
+ raise 'Error: Param user_partnership is nil!' if user_partnership.nil?
355
+ raise 'Error: Param sport is nil!' if sport.nil?
356
+
357
+ url = if user_partnership.is_a?(Resource::User)
358
+ url_for_path(subscription_user_path(user_partnership.username, sport))
359
+ elsif user_partnership.is_a?(Resource::Partnership)
360
+ first_username = user_partnership.first_user.username
361
+ second_username = user_partnership.second_user.username
362
+ url_for_path(subscription_partnership_path(first_username, second_username, sport))
363
+ elsif user_partnership.is_a?(String)
364
+ url_for_uri(user_partnership)
365
+ else
366
+ raise 'Error: Invalid parameter!'
367
+ end
368
+
369
+ begin
370
+ client.delete(url, options) do |response|
371
+ response.code == 200
372
+ end
373
+ rescue => e
374
+ raise e if debug
375
+ false
376
+ end
377
+ end
378
+
379
+ # Deletes a subscription.
380
+ #
381
+ # @param [Subscription] subscription
382
+ # @param [Hash] options
383
+ # @return [Boolean]
384
+ #
385
+ def delete_subscription(subscription, options={})
386
+ raise 'Error: Param subscription is nil!' if subscription.nil?
387
+
388
+ url = url_for_resource(subscription)
389
+ begin
390
+ client.delete(url, options) do |response|
391
+ response.code == 200
392
+ end
393
+ rescue => e
394
+ raise e if debug
395
+ false
396
+ end
397
+ end
398
+
399
+
400
+ # Creates an entry with public visibility as default.
401
+ #
402
+ # @param [User|Partnership|String] user_partnership
403
+ # @param [Hash] options
404
+ # @param [Block] block
405
+ # @return [Entry|Boolean]
406
+ #
407
+ # ====Examples
408
+ # entry = @coach.create_entry(@user, :running) do |e|
409
+ # e.comment = 'test'
410
+ # e.number_of_rounds = 10
411
+ # e.public_visible = Coach4rb::Privacy::Public
412
+ # end
413
+ #
414
+ # entry = @coach.create_entry(@user, :soccer) do |e|
415
+ # e.comment = 'test'
416
+ # e.number_of_rounds = 10
417
+ # end
418
+ #
419
+ def create_entry(user_partnership, sport, options={}, &block)
420
+ raise 'Error: Param user_partnership is nil!' if user_partnership.nil?
421
+ raise 'Error: Param sport is nil!' if sport.nil?
422
+
423
+ entry_type = sport.downcase.to_sym
424
+ builder = Builder::Entry.builder(entry_type)
425
+
426
+ url = if user_partnership.is_a?(Resource::Entity)
427
+ url_for_resource(user_partnership) + sport.to_s
428
+ elsif user_partnership.is_a?(String)
429
+ url_for_uri(user_partnership) + sport.to_s
430
+ else
431
+ raise 'Error: Invalid parameter!'
432
+ end
433
+
434
+ block.call(builder) if block_given?
435
+
436
+ begin
437
+ client.post(url, builder.to_xml, options) do |response|
438
+ if uri = response.headers[:location]
439
+ entry_by_uri(uri, options)
440
+ else
441
+ false
442
+ end
443
+ end
444
+ rescue => e
445
+ raise e if debug
446
+ false
447
+ end
448
+ end
449
+
450
+
451
+ # Updates an entry.
452
+ #
453
+ # @param [Entry|String] entry
454
+ # @param [Hash] options
455
+ # @param [Block] block
456
+ # @return [Entry|Boolean]
457
+ #
458
+ # ====Examples
459
+ #
460
+ # entry = @coach.entry_by_uri '/CyberCoachServer/resources/users/wantsomemoney/Running/1138/'
461
+ # updated_entry = @proxy.update_entry(entry) do |entry|
462
+ # entry.comment = 'Test!!'
463
+ # end
464
+ #
465
+ # uri = '/CyberCoachServer/resources/users/wantsomemoney/Running/1138/'
466
+ # res = @proxy.update_entry(uri) do |entry|
467
+ # entry.comment = 'Test!'
468
+ # end
469
+ #
470
+ def update_entry(entry, options={}, &block)
471
+ raise 'Error: Param entry is nil!' if entry.nil?
472
+
473
+ url, entry_type = if entry.is_a?(Resource::Entry)
474
+ [url_for_resource(entry), entry.type]
475
+ else
476
+ *, type, id = url_for_uri(entry).split('/')
477
+ type = type.downcase.to_sym
478
+ [url_for_uri(entry), type]
479
+ end
480
+
481
+ builder = Builder::Entry.builder(entry_type)
482
+ block.call(builder)
483
+ begin
484
+ client.put(url, builder.to_xml, options) do |response|
485
+ a_hash = parse(response)
486
+ Resource::Entry.from_coach a_hash
487
+ end
488
+ rescue => e
489
+ raise e if debug
490
+ false
491
+ end
492
+ end
493
+
494
+
495
+ # Deletes an entry..
496
+ #
497
+ # @param [Entry|String] entry
498
+ # @param [Hash] options
499
+ # @return [Boolean]
500
+ #
501
+ def delete_entry(entry, options={})
502
+ raise 'Error: Param entry is nil!' if entry.nil?
503
+
504
+ url = if entry.is_a?(Resource::Entry)
505
+ url_for_resource(entry)
506
+ elsif entry.is_a?(String)
507
+ url_for_uri(entry)
508
+ else
509
+ raise 'Error: Invalid parameter!'
510
+ end
511
+ begin
512
+ client.delete(url, options) do |response|
513
+ response.code == 200
514
+ end
515
+ rescue => e
516
+ raise e if debug
517
+ false
518
+ end
519
+ end
520
+
521
+
522
+ # Retrieves a user by its username.
523
+ #
524
+ # @param [String|User] username | user
525
+ # @param [Hash] options
526
+ # @return [User]
527
+ #
528
+ # ====Examples
529
+ #
530
+ # user = @coach.user a_user
531
+ # user = @coach.user 'arueedlinger'
532
+ # user = @coach.user 'arueedlinger', {}
533
+ #
534
+ def user(user, options={})
535
+ raise 'Error: Param user is nil!' if user.nil?
536
+
537
+ url = if user.is_a?(Resource::User)
538
+ url_for_resource(user)
539
+ elsif user.is_a?(String)
540
+ url_for_path(user_path(user))
541
+ else
542
+ raise 'Error: Invalid parameter!'
543
+ end
544
+ url = append_query_params(url, options)
545
+ client.get(url, options) do |response|
546
+ a_hash = parse(response)
547
+ Resource::User.from_coach a_hash
548
+ end
549
+ end
550
+
551
+
552
+ # Retrieves a user by its uri.
553
+ #
554
+ # @param [String] uri
555
+ # @param [Hash] options
556
+ # @return [User]
557
+ #
558
+ # user = @coach.user_by_uri '/CyberCoachServer/resources/users/arueedlinger', {}
559
+ # user = @coach.user_by_uri '/CyberCoachServer/resources/users/arueedlinger'
560
+ #
561
+ def user_by_uri(uri, options={})
562
+ raise 'Error: Param uri is nil!' if uri.nil?
563
+
564
+ url = url_for_uri(uri)
565
+ url = append_query_params(url, options)
566
+ client.get(url, options) do |response|
567
+ a_hash = parse(response)
568
+ Resource::User.from_coach a_hash
569
+ end
570
+ end
571
+
572
+
573
+ # Retrieves users.
574
+ #
575
+ # @param [Hash] options
576
+ # @return [PageResource]
577
+ #
578
+ # ====Examples
579
+ # users = @coach.users
580
+ # users = @coach.users query: { start: 0, size: 10}
581
+ #
582
+ def users(options={query: {}})
583
+ url = url_for_path(user_path)
584
+ url = append_query_params(url, options)
585
+ client.get(url, options) do |response|
586
+ a_hash = parse(response)
587
+ Resource::Page.from_coach a_hash, Resource::User
588
+ end
589
+ end
590
+
591
+
592
+ # Retrieves a partnership by its uri.
593
+ #
594
+ # @param [String] uri
595
+ # @param [Hash] options
596
+ # @return [Partnership]
597
+ #
598
+ # ====Example
599
+ #
600
+ # partnership = @coach.partnership_by_uri '/CyberCoachServer/resources/partnerships/arueedlinger;asarteam5/'
601
+ #
602
+ def partnership_by_uri(uri, options={})
603
+ raise 'Error: Param uri is nil!' if uri.nil?
604
+
605
+ url = url_for_uri(uri)
606
+ url = append_query_params(url, options)
607
+ client.get(url, options) do |response|
608
+ a_hash = parse(response)
609
+ Resource::Partnership.from_coach a_hash
610
+ end
611
+ end
612
+
613
+
614
+ # Retrieves a partnership by the first username and second username.
615
+ #
616
+ # @param [String] first_username
617
+ # @param [String] second_username
618
+ # @param [Hash] options
619
+ # @return [Partnership]
620
+ #
621
+ # ====Example
622
+ #
623
+ # partnership = @coach.partnership 'arueedlinger', 'asarteam5'
624
+ #
625
+ def partnership(first_username, second_username, options={})
626
+ raise 'Error: Param first_username is nil!' if first_username.nil?
627
+ raise 'Error: Param second_username is nil!' if second_username.nil?
628
+
629
+ url = url_for_path(partnership_path(first_username, second_username))
630
+ url = append_query_params(url, options)
631
+ client.get(url, options) do |response|
632
+ a_hash = parse(response)
633
+ Resource::Partnership.from_coach a_hash
634
+ end
635
+ end
636
+
637
+
638
+ # Retrieves partnerships.
639
+ #
640
+ # @param [Hash] options
641
+ # @return [PageResource]
642
+ #
643
+ # ====Examples
644
+ #
645
+ # partnerships = @coach.partnerships
646
+ # partnerships = @coach.partnerships query: { start: 0, size: 10}
647
+ #
648
+ def partnerships(options={query: {}})
649
+ url = url_for_path(partnership_path)
650
+ url = append_query_params(url, options)
651
+ client.get(url, options) do |response|
652
+ a_hash = parse(response)
653
+ Resource::Page.from_coach a_hash, Resource::Partnership
654
+ end
655
+ end
656
+
657
+
658
+ # Retrieves a subscription.
659
+ #
660
+ # ====Example
661
+ #
662
+ # subscription = @coach.subscription_by_uri '/CyberCoachServer/resources/users/newuser4/'
663
+ #
664
+ def subscription_by_uri(uri, options={})
665
+ raise 'Error: Param uri is nil!' if uri.nil?
666
+
667
+ url = url_for_uri(uri)
668
+ url = append_query_params(url, options)
669
+ client.get(url, options) do |response|
670
+ a_hash = parse(response)
671
+ Resource::Subscription.from_coach a_hash
672
+ end
673
+ end
674
+
675
+
676
+ # Retrieves a subscription.
677
+ #
678
+ # @param [String|Subscription] first_user | subscription
679
+ # @param [String] second_user | sport
680
+ # @param [String|Hash] sport | options
681
+ # @param [Hash|nil] options
682
+ #
683
+ # ====Examples
684
+ #
685
+ # subscription = @coach.subscription subscription
686
+ # subscription = @coach.subscription subscription, {}
687
+ # subscription = @coach.subscription 'newuser4', 'running'
688
+ # subscription = @coach.subscription 'newuser4', 'running', {}
689
+ # subscription = @coach.subscription 'newuser4','newuser5', 'running'
690
+ # subscription = @coach.subscription 'newuser4','newuser5', 'running', {}
691
+ # subscription = @coach.subscription 'newuser4', :running
692
+ # subscription = @coach.subscription 'newuser4', :running, {}
693
+ # subscription = @coach.subscription 'newuser4','newuser5', :running
694
+ # subscription = @coach.subscription 'newuser4','newuser5', :running, {}
695
+ #
696
+ def subscription(*args)
697
+ first_param, second_param, third_param, fourth_param, = args
698
+ url, options = if first_param.is_a?(Resource::Entry) && first_param.uri
699
+ [url_for_resource(first_param), second_param || {}]
700
+ elsif first_param.is_a?(String) && second_param.is_a?(String) && (third_param.is_a?(String) || third_param.is_a?(Symbol))
701
+ [url_for_path(subscription_partnership_path(first_param, second_param, third_param)), fourth_param || {}]
702
+ elsif first_param.is_a?(String) && (second_param.is_a?(String) || second_param.is_a?(Symbol))
703
+ [url_for_path(subscription_user_path(first_param, second_param)), third_param || {}]
704
+ elsif first_param.is_a?(Resource::Subscription)
705
+ [url_for_resource(first_param), second_param || {}]
706
+ else
707
+ raise 'Error: Invalid parameters!'
708
+ end
709
+ url = append_query_params(url, options)
710
+ client.get(url, options) do |response|
711
+ a_hash = parse(response)
712
+ Resource::Subscription.from_coach a_hash
713
+ end
714
+ end
715
+
716
+
717
+ # Retrieves an entry by its uri.
718
+ #
719
+ # ====Example
720
+ #
721
+ # entry = @coach.entry_by_uri '/CyberCoachServer/resources/users/wantsomemoney/Running/1138/'
722
+ #
723
+ def entry_by_uri(uri, options={})
724
+ raise 'Error: Param uri is nil!' if uri.nil?
725
+
726
+ begin
727
+ url = url_for_uri(uri)
728
+ url = append_query_params(url, options)
729
+ client.get(url, options) do |response|
730
+ return false if response.code == 404
731
+ a_hash = parse(response)
732
+ Resource::Entry.from_coach a_hash
733
+ end
734
+ rescue => e
735
+ raise e if debug
736
+ false
737
+ end
738
+ end
739
+
740
+
741
+ # Retrieves an entry.
742
+ #
743
+ # ====Example
744
+ #
745
+ # subscription = @coach.subscription 'arueedlinger', 'running'
746
+ # subscription_entry = subscription.entries.first
747
+ # entry = @coach.entry subscription_entry
748
+ #
749
+ def entry(entry, options={})
750
+ raise 'Error: Param entry is nil!' if entry.nil?
751
+
752
+ begin
753
+ url = url_for_resource(entry)
754
+ url = append_query_params(url, options)
755
+ client.get(url, options) do |response|
756
+ a_hash = parse(response)
757
+ Resource::Entry.from_coach a_hash
758
+ end
759
+ rescue => e
760
+ raise e if debug
761
+ false
762
+ end
763
+ end
764
+
765
+
766
+ private
767
+
768
+ def debug
769
+ @debug
770
+ end
771
+
772
+ def client
773
+ @client
774
+ end
775
+
776
+ def parse(response)
777
+ @response_parser.parse(response)
778
+ end
779
+
780
+ # Url helpers and path helpers for creating correct urls.
781
+
782
+ def url_for_resource(resource)
783
+ if resource.uri
784
+ "#{client.site}#{resource.uri}"
785
+ else
786
+ "#{client.service_uri}#{resource.entity_path}"
787
+ end
788
+ end
789
+
790
+
791
+ def url_for_uri(uri)
792
+ "#{client.site}#{uri}"
793
+ end
794
+
795
+
796
+ def url_for_path(path)
797
+ "#{client.service_uri}#{path}"
798
+ end
799
+
800
+
801
+ def user_path(username='')
802
+ "/users/#{username}"
803
+ end
804
+
805
+
806
+ def partnership_path(first_username=nil, second_username=nil)
807
+ if first_username.nil? && second_username.nil?
808
+ '/partnerships/'
809
+ elsif first_username && second_username
810
+ "/partnerships/#{first_username};#{second_username}"
811
+ else
812
+ raise 'Error: Invalid parameters!'
813
+ end
814
+ end
815
+
816
+
817
+ def subscription_user_path(username, sport)
818
+ "/users/#{username}/#{sport}"
819
+ end
820
+
821
+
822
+ def subscription_partnership_path(first_username, second_username, sport)
823
+ "/partnerships/#{first_username};#{second_username}/#{sport}"
824
+ end
825
+
826
+
827
+ def append_query_params(url, options)
828
+ new_uri = Addressable::URI.parse(url)
829
+ new_uri.query_values = options[:query] if options[:query]
830
+ options.delete(:query) #clean up
831
+ new_uri.to_s
832
+ end
833
+
834
+ end
835
+
836
+ end