Class: Coach4rb::Coach
- Inherits:
-
Object
- Object
- Coach4rb::Coach
- Includes:
- Mixin::BasicAuth
- Defined in:
- lib/coach4rb/coach.rb
Instance Method Summary (collapse)
-
- (User) authenticate(username, password)
Authenticates a user against the Cyber Coach Webservice.
-
- (Boolean) available?
Checks if the Cyber Coach Webservice is available.
-
- (Boolean) breakup_between(first_user, second_user, options = {})
Breaks up a partnership between two users.
-
- (Entry|Boolean) create_entry(user_partnership, sport, options = {}, &block)
Creates an entry with public visibility as default.
-
- (Partnership) create_partnership(first_user, second_user, options = {}, &block)
Creates a partnership with public visibility as default.
-
- (Subscription) create_subscription(user_partnership, sport, options = {}, &block)
(also: #update_subscription, #subscribe)
Creates a subscription with public visibility as default.
-
- (User) create_user(options = {}, &block)
Creates a user with public visibility as default.
-
- (Boolean) delete_entry(entry, options = {})
Deletes an entry..
-
- (Boolean) delete_partnership(partnership, options = {})
Deletes a partnership.
-
- (Boolean) delete_subscription(subscription, options = {})
Deletes a subscription.
-
- (Boolean) delete_user(user, options = {})
Deletes a user.
-
- (Object) entry(entry, options = {})
Retrieves an entry.
-
- (Object) entry_by_uri(uri, options = {})
Retrieves an entry by its uri.
-
- (Coach) initialize(client, response_parser, debug)
constructor
Creates a Coach client for the cyber coach webservcie.
-
- (Partnership) partnership(first_username, second_username, options = {})
Retrieves a partnership by the first username and second username.
-
- (Partnership) partnership_by_uri(uri, options = {})
Retrieves a partnership by its uri.
-
- (PageResource) partnerships(options = {query: {}})
Retrieves partnerships.
-
- (Object) subscription(*args)
Retrieves a subscription.
-
- (Object) subscription_by_uri(uri, options = {})
Retrieves a subscription.
-
- (Boolean) unsubscribe(user_partnership, sport, options = {})
Deletes a subscription.
-
- (Entry|Boolean) update_entry(entry, options = {}, &block)
Updates an entry.
-
- (User) update_user(user, options = {}, &block)
Updates a user.
-
- (User) user(user, options = {})
Retrieves a user by its username.
-
- (User) user_by_uri(uri, options = {})
Retrieves a user by its uri.
-
- (Boolean) user_exists?(username)
Tests if the user given its username exists..
-
- (Boolean) username_available?(username)
Tests if the given username is available.
-
- (PageResource) users(options = {query: {}})
Retrieves users.
Methods included from Mixin::BasicAuth
Constructor Details
- (Coach) initialize(client, response_parser, debug)
Creates a Coach client for the cyber coach webservcie.
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
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 = {authorization: basic_auth_encryption(username, password)} url = url_for_path('/authenticateduser/') client.get(url, ) 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
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.
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, ={}) 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, ) 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
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, ={}, &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, ) do |response| if uri = response.headers[:location] entry_by_uri(uri, ) 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
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, ={}, &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, ) 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
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, ={}, &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, ) 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
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(={}, &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, ) 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..
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, ={}) 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, ) do |response| response.code == 200 end rescue => e raise e if debug false end end |
- (Boolean) delete_partnership(partnership, options = {})
Deletes a partnership
249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/coach4rb/coach.rb', line 249 def delete_partnership(partnership, ={}) raise 'Error: Param partnership is nil!' if partnership.nil? url = url_for_resource(partnership) begin client.delete(url, ) do |response| response.code == 200 end rescue => e raise e if debug false end end |
- (Boolean) delete_subscription(subscription, options = {})
Deletes a subscription.
396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/coach4rb/coach.rb', line 396 def delete_subscription(subscription, ={}) raise 'Error: Param subscription is nil!' if subscription.nil? url = url_for_resource(subscription) begin client.delete(url, ) do |response| response.code == 200 end rescue => e raise e if debug false end end |
- (Boolean) delete_user(user, options = {})
Deletes a user.
Examples
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, ={}) 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, ) do |response| response.code == 200 end rescue => e raise e if debug end end |
- (Object) entry(entry, options = {})
Retrieves an entry.
Examples
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, ={}) raise 'Error: Param entry is nil!' if entry.nil? begin url = url_for_resource(entry) url = append_query_params(url, ) client.get(url, ) 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
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, ={}) raise 'Error: Param uri is nil!' if uri.nil? begin url = url_for_uri(uri) url = append_query_params(url, ) client.get(url, ) 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
647 648 649 650 651 652 653 654 655 656 657 |
# File 'lib/coach4rb/coach.rb', line 647 def partnership(first_username, second_username, ={}) 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, ) client.get(url, ) 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
624 625 626 627 628 629 630 631 632 633 |
# File 'lib/coach4rb/coach.rb', line 624 def partnership_by_uri(uri, ={}) raise 'Error: Param uri is nil!' if uri.nil? url = url_for_uri(uri) url = append_query_params(url, ) client.get(url, ) do |response| a_hash = parse(response) Resource::Partnership.from_coach a_hash end end |
- (PageResource) partnerships(options = {query: {}})
Retrieves partnerships.
Examples
671 672 673 674 675 676 677 678 |
# File 'lib/coach4rb/coach.rb', line 671 def partnerships(={query: {}}) url = url_for_path(partnership_path) url = append_query_params(url, ) client.get(url, ) do |response| a_hash = parse(response) Resource::Page.from_coach a_hash, Resource::Partnership end end |
- (Object) subscription(*args)
Retrieves a subscription.
Examples
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, = 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, ) client.get(url, ) do |response| a_hash = parse(response) Resource::Subscription.from_coach a_hash end end |
- (Object) subscription_by_uri(uri, options = {})
Retrieves a subscription.
Examples
687 688 689 690 691 692 693 694 695 696 |
# File 'lib/coach4rb/coach.rb', line 687 def subscription_by_uri(uri, ={}) raise 'Error: Param uri is nil!' if uri.nil? url = url_for_uri(uri) url = append_query_params(url, ) client.get(url, ) do |response| a_hash = parse(response) Resource::Subscription.from_coach a_hash end end |
- (Boolean) unsubscribe(user_partnership, sport, options = {})
Deletes a subscription.
Examples
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, ={}) 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, ) 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
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, ={}, &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, ) 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
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, ={}, &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, ) 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
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, ={}) 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, ) client.get(url, ) 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
581 582 583 584 585 586 587 588 589 590 |
# File 'lib/coach4rb/coach.rb', line 581 def user_by_uri(uri, ={}) raise 'Error: Param uri is nil!' if uri.nil? url = url_for_uri(uri) url = append_query_params(url, ) client.get(url, ) 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
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
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
604 605 606 607 608 609 610 611 |
# File 'lib/coach4rb/coach.rb', line 604 def users(={query: {}}) url = url_for_path(user_path) url = append_query_params(url, ) client.get(url, ) do |response| a_hash = parse(response) Resource::Page.from_coach a_hash, Resource::User end end |