Class: Coach4rb::Coach

Inherits:
Object
  • Object
show all
Includes:
Mixin::BasicAuth
Defined in:
lib/coach4rb/coach.rb

Instance Method Summary (collapse)

Methods included from Mixin::BasicAuth

included

Constructor Details

- (Coach) initialize(client, response_parser, debug)

Creates a Coach client for the cyber coach webservcie.

Parameters:



13
14
15
16
17
# File 'lib/coach4rb/coach.rb', line 13

def initialize(client, response_parser, debug)
  @client = client
  @response_parser = response_parser
  @debug = debug
end

Instance Method Details

- (User) authenticate(username, password)

Authenticates a user against the Cyber Coach Webservice.

Example

Examples:

@coach.authenticate('arueedlinger', 'test')

Parameters:

  • username
  • password

Returns:

  • (User)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/coach4rb/coach.rb', line 30

def authenticate(username, password)
  begin
    options = {authorization: basic_auth_encryption(username, password)}
    url = url_for_path('/authenticateduser/')
    client.get(url, options) do |response|
      if response.code == 200
        a_hash = parse response
        Resource::User.from_coach a_hash
      else
        false
      end
    end
  rescue
    raise 'Error: Could not authenticate user!'
  end
end

- (Boolean) available?

Checks if the Cyber Coach Webservice is available.

Examples

Examples:

@coach.available?

Returns:

  • (Boolean)


92
93
94
# File 'lib/coach4rb/coach.rb', line 92

def available?
  client.get(url_for_path('/')) { |response| response.code == 200 } rescue false
end

- (Boolean) breakup_between(first_user, second_user, options = {})

Breaks up a partnership between two users.

Parameters:

  • first_user (User|String)
  • second_user (User|String)
  • options (Hash) (defaults to: {})

Returns:

  • (Boolean)


271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/coach4rb/coach.rb', line 271

def breakup_between(first_user, second_user, options={})
  raise 'Error: Param first_user is nil!' if first_user.nil?
  raise 'Error: Param second_user is nil!' if second_user.nil?

  path = if first_user.is_a?(Resource::User) && second_user.is_a?(Resource::User)
           partnership_path(first_user.username, second_user.username)
         elsif first_user.is_a?(String) && second_user.is_a?(String)
           partnership_path(first_user, second_user)
         else
           raise 'Error: Invalid parameters!'
         end
  url = url_for_path(path)
  begin
    client.delete(url, options) do |response|
      response.code == 200
    end
  rescue => e
    raise e if debug
    false
  end
end

- (Entry|Boolean) create_entry(user_partnership, sport, options = {}, &block)

Creates an entry with public visibility as default.

Examples

Examples:

entry = @coach.create_entry(@user, :running) do |e|
   e.comment = 'test'
   e.number_of_rounds = 10
   e.public_visible = Coach4rb::Privacy::Public
end
entry = @coach.create_entry(@user, :soccer) do |e|
  e.comment = 'test'
  e.number_of_rounds = 10
end

Parameters:

  • user_partnership (User|Partnership|String)
  • options (Hash) (defaults to: {})
  • block (Block)

Returns:

  • (Entry|Boolean)


432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/coach4rb/coach.rb', line 432

def create_entry(user_partnership, sport, options={}, &block)
  raise 'Error: Param user_partnership is nil!' if user_partnership.nil?
  raise 'Error: Param sport is nil!' if sport.nil?

  entry_type = sport.downcase.to_sym
  builder = Builder::Entry.builder(entry_type)

  url = if user_partnership.is_a?(Resource::Entity)
          url_for_resource(user_partnership) + sport.to_s
        elsif user_partnership.is_a?(String)
          url_for_uri(user_partnership) + sport.to_s
        else
          raise 'Error: Invalid parameter!'
        end

  block.call(builder) if block_given?

  begin
    client.post(url, builder.to_xml, options) do |response|
      if uri = response.headers[:location]
        entry_by_uri(uri, options)
      else
        false
      end
    end
  rescue => e
    raise e if debug
    false
  end
