class CoachClient::Partnership

A partnership resource of the CyberCoach service.

Attributes

datecreated[R]

@return [Integer]

id[R]

@return [Integer]

publicvisible[RW]

@return [Integer]

subscriptions[R]

@return [Array<CoachClient::Subscription>]

user1[RW]

@return [CoachClient::User]

user1_confirmed[R]

@return [Boolean]

user2[RW]

@return [CoachClient::User]

user2_confirmed[R]

@return [Boolean]

Public Class Methods

extract_users_from_uri(uri) click to toggle source

Extracts the usernames from the partnership URI

@param [String] uri @return [Array<String>] the usernames

# File lib/coach_client/partnership.rb, line 30
def self.extract_users_from_uri(uri)
  match = uri.match(/partnerships\/(\w+);(\w+)\//)
  match.captures
end
list(client, size: 20, start: 0, all: false) { |partnership| ... } click to toggle source

Returns a list of partnerships from the CyberCoach service for which the given block returns a true value.

If no block is given, the whole list is returned.

@param [CoachClient::Client] client @param [Integer] size @param [Integer] start @param [Boolean] all @yieldparam [CoachClient::Partnership] partnership the partnership @yieldreturn [Boolean] whether the partnership should be added to the list @return [Array<CoachClient::Partnership>] the list of partnerships

# File lib/coach_client/partnership.rb, line 57
def self.list(client, size: 20, start: 0, all: false)
  list = []
  if all
    total = self.total(client)
    start = 0
    size = client.max_size
  end
  loop do
    response = CoachClient::Request.get(client.url + path,
                                        params: { start: start, size: size })
    response.to_h[:partnerships].each do |p|
      user1, user2 = extract_users_from_uri(p[:uri])
      partnership = new(client, user1, user2)
      list << partnership if !block_given? || yield(partnership)
    end
    break unless all
    start += size
    break if start >= total
  end
  list
end
new(client, user1, user2, publicvisible: nil) click to toggle source

Creates a new partnership.

@param [CoachClient::Client] client @param [String, CoachClient::User] user1 @param [String, CoachClient::User] user2 @param [Integer] publicvisible @return [CoachClient::Partnership]

Calls superclass method CoachClient::Resource::new
# File lib/coach_client/partnership.rb, line 86
def initialize(client, user1, user2, publicvisible: nil)
  super(client)
  @user1 = if user1.is_a?(CoachClient::User)
             user1
           else
             CoachClient::User.new(client, user1)
           end
  @user2 = if user2.is_a?(CoachClient::User)
             user2
           else
             CoachClient::User.new(client, user2)
           end
  @publicvisible = publicvisible
end
path() click to toggle source

Returns the relative path to the partnership resource.

@return [String] the relative path

# File lib/coach_client/partnership.rb, line 22
def self.path
  'partnerships/'
end
total(client) click to toggle source

Returns the total number of partnerships present on the CyberCoach service.

@param [CoachClient::Client] client @return [Integer] the total number of partnerships

# File lib/coach_client/partnership.rb, line 39
def self.total(client)
  response = CoachClient::Request.get(client.url + path,
                                      params: { size: 0 })
  response.to_h[:available]
end

Public Instance Methods

cancel() click to toggle source

Cancels the partnership on the CyberCoach service.

This sets the confirmation status of user1 to false.

@raise [CoachClient::Unauthorized] if not authorized @return [CoachClient::Partnership] the invalidated partnership

# File lib/coach_client/partnership.rb, line 205
def cancel
  response = CoachClient::Request.delete(url, username: @user1.username,
                                         password: @user1.password)
  set_user_confirmed(response.to_h)
  self
end
confirm() click to toggle source

Confirms the partnership on the CyberCoach service.

@raise [CoachClient::Unauthorized] if not authorized @raise [CoachClient::IncompleteInformation] if not all needed information

is given

@raise [CoachClient::NotConfirmed] if the partnership could not be proposed @return [CoachClient::Partnership] the confirmed partnership

# File lib/coach_client/partnership.rb, line 187
def confirm
  response = CoachClient::Request.put(url, username: @user2.username,
                                      password: @user2.password,
                                      payload: payload,
                                      content_type: :xml)
  unless response.code == 200 || response.code == 201
    fail CoachClient::NotConfirmed.new(self), 'Could not confirm partnership'
  end
  set_user_confirmed(response.to_h)
  self
end
delete() click to toggle source

Deletes the partnership on the CyberCoach service.

@raise [CoachClient::NotFound] if the partnership does not exist @raise [CoachClient::Unauthorized] if not authorized @return [true]

# File lib/coach_client/partnership.rb, line 230
def delete
  fail CoachClient::NotFound unless exist?
  invalidate if @user2_confirmed
  if @user1_confirmed
    response = CoachClient::Request.delete(url, username: @user1.username,
                                          password: @user1.password)
    set_user_confirmed(response.to_h)
  end
  true
end
invalidate() click to toggle source

Invalidates the partnership on the CyberCoach service.

This sets the confirmation status of user2 to false.

@raise [CoachClient::Unauthorized] if not authorized @return [CoachClient::Partnership] the invalidated partnership

# File lib/coach_client/partnership.rb, line 218
def invalidate
  response = CoachClient::Request.delete(url, username: @user2.username,
                                         password: @user2.password)
  set_user_confirmed(response.to_h)
  self
end
operational?() click to toggle source

Returns whether the partnership is operational.

@return [Boolean]

# File lib/coach_client/partnership.rb, line 244
def operational?
  @user1_confirmed && @user2_confirmed
end
propose() click to toggle source

Proposes the partnership on the CyberCoach service.

@raise [CoachClient::Unauthorized] if not authorized @raise [CoachClient::IncompleteInformation] if not all needed information

is given

@raise [CoachClient::NotProposed] if the partnership could not be proposed @return [CoachClient::Partnership] the proposed partnership

# File lib/coach_client/partnership.rb, line 168
def propose
  response = CoachClient::Request.put(url, username: @user1.username,
                                      password: @user1.password,
                                      payload: payload,
                                      content_type: :xml)
  unless response.code == 200 || response.code == 201
    fail CoachClient::NotProposed.new(self), 'Could not propose partnership'
  end
  set_user_confirmed(response.to_h)
  self
end
save() click to toggle source

Saves the partnership to the CyberCoach service.

The partnership is created if it does not exist on the CyberCoach service, otherwise it tries to overwrite it.

@raise [CoachClient::Unauthorized] if not authorized @raise [CoachClient::IncompleteInformation] if not all needed information

is given

@raise [CoachClient::NotSaved] if the partnership could not be saved @return [CoachClient::Partnership] the saved partnership

# File lib/coach_client/partnership.rb, line 139
def save
  unless operational?
    propose unless @user1_confirmed
    return confirm
  end
  response = begin
               CoachClient::Request.put(url, username: @user1.username,
                                        password: @user1.password,
                                        payload: payload,
                                        content_type: :xml)
             rescue CoachClient::Exception
               CoachClient::Request.put(url, username: @user2.username,
                                        password: @user2.password,
                                        payload: payload,
                                        content_type: :xml)
             end
  unless response.code == 200 || response.code == 201
    fail CoachClient::NotSaved.new(self), 'Could not save partnership'
  end
  self
end
to_s() click to toggle source

Returns the string representation of the user.

@return [String]

# File lib/coach_client/partnership.rb, line 258
def to_s
  "#{@user1.username};#{@user2.username}"
end
update() click to toggle source

Updates the partnership with the data from the CyberCoach service.

@raise [CoachClient::NotFound] if the partnership does not exist @return [CoachClient::Partnership] the updated partnership

# File lib/coach_client/partnership.rb, line 105
def update
  response = begin
               CoachClient::Request.get(url, username: @user1.username,
                                        password: @user1.password)
             rescue CoachClient::Exception
               CoachClient::Request.get(url, username: @user2.username,
                                        password: @user2.password)
             end
  response = response.to_h
  @id = response[:id]
  @datecreated = response[:datecreated]
  @publicvisible = response[:publicvisible]
  set_user_confirmed(response)
  @subscriptions = []
  unless response[:subscriptions].nil?
    response[:subscriptions].each do |s|
      sport = s[:uri].match(/\/(\w+)\/\z/).captures.first
      @subscriptions << CoachClient::PartnershipSubscription.new(client, self,
                                                                 sport)
    end
  end
  self
end
url() click to toggle source

Returns the URL of the partnership.

@return [String] the url of the partnership

# File lib/coach_client/partnership.rb, line 251
def url
  "#{@client.url}#{self.class.path}#{@user1.username};#{@user2.username}"
end

Private Instance Methods

payload() click to toggle source
# File lib/coach_client/partnership.rb, line 264
def payload
  vals = to_h
  vals.delete(:user1)
  vals.delete(:user2)
  vals.delete_if { |_k, v| v.nil? || v.to_s.empty? }
  Gyoku.xml(partnership: vals)
end
set_user_confirmed(response) click to toggle source
# File lib/coach_client/partnership.rb, line 272
def set_user_confirmed(response)
  if response[:user1][:username] == @user1.username
    @user1_confirmed = response[:userconfirmed1]
    @user2_confirmed = response[:userconfirmed2]
  else
    @user1_confirmed = response[:userconfirmed2]
    @user2_confirmed = response[:userconfirmed1]
  end
end