class CoachClient::Subscription

A subscription resource of the CyberCoach service.

@note Use the subclass {CoachClient::UserSubscription} or

{CoachClient::PartnershipSubscription} for a user or partnership
subscription respectively.

Attributes

datesubscribed[R]

@return [Integer]

entries[R]

@return [Array<CoachClient::Entry>]

id[R]

@return [Integer]

publicvisible[RW]

@return [Integer]

sport[RW]

@return [CoachClient::Sport]

Public Class Methods

new(client, sport, publicvisible: nil) click to toggle source

Creates a new subscription.

@param [CoachClient::Client] client @param [String, Symbol, CoachClient::Sport] sport @param [Integer] publicvisible @return [CoachClient::Subscription]

Calls superclass method CoachClient::Resource::new
# File lib/coach_client/subscription.rb, line 26
def initialize(client, sport, publicvisible: nil)
  super(client)
  @sport = if sport.is_a?(CoachClient::Sport)
             sport
           else
             CoachClient::Sport.new(client, sport)
           end
  @publicvisible = publicvisible
end

Public Instance Methods

delete(user) click to toggle source

Deletes the subscription on the CyberCoach service.

@raise [CoachClient::NotFound] if the subscription does not exist @raise [CoachClient::Unauthorized] if not authorized @param [CoachClient::User] user @return [true]

# File lib/coach_client/subscription.rb, line 105
def delete(user)
  CoachClient::Request.delete(url, username: user.username,
                              password: user.password)
  true
end
save(user) click to toggle source

Saves the subscription to the CyberCoach service.

The subscription 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 subscription could not be saved @param [CoachClient::User] user @return [CoachClient::Subscription] the saved subscription

# File lib/coach_client/subscription.rb, line 82
def save(user)
  vals = to_h
  vals.delete(:user)
  vals.delete(:partnership)
  vals.delete(:sport)
  vals.delete_if { |_k, v| v.nil? || v.to_s.empty? }
  payload = Gyoku.xml(subscription: vals)
  response = CoachClient::Request.put(url, username: user.username,
                                      password: user.password,
                                      payload: payload,
                                      content_type: :xml)
  unless response.code == 200 || response.code == 201
    fail CoachClient::NotSaved.new(self), 'Could not save subscription'
  end
  self
end
update(user, size: 20, start: 0, all: false) click to toggle source

Updates the subscription with the data from the CyberCoach service.

@raise [CoachClient::NotFound] if the subscription does not exist @param [CoachClient::User] user @param [Integer] size the number of entries @param [Integer] start the start of entries list @param [Boolean] all whether all entries are retrieved @return [CoachClient::Subscription] the updated subscription

# File lib/coach_client/subscription.rb, line 44
def update(user, size: 20, start: 0, all: false)
  response = {}
  if all
    start = 0
    size = @client.max_size
  end
  @entries = []
  loop do
    response = CoachClient::Request.get(url, username: user.username,
                                        password: user.password,
                                        params: { start: start, size: size })
    response = response.to_h
    break if response[:entries].nil?
    response[:entries].each do |e|
      tag = "entry#{@sport}"
      id = CoachClient::Entry.extract_id_from_uri(e[tag.to_sym][:uri])
      @entries << CoachClient::Entry.new(client, self, id: id)
    end
    break unless all && next?(response[:links])
    start += size
  end
  @id = response[:id]
  @datesubscribed = response[:datesubscribed]
  @publicvisible = response[:publicvisible]
  self
end