end

- (Partnership) create_partnership(first_user, second_user, options = {}, &block)

Creates a partnership with public visibility as default.

Examples

Examples:

@coach.create_partnership('arueedlinger','wanze2')
@coach.create_partnership('arueedlinger','wanze2') do |p|
   p.public_visible = Coach4rb::Private
end

Parameters:

  • first_user (User|String)
  • second_user (User|String)
  • options (Hash) (defaults to: {})

Returns:

  • (Partnership)


217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/coach4rb/coach.rb', line 217

def create_partnership(first_user, second_user, options={}, &block)
  raise 'Error: Param first_user is nil!' if first_user.nil?
  raise 'Error: Param second_user is nil!' if second_user.nil?

  path = if first_user.is_a?(Resource::User) && second_user.is_a?(Resource::User)
           partnership_path(first_user.username, second_user.username)
         elsif first_user.is_a?(String) && second_user.is_a?(String)
           partnership_path(first_user, second_user)
         else
           raise 'Error: Invalid parameters!'
         end
  url = url_for_path(path)
  builder = Builder::Partnership.new(public_visible: Privacy::Public)
  block.call(builder) if block_given?
  begin
    client.put(url, builder.to_xml, options) do |response|
      a_hash = parse response
      Resource::Partnership.from_coach a_hash
    end
  rescue => e
    raise e if debug
    false
  end
end

- (Subscription) create_subscription(user_partnership, sport, options = {}, &block) Also known as: update_subscription, subscribe

Creates a subscription with public visibility as default.

Examples

Examples:

@coach.create_subscription(user, :boxing) do |subscription|
  subscription.public_visible = Coach4rb::Privacy::Public
end
@coach.create_subscription(user, :boxing)
partnership = @coach.partnership 'arueedlinger', 'asarteam5'
@coach.subscribe(partnership, :running)

Parameters:

  • user_partnership (User|Partnership|String)
  • sport (String)
  • options (Hash) (defaults to: {})
  • block (Block)

Returns:

  • (Subscription)


315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/coach4rb/coach.rb', line 315

def create_subscription(user_partnership, sport, options={}, &block)
  raise 'Error: Param user_partnership is nil!' if user_partnership.nil?
  raise 'Error: Param sport is nil!' if sport.nil?

  url = if user_partnership.is_a?(Resource::User)
          url_for_path(subscription_user_path(user_partnership.username, sport))
        elsif user_partnership.is_a?(Resource::Partnership)
          first_username = user_partnership.first_user.username
          second_username = user_partnership.second_user.username
          url_for_path(subscription_partnership_path(first_username, second_username, sport))
        elsif user_partnership.is_a?(String)
          url_for_uri(user_partnership)
        else
          raise 'Error: Invalid parameter!'
        end

  builder = Builder::Subscription.new(public_visible: Privacy::Public)
  block.call(builder) if block_given?

  begin
    client.put(url, builder.to_xml, options) do |response|
      a_hash = parse response
      Resource::Subscription.from_coach a_hash
    end
  rescue => e
    raise e if debug
    false
  end
end

- (User) create_user(options = {}, &block)

Creates a user with public visibility as default.

Examples

Examples:

@coach.create_user do |user|
  user.real_name= 'the hoff'
  user.username= 'wantsomemoney'
  user.password= 'test'
  user.email= 'test@test.com'
  user.public_visible= 2
end

Parameters:

  • options (Hash) (defaults to: {})
  • block (Block)

Returns:

  • (User)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/coach4rb/coach.rb', line 113

def create_user(options={}, &block)
  builder = Builder::User.new(public_visible: Privacy::Public)
  block.call(builder)
  url = url_for_path(user_path(builder.username))

  begin
    client.put(url, builder.to_xml, options) do |response|
      a_hash = parse response
      Resource::User.from_coach a_hash
    end
  rescue =>e
    raise e if debug
    false
  end
