class CoachClient::Entry

A entry resource of the CyberCoach serivce.

Attributes

bicycletype[RW]

For cycling.

@return [String]

comment[RW]

@return [String]

courselength[RW]

For cycling and running.

@return [Integer]

coursetype[RW]

For cycling and running.

@return [String]

datecreated[R]

@return [Integer]

datemodified[R]

@return [Integer]

entrydate[RW]

@return [Date]

entryduration[RW]

@return [Integer]

entrylocation[RW]

@return [String]

id[R]

@return [Integer]

numberofrounds[RW]

For every sport but soccer.

@return [Integer]

publicvisible[RW]

@return [Integer]

roundduration[RW]

For boxing.

@return [Integer]

subscription[RW]

@return [CoachClient::Subscription]

track[RW]

For cycling and running.

@return [String]

Public Class Methods

extract_id_from_uri(uri) click to toggle source

Extracts the entry id from the URI

@param [String] uri @return [String] the entry id

# File lib/coach_client/entry.rb, line 48
def self.extract_id_from_uri(uri)
  match = uri.match(/\/(\d+)\/\z/)
  match.captures.first
end
new(client, subscription, info = {}) click to toggle source

Creates a new entry.

@param [CoachClient::Client] client @param [CoachClient::Subscription] subscription @param [Hash] info @option info [Integer] :id @option info [Integer] :publicvisible @option info [String] :comment @option info [Date] :entrydate @option info [Integer] :entryduration @option info [String] :entrylocation @option info [Integer] :roundduration for boxing @option info [Integer] :numberofrounds except for soccer @option info [Integer] :courselength for cycling and running @option info [String] :coursetype for cycling and running @option info [String] :track for cycling and running @option info [String] :bicycletype for cycling @return [CoachClient::Entry]

Calls superclass method CoachClient::Resource::new
# File lib/coach_client/entry.rb, line 71
def initialize(client, subscription, info = {})
  super(client)
  @subscription = subscription
  @id = info[:id]
  @publicvisible = info[:publicvisible]
  @comment = info[:comment]
  @entrydate = info[:entrydate]
  @entryduration = info[:entryduration]
  @entrylocation = info[:entrylocation]
  unless subscription.sport.sport == :soccer
    @numberofrounds = info[:numberofrounds]
  end
  if subscription.sport.sport == :boxing
    @roundduration = info[:roundduration]
  end
  if subscription.sport.sport == :cycling || subscription.sport.sport == :running
    @courselength = info[:courselength]
    @coursetype = info[:coursetype]
    @track = info[:track]
  end
  @bicycletype = info[:bicycletype] if subscription.sport.sport == :cycling
end

Public Instance Methods

create() click to toggle source

Creates the entry on the CyberCoach service.

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

is given

@raise [CoachClient::NotSaved] if the entry could not be saved @return [CoachClient::Entry] the created entry

# File lib/coach_client/entry.rb, line 133
def create
  response = CoachClient::Request.post(@subscription.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 create entry'
  end
  @id = self.class.extract_id_from_uri(response.header[:location])
  self
end
delete() click to toggle source

Deletes the entry on the CyberCoach service.

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

# File lib/coach_client/entry.rb, line 189
def delete
  fail CoachClient::NotFound.new(self), 'Entry not found' unless exist?
  CoachClient::Request.delete(url, username: user.username,
                              password: user.password)
  true
end
exist?() click to toggle source

Returns whether the resource exists on the CyberCoach service.

@return [Boolean]

Calls superclass method CoachClient::Resource#exist?
# File lib/coach_client/entry.rb, line 199
def exist?
  return false unless @id
  super(username: user.username, password: user.password)
end
save() click to toggle source

Saves the entry to the CyberCoach service.

The entry 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 entry could not be saved @return [CoachClient::Entry] the created entry

# File lib/coach_client/entry.rb, line 156
def save
  return create unless @id
  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 entry'
  end
  self
end
to_s() click to toggle source

Returns the string representation of the entry.

@return [String]

# File lib/coach_client/entry.rb, line 214
def to_s
  @id.to_s
end
update() click to toggle source

Updates the entry with the data from the CyberCoach service.

@raise [CoachClient::NotFound] if the entry does not exist @return [CoachClient::User] the updated user

# File lib/coach_client/entry.rb, line 98
def update
  fail CoachClient::NotFound.new(self), 'Entry not found' if @id.nil?
  response = CoachClient::Request.get(url, username: user.username,
                                      password: user.password)
  tag = "entry#{@subscription.sport}"
  response = response.to_h[tag.to_sym]
  @datecreated = response[:datecreated]
  @datemodified = response[:datemodified]
  @publicvisible = response[:publicvisible]
  @comment = response[:comment]
  @entrydate = response[:entrydate]
  @entryduration = response[:entryduration]
  @entrylocation = response[:entrylocation]
  unless @subscription.sport.sport == :soccer
    @numberofrounds = response[:numberofrounds]
  end
  if @subscription.sport.sport == :boxing
    @roundduration = response[:roundduration]
  end
  if @subscription.sport.sport == :cycling || @subscription.sport.sport == :running
    @courselength = response[:courselength]
    @coursetype = response[:coursetype]
    @track = response[:track]
  end
  @bicycletype = response[:bicycletype] if @subscription.sport.sport == :cycling
  self
end
url() click to toggle source

Returns the URL of the entry.

@return [String] the url of the entry

# File lib/coach_client/entry.rb, line 207
def url
  "#{@subscription.url}/#{@id}"
end
user() click to toggle source

Returns the user that is used for the authentication.

@return [CoachClient::User]

# File lib/coach_client/entry.rb, line 171
def user
  if @subscription.is_a?(CoachClient::PartnershipSubscription)
    partnership = @subscription.partnership
    if partnership.user1.authenticated?
      partnership.user1
    else
      partnership.user2
    end
  else
    @subscription.user
  end
end

Private Instance Methods

payload() click to toggle source
# File lib/coach_client/entry.rb, line 220
def payload
  vals = to_h
  vals.delete(:subscription)
  vals.delete_if { |_k, v| v.nil? || v.to_s.empty? }
  tag = "entry#{@subscription.sport}"
  Gyoku.xml(tag.to_sym => vals)
end