class CoachClient::Entry
A entry resource of the CyberCoach serivce.
Attributes
For cycling.
@return [String]
@return [String]
For cycling and running.
@return [Integer]
For cycling and running.
@return [String]
@return [Integer]
@return [Integer]
@return [Date]
@return [Integer]
@return [String]
@return [Integer]
For every sport but soccer.
@return [Integer]
@return [Integer]
For boxing.
@return [Integer]
@return [CoachClient::Subscription]
For cycling and running.
@return [String]
Public Class Methods
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
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]
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
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
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
Returns whether the resource exists on the CyberCoach service.
@return [Boolean]
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
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
Returns the string representation of the entry.
@return [String]
# File lib/coach_client/entry.rb, line 214 def to_s @id.to_s end
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
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
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
# 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