end

- (Boolean) delete_entry(entry, options = {})

Deletes an entry..

Parameters:

  • entry (Entry|String)
  • options (Hash) (defaults to: {})

Returns:

  • (Boolean)


515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/coach4rb/coach.rb', line 515

def delete_entry(entry, options={})
  raise 'Error: Param entry is nil!' if entry.nil?

  url = if entry.is_a?(Resource::Entry)
          url_for_resource(entry)
        elsif entry.is_a?(String)
          url_for_uri(entry)
        else
          raise 'Error: Invalid parameter!'
        end
  begin
    client.delete(url, options) do |response|
      response.code == 200
    end
  rescue => e
    raise e if debug
    false
  end
end

- (Boolean) delete_partnership(partnership, options = {})

Deletes a partnership

Parameters:

  • partnership (Partnership)
  • options (Hash) (defaults to: {})

Returns:

  • (Boolean)


249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/coach4rb/coach.rb', line 249

def delete_partnership(partnership, options={})
  raise 'Error: Param partnership is nil!' if partnership.nil?

  url = url_for_resource(partnership)
  begin
    client.delete(url, options) do |response|
      response.code == 200
    end
  rescue => e
    raise e if debug
    false
  end
end

- (Boolean) delete_subscription(subscription, options = {})

Deletes a subscription.

Parameters:

  • subscription (Subscription)
  • options (Hash) (defaults to: {})

Returns:

  • (Boolean)


396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/coach4rb/coach.rb', line 396

def delete_subscription(subscription, options={})
  raise 'Error: Param subscription is nil!' if subscription.nil?

  url = url_for_resource(subscription)
  begin
    client.delete(url, options) do |response|
      response.code == 200
    end
  rescue => e
    raise e if debug
    false
  end
end

- (Boolean) delete_user(user, options = {})

Deletes a user.

Examples

Examples:

@coach.delete_user(user)
@coach.delete_user('arueedlinger')

Parameters:

  • user (User\String)
  • options (Hash) (defaults to: {})

Returns:

  • (Boolean)


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/coach4rb/coach.rb', line 181

def delete_user(user, options={})
  raise 'Error: Param user is nil!' if user.nil?

  url = if user.is_a?(Resource::User)
    url_for_resource(user)
  elsif user.is_a?(String)
    url_for_path(user_path(user))
  else
    raise 'Error: Invalid parameters!'
  end
  begin
    client.delete(url, options) do |response|
      response.code == 200
    end
  rescue => e
    raise e if debug
  end
end

- (Object) entry(entry, options = {})

Retrieves an entry.

Examples

Examples:

subscription = @coach.subscription 'arueedlinger', 'running'
subscription_entry = subscription.entries.first
entry = @coach.entry subscription_entry

Parameters:

  • entry (Entry)
  • options (Hash) (defaults to: {})


785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
# File 'lib/coach4rb/coach.rb', line 785

def entry(entry, options={})
  raise 'Error: Param entry is nil!' if entry.nil?

  begin
    url = url_for_resource(entry)
    url = append_query_params(url, options)
    client.get(url, options) do |response|
      a_hash = parse(response)
      Resource::Entry.from_coach a_hash
    end
  rescue => e
    raise e if debug
    false
  end
end

- (Object) entry_by_uri(uri, options = {})

Retrieves an entry by its uri.

Examples

Examples:

entry = @coach.entry_by_uri '/CyberCoachServer/resources/users/wantsomemoney/Running/1138/'

Parameters:

  • uri (String)
  • options (Hash) (defaults to: {})


757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# File 'lib/coach4rb/coach.rb', line 757

def entry_by_uri(uri, options={})
  raise 'Error: Param uri is nil!' if uri.nil?

  begin
    url = url_for_uri(uri)
    url = append_query_params(url, options)
    client.get(url, options) do |response|
      return false if response.code == 404
      a_hash = parse(response)
      Resource::Entry.from_coach a_hash
    end
  rescue => e
    raise e if debug
    false
  end
