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

#initialize(client, response_parser, debug) ⇒ Coach

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

#authenticate(username, password) ⇒ User

Authenticates a user against the Cyber Coach Webservice.

Example

@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

#available?Boolean

Checks if the Cyber Coach Webservice is available.

Example

@coach.available?

Returns:

  • (Boolean)


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

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

#breakup_between(first_user, second_user, options = {}) ⇒ Boolean

Breaks up a partnership between two users.

Parameters:

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

Returns:

  • (Boolean)


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

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

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

Creates an entry with public visibility as default.

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)


424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/coach4rb/coach.rb', line 424

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

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

Creates a partnership with public visibility as default.

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)


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

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

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

Creates a subscription with public visibility as default.

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)


310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/coach4rb/coach.rb', line 310

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

#create_user(options = {}, &block) ⇒ User

Creates a user with public visibility as default.

Examples

end

Parameters:

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

Returns:

  • (User)


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

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

#delete_entry(entry, options = {}) ⇒ Boolean

Deletes an entry..

Parameters:

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

Returns:

  • (Boolean)


506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/coach4rb/coach.rb', line 506

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

#delete_partnership(partnership, options = {}) ⇒ Boolean

Deletes a partnership

Parameters:

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

Returns:

  • (Boolean)


246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/coach4rb/coach.rb', line 246

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

#delete_subscription(subscription, options = {}) ⇒ Boolean

Deletes a subscription.

Parameters:

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

Returns:

  • (Boolean)


390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/coach4rb/coach.rb', line 390

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

#delete_user(user, options = {}) ⇒ Boolean

Deletes a user.

Examples

@coach.delete_user(user) @coach.delete_user(‘arueedlinger’)

Parameters:

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

Returns:

  • (Boolean)


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

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

#entry(entry, options = {}) ⇒ Object

Retrieves an entry.

Example

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



754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
# File 'lib/coach4rb/coach.rb', line 754

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

#entry_by_uri(uri, options = {}) ⇒ Object

Retrieves an entry by its uri.

Example

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



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

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(first_username, second_username, options = {}) ⇒ Partnership

Retrieves a partnership by the first username and second username.

Example

partnership = @coach.partnership ‘arueedlinger’, ‘asarteam5’

Parameters:

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

Returns:

  • (Partnership)


630
631
632
633
634
635
636
637
638
639
640
# File 'lib/coach4rb/coach.rb', line 630

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_by_uri(uri, options = {}) ⇒ Partnership

Retrieves a partnership by its uri.

Example

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

Parameters:

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

Returns:

  • (Partnership)


607
608
609
610
611
612
613
614
615
616
# File 'lib/coach4rb/coach.rb', line 607

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

#partnerships(options = {query: {}}) ⇒ PageResource

Retrieves partnerships.

Examples

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

Parameters:

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

Returns:

  • (PageResource)


653
654
655
656
657
658
659
660
# File 'lib/coach4rb/coach.rb', line 653

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

#subscription(*args) ⇒ Object

Retrieves a subscription.

Examples

subscription = @coach.subscription subscription subscription = @coach.subscription subscription, {} subscription = @coach.subscription ‘newuser4’, ‘running’ subscription = @coach.subscription ‘newuser4’, ‘running’, {} subscription = @coach.subscription ‘newuser4’,‘newuser5’, ‘running’ subscription = @coach.subscription ‘newuser4’,‘newuser5’, ‘running’, {} subscription = @coach.subscription ‘newuser4’, :running subscription = @coach.subscription ‘newuser4’, :running, {} subscription = @coach.subscription ‘newuser4’,‘newuser5’, :running subscription = @coach.subscription ‘newuser4’,‘newuser5’, :running, {}

Parameters:

  • first_user (String|Subscription)

    | subscription

  • second_user (String)

    | sport

  • sport (String|Hash)

    | options

  • options (Hash|nil)


701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# File 'lib/coach4rb/coach.rb', line 701

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

#subscription_by_uri(uri, options = {}) ⇒ Object

Retrieves a subscription.

Example

subscription = @coach.subscription_by_uri ‘/CyberCoachServer/resources/users/newuser4/’



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

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

#unsubscribe(user_partnership, sport, options = {}) ⇒ Boolean

Deletes a subscription.

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)


358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/coach4rb/coach.rb', line 358

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

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

Updates an entry.

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)


475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/coach4rb/coach.rb', line 475

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

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

Updates a user.

Examples

@coach.update_user(user) do |user

user.real_name= 'the hoff'
user.password= 'test'
user.email= '[email protected]'
user.public_visible= 2

end

Parameters:

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

Returns:

  • (User)


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

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, options = {}) ⇒ User

Retrieves a user by its username.

Examples

user = @coach.user a_user user = @coach.user ‘arueedlinger’ user = @coach.user ‘arueedlinger’, {}

Parameters:

  • username (String|User)

    | user

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

Returns:

  • (User)


539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/coach4rb/coach.rb', line 539

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_by_uri(uri, options = {}) ⇒ User

Retrieves a user by its uri.

user = @coach.user_by_uri ‘/CyberCoachServer/resources/users/arueedlinger’, {} user = @coach.user_by_uri ‘/CyberCoachServer/resources/users/arueedlinger’

Parameters:

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

Returns:

  • (User)


566
567
568
569
570
571
572
573
574
575
# File 'lib/coach4rb/coach.rb', line 566

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

#user_exists?(username) ⇒ Boolean

Tests if the user given its username exists..

Example

@coach.user_exsists?(‘arueedlinger’)

Parameters:

  • username

Returns:

  • (Boolean)


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

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

#username_available?(username) ⇒ Boolean

Tests if the given username is available.

Example

@coach.username_available(‘arueedlinger’)

Parameters:

  • username

Returns:

  • (Boolean)


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

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

#users(options = {query: {}}) ⇒ PageResource

Retrieves users.

Examples

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

Parameters:

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

Returns:

  • (PageResource)


587
588
589
590
591
592
593
594
# File 'lib/coach4rb/coach.rb', line 587

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