Class: CoachClient::Entry
- Defined in:
- lib/coach_client/entry.rb
Overview
A entry resource of the CyberCoach serivce.
Instance Attribute Summary collapse
-
#bicycletype ⇒ String
For cycling.
- #comment ⇒ String
-
#courselength ⇒ Integer
For cycling and running.
-
#coursetype ⇒ String
For cycling and running.
- #datecreated ⇒ Integer readonly
- #datemodified ⇒ Integer readonly
- #entrydate ⇒ Date
- #entryduration ⇒ Integer
- #entrylocation ⇒ String
- #id ⇒ Integer readonly
-
#numberofrounds ⇒ Integer
For every sport but soccer.
- #publicvisible ⇒ Integer
-
#roundduration ⇒ Integer
For boxing.
- #subscription ⇒ CoachClient::Subscription
-
#track ⇒ String
For cycling and running.
Attributes inherited from Resource
Class Method Summary collapse
-
.extract_id_from_uri(uri) ⇒ String
Extracts the entry id from the URI.
Instance Method Summary collapse
-
#create ⇒ CoachClient::Entry
Creates the entry on the CyberCoach service.
-
#delete ⇒ true
Deletes the entry on the CyberCoach service.
-
#exist? ⇒ Boolean
Returns whether the resource exists on the CyberCoach service.
-
#initialize(client, subscription, info = {}) ⇒ CoachClient::Entry
constructor
Creates a new entry.
-
#save ⇒ CoachClient::Entry
Saves the entry to the CyberCoach service.
-
#to_s ⇒ String
Returns the string representation of the entry.
-
#update ⇒ CoachClient::User
Updates the entry with the data from the CyberCoach service.
-
#url ⇒ String
Returns the URL of the entry.
-
#user ⇒ CoachClient::User
Returns the user that is used for the authentication.
Methods inherited from Resource
Constructor Details
#initialize(client, subscription, info = {}) ⇒ CoachClient::Entry
Creates a new entry.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# 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 |
Instance Attribute Details
#bicycletype ⇒ String
For cycling.
42 43 44 |
# File 'lib/coach_client/entry.rb', line 42 def bicycletype @bicycletype end |
#comment ⇒ String
17 18 19 |
# File 'lib/coach_client/entry.rb', line 17 def comment @comment end |
#courselength ⇒ Integer
For cycling and running.
32 33 34 |
# File 'lib/coach_client/entry.rb', line 32 def courselength @courselength end |
#coursetype ⇒ String
For cycling and running.
37 38 39 |
# File 'lib/coach_client/entry.rb', line 37 def coursetype @coursetype end |
#datecreated ⇒ Integer (readonly)
5 6 7 |
# File 'lib/coach_client/entry.rb', line 5 def datecreated @datecreated end |
#datemodified ⇒ Integer (readonly)
5 6 7 |
# File 'lib/coach_client/entry.rb', line 5 def datemodified @datemodified end |
#entrydate ⇒ Date
11 12 13 |
# File 'lib/coach_client/entry.rb', line 11 def entrydate @entrydate end |
#entryduration ⇒ Integer
8 9 10 |
# File 'lib/coach_client/entry.rb', line 8 def entryduration @entryduration end |
#entrylocation ⇒ String
17 18 19 |
# File 'lib/coach_client/entry.rb', line 17 def entrylocation @entrylocation end |
#id ⇒ Integer (readonly)
5 6 7 |
# File 'lib/coach_client/entry.rb', line 5 def id @id end |
#numberofrounds ⇒ Integer
For every sport but soccer.
22 23 24 |
# File 'lib/coach_client/entry.rb', line 22 def numberofrounds @numberofrounds end |
#publicvisible ⇒ Integer
8 9 10 |
# File 'lib/coach_client/entry.rb', line 8 def publicvisible @publicvisible end |
#roundduration ⇒ Integer
For boxing.
27 28 29 |
# File 'lib/coach_client/entry.rb', line 27 def roundduration @roundduration end |
#subscription ⇒ CoachClient::Subscription
14 15 16 |
# File 'lib/coach_client/entry.rb', line 14 def subscription @subscription end |
#track ⇒ String
For cycling and running.
37 38 39 |
# File 'lib/coach_client/entry.rb', line 37 def track @track end |
Class Method Details
.extract_id_from_uri(uri) ⇒ String
Extracts the entry id from the URI
48 49 50 51 |
# File 'lib/coach_client/entry.rb', line 48 def self.extract_id_from_uri(uri) match = uri.match(/\/(\d+)\/\z/) match.captures.first end |
Instance Method Details
#create ⇒ CoachClient::Entry
Creates the entry on the CyberCoach service.
133 134 135 136 137 138 139 140 141 142 143 144 |
# 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 ⇒ true
Deletes the entry on the CyberCoach service.
189 190 191 192 193 194 |
# 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? ⇒ Boolean
Returns whether the resource exists on the CyberCoach service.
199 200 201 202 |
# File 'lib/coach_client/entry.rb', line 199 def exist? return false unless @id super(username: user.username, password: user.password) end |
#save ⇒ CoachClient::Entry
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.
156 157 158 159 160 161 162 163 164 165 166 |
# 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 ⇒ String
Returns the string representation of the entry.
214 215 216 |
# File 'lib/coach_client/entry.rb', line 214 def to_s @id.to_s end |
#update ⇒ CoachClient::User
Updates the entry with the data from the CyberCoach service.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# 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 ⇒ String
Returns the URL of the entry.
207 208 209 |
# File 'lib/coach_client/entry.rb', line 207 def url "#{@subscription.url}/#{@id}" end |
#user ⇒ CoachClient::User
Returns the user that is used for the authentication.
171 172 173 174 175 176 177 178 179 180 181 182 |
# 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 |