end

- (Partnership) partnership(first_username, second_username, options = {})

Retrieves a partnership by the first username and second username.

Examples

Examples:

partnership = @coach.partnership 'arueedlinger', 'asarteam5'

Parameters:

  • first_username (String)
  • second_username (String)
  • options (Hash) (defaults to: {})

Returns:

  • (Partnership)


647
648
649
650
651
652
653
654
655
656
657
# File 'lib/coach4rb/coach.rb', line 647

def partnership(first_username, second_username, options={})
  raise 'Error: Param first_username is nil!' if first_username.nil?
  raise 'Error: Param second_username is nil!' if second_username.nil?

  url = url_for_path(partnership_path(first_username, second_username))
  url = append_query_params(url, options)
  client.get(url, options) do |response|
    a_hash = parse(response)
    Resource::Partnership.from_coach a_hash
  end
end

- (Partnership) partnership_by_uri(uri, options = {})

Retrieves a partnership by its uri.

Examples

Examples:

partnership = @coach.partnership_by_uri '/CyberCoachServer/resources/partnerships/arueedlinger;asarteam5/'

Parameters:

  • uri (String)
  • options (Hash) (defaults to: {})

Returns:

  • (Partnership)


624
625
626
627
628
629
630
631
632
633
# File 'lib/coach4rb/coach.rb', line 624

def partnership_by_uri(uri, options={})
  raise 'Error: Param uri is nil!' if uri.nil?

  url = url_for_uri(uri)
  url = append_query_params(url, options)
  client.get(url, options) do |response|
    a_hash = parse(response)
    Resource::Partnership.from_coach a_hash
  end
end

- (PageResource) partnerships(options = {query: {}})

Retrieves partnerships.

Examples

Examples:

partnerships = @coach.partnerships
partnerships = @coach.partnerships query: { start: 0, size: 10}

Parameters:

  • options (Hash) (defaults to: {query: {}})

Returns:

  • (PageResource)


671
672
673
674
675
676
677
678
# File 'lib/coach4rb/coach.rb', line 671

def partnerships(options={query: {}})
  url = url_for_path(partnership_path)
  url = append_query_params(url, options)
  client.get(url, options) do |response|
    a_hash = parse(response)
    Resource::Page.from_coach a_hash, Resource::Partnership
  end
end

- (Object) subscription(*args)

Retrieves a subscription.

Examples

Examples:

subscription = @coach.subscription subscription
subscription = @coach.subscription subscription, query: { start: 0, size: 10}
subscription = @coach.subscription 'newuser4', 'running'
subscription = @coach.subscription 'newuser4', 'running', query: { start: 0, size: 10}
subscription = @coach.subscription 'newuser4','newuser5', 'running'
subscription = @coach.subscription 'newuser4','newuser5', 'running', query: { start: 0, size: 10}
subscription = @coach.subscription 'newuser4', :running
subscription = @coach.subscription 'newuser4', :running, query: { start: 0, size: 10}
subscription = @coach.subscription 'newuser4','newuser5', :running
subscription = @coach.subscription 'newuser4','newuser5', :running, query: { start: 0, size: 10}

Parameters:

  • first_user (String|Subscription)

    | subscription

  • second_user (String)

    | sport

  • sport (String|Hash)

    | options

  • options (Hash|nil)


728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# File 'lib/coach4rb/coach.rb', line 728

def subscription(*args)
  first_param, second_param, third_param, fourth_param, = args
  url, options = if first_param.is_a?(Resource::Entry) && first_param.uri
                   [url_for_resource(first_param), second_param || {}]
                 elsif first_param.is_a?(String) && second_param.is_a?(String) && (third_param.is_a?(String) || third_param.is_a?(Symbol))
                   [url_for_path(subscription_partnership_path(first_param, second_param, third_param)), fourth_param || {}]
                 elsif first_param.is_a?(String) && (second_param.is_a?(String) || second_param.is_a?(Symbol))
                   [url_for_path(subscription_user_path(first_param, second_param)), third_param || {}]
                 elsif first_param.is_a?(Resource::Subscription)
                   [url_for_resource(first_param), second_param || {}]
                 else
                   raise 'Error: Invalid parameters!'
                 end
  url = append_query_params(url, options)
  client.get(url, options) do |response|
    a_hash = parse(response)
    Resource::Subscription.from_coach a_hash
  end
end

- (Object) subscription_by_uri(uri, options = {})

Retrieves a subscription.

Examples

Examples:

subscription = @coach.subscription_by_uri '/CyberCoachServer/resources/users/newuser4/'


687
688
689
690
691
692
693
694
695
696
# File 'lib/coach4rb/coach.rb', line 687

def subscription_by_uri(uri, options={})
  raise 'Error: Param uri is nil!' if uri.nil?

  url = url_for_uri(uri)
  url = append_query_params(url, options)
  client.get(url, options) do |response|
    a_hash = parse(response)
    Resource::Subscription.from_coach a_hash
  end
end

- (Boolean) unsubscribe(user_partnership, sport, options = {})

Deletes a subscription.

Examples

Examples:

user = @coach.user 'arueedlinger'
@coach.unsubscribe(user, :boxing)
partnership = @coach.partnership 'arueedlinger', 'asarteam5'
@coach.unsubscribe(partnership, :running)

Parameters:

  • subscription (Subscription|String)
  • options (Hash) (defaults to: {})

Returns:

  • (Boolean)


364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/coach4rb/coach.rb', line 364

def unsubscribe(user_partnership, sport, options={})
  raise 'Error: Param user_partnership is nil!' if user_partnership.nil?
  raise 'Error: Param sport is nil!' if sport.nil?

  url = if user_partnership.is_a?(Resource::User)
          url_for_path(subscription_user_path(user_partnership.username, sport))
        elsif user_partnership.is_a?(Resource::Partnership)
          first_username = user_partnership.first_user.username
          second_username = user_partnership.second_user.username
          url_for_path(subscription_partnership_path(first_username, second_username, sport))
        elsif user_partnership.is_a?(String)
          url_for_uri(user_partnership)
        else
          raise 'Error: Invalid parameter!'
        end

  begin
    client.delete(url, options) do |response|
      response.code == 200
    end
  rescue => e
    raise e if debug
    false
  end
end

- (Entry|Boolean) update_entry(entry, options = {}, &block)

Updates an entry.

Examples

Examples:

entry = @coach.entry_by_uri '/CyberCoachServer/resources/users/wantsomemoney/Running/1138/'
updated_entry = @proxy.update_entry(entry) do |entry|
  entry.comment = 'Test!!'
end
uri = '/CyberCoachServer/resources/users/wantsomemoney/Running/1138/'
res = @proxy.update_entry(uri) do |entry|
  entry.comment = 'Test!'
end

Parameters:

  • entry (Entry|String)
  • options (Hash) (defaults to: {})
  • block (Block)

Returns:

  • (Entry|Boolean)


484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/coach4rb/coach.rb', line 484

def update_entry(entry, options={}, &block)
  raise 'Error: Param entry is nil!' if entry.nil?

  url, entry_type = if entry.is_a?(Resource::Entry)
                      [url_for_resource(entry), entry.type]
                    else
                      *, type, id = url_for_uri(entry).split('/')
                      type = type.downcase.to_sym
                      [url_for_uri(entry), type]
                    end

  builder = Builder::Entry.builder(entry_type)
  block.call(builder)
  begin
    client.put(url, builder.to_xml, options) do |response|
      a_hash = parse(response)
      Resource::Entry.from_coach a_hash
    end
  rescue => e
    raise e if debug
    false
  end
end

- (User) update_user(user, options = {}, &block)

Updates a user.

Examples

Examples:

@coach.update_user(user) do |user
  user.real_name= 'the hoff'
  user.password= 'test'
  user.email= 'test@test.com'
  user.public_visible= 2
end

Parameters:

  • user (User|String)
  • options (Hash) (defaults to: {})
  • block (Block)

Returns:

  • (User)


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/coach4rb/coach.rb', line 146

def update_user(user, options={}, &block)
  raise 'Error: Param user is nil!' if user.nil?

  builder = Builder::User.new
  block.call(builder)
  url = if user.is_a?(Resource::User) && user.uri
          url_for_resource(user)
        else
          url_for_uri(user)
        end

  begin
    client.put(url, builder.to_xml, options) do |response|
      a_hash = parse response
      Resource::User.from_coach a_hash
    end
  rescue => e
    raise e if debug
    false
  end
end

- (User) user(user, options = {})

Retrieves a user by its username.

Examples

Examples:

user = @coach.user a_user
user = @coach.user 'arueedlinger'
user = @coach.user 'arueedlinger', query: { start: 0, soze: 10 }

Parameters:

  • username (String|User)

    | user

  • options (Hash) (defaults to: {})

Returns:

  • (User)


550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/coach4rb/coach.rb', line 550

def user(user, options={})
  raise 'Error: Param user is nil!' if user.nil?

  url = if user.is_a?(Resource::User)
          url_for_resource(user)
        elsif user.is_a?(String)
          url_for_path(user_path(user))
        else
          raise 'Error: Invalid parameter!'
        end
  url = append_query_params(url, options)
  client.get(url, options) do |response|
    a_hash = parse(response)
    Resource::User.from_coach a_hash
  end
end

- (User) user_by_uri(uri, options = {})

Retrieves a user by its uri.

Examples

Examples:

user = @coach.user_by_uri '/CyberCoachServer/resources/users/arueedlinger', query: { start: 0, soze: 10 }
user = @coach.user_by_uri '/CyberCoachServer/resources/users/arueedlinger'

Parameters:

  • uri (String)
  • options (Hash) (defaults to: {})

Returns:

  • (User)


581
582
583
584
585
586
587
588
589
590
# File 'lib/coach4rb/coach.rb', line 581

def user_by_uri(uri, options={})
  raise 'Error: Param uri is nil!' if uri.nil?

  url = url_for_uri(uri)
  url = append_query_params(url, options)
  client.get(url, options) do |response|
    a_hash = parse(response)
    Resource::User.from_coach a_hash
  end
end

- (Boolean) user_exists?(username)

Tests if the user given its username exists..

Examples

Examples:

@coach.user_exsists?('arueedlinger')

Parameters:

  • username

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
# File 'lib/coach4rb/coach.rb', line 74

def user_exists?(username)
  begin
    url = url_for_path(user_path(username))
    client.get(url) { |response| response.code == 200 }
  rescue
    raise 'Error: Could not test user existence!'
  end
end

- (Boolean) username_available?(username)

Tests if the given username is available.

Examples

Examples:

@coach.username_available('arueedlinger')

Parameters:

  • username

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/coach4rb/coach.rb', line 58

def username_available?(username)
  # check if username is alphanumeric and that it contains at least one letter
  return false unless /^[a-zA-Z0-9]{1,}$/ =~ username
  !user_exists?(username) rescue raise 'Error: Could not test username availability!'
end

- (PageResource) users(options = {query: {}})

Retrieves users.

Examples

Examples:

users = @coach.users
users = @coach.users query: { start: 0, size: 10}

Parameters:

  • options (Hash) (defaults to: {query: {}})

Returns:

  • (PageResource)


604
605
606
607
608
609
610
611
# File 'lib/coach4rb/coach.rb', line 604

def users(options={query: {}})
  url = url_for_path(user_path)
  url = append_query_params(url, options)
  client.get(url, options) do |response|
    a_hash = parse(response)
    Resource::Page.from_coach a_hash, Resource::User
  end
end