Class: CoachClient::Entry

Inherits:
Resource show all
Defined in:
lib/coach_client/entry.rb

Overview

A entry resource of the CyberCoach serivce.

Instance Attribute Summary collapse

Attributes inherited from Resource

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#to_h

Constructor Details

#initialize(client, subscription, info = {}) ⇒ CoachClient::Entry

Creates a new entry.

Parameters:

Options Hash (info):

  • :id (Integer)
  • :publicvisible (Integer)
  • :comment (String)
  • :entrydate (Date)
  • :entryduration (Integer)
  • :entrylocation (String)
  • :roundduration (Integer)

    for boxing

  • :numberofrounds (Integer)

    except for soccer

  • :courselength (Integer)

    for cycling and running

  • :coursetype (String)

    for cycling and running

  • :track (String)

    for cycling and running

  • :bicycletype (String)

    for cycling



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

#bicycletypeString

For cycling.

Returns:

  • (String)


42
43
44
# File 'lib/coach_client/entry.rb', line 42

def bicycletype
  @bicycletype
end

#commentString

Returns:

  • (String)


17
18
19
# File 'lib/coach_client/entry.rb', line 17

def comment
  @comment
end

#courselengthInteger

For cycling and running.

Returns:

  • (Integer)


32
33
34
# File 'lib/coach_client/entry.rb', line 32

def courselength
  @courselength
end

#coursetypeString

For cycling and running.

Returns:

  • (String)


37
38
39
# File 'lib/coach_client/entry.rb', line 37

def coursetype
  @coursetype
end

#datecreatedInteger (readonly)

Returns:

  • (Integer)


5
6
7
# File 'lib/coach_client/entry.rb', line 5

def datecreated
  @datecreated
end

#datemodifiedInteger (readonly)

Returns:

  • (Integer)


5
6
7
# File 'lib/coach_client/entry.rb', line 5

def datemodified
  @datemodified
end

#entrydateDate

Returns:

  • (Date)


11
12
13
# File 'lib/coach_client/entry.rb', line 11

def 
  @entrydate
end

#entrydurationInteger

Returns:

  • (Integer)


8
9
10
# File 'lib/coach_client/entry.rb', line 8

def entryduration
  @entryduration
end

#entrylocationString

Returns:

  • (String)


17
18
19
# File 'lib/coach_client/entry.rb', line 17

def entrylocation
  @entrylocation
end

#idInteger (readonly)

Returns:

  • (Integer)


5
6
7
# File 'lib/coach_client/entry.rb', line 5

def id
  @id
end

#numberofroundsInteger

For every sport but soccer.

Returns:

  • (Integer)


22
23
24
# File 'lib/coach_client/entry.rb', line 22

def numberofrounds
  @numberofrounds
end

#publicvisibleInteger

Returns:

  • (Integer)


8
9
10
# File 'lib/coach_client/entry.rb', line 8

def publicvisible
  @publicvisible
end

#rounddurationInteger

For boxing.

Returns:

  • (Integer)


27
28
29
# File 'lib/coach_client/entry.rb', line 27

def roundduration
  @roundduration
end

#subscriptionCoachClient::Subscription



14
15
16
# File 'lib/coach_client/entry.rb', line 14

def subscription
  @subscription
end

#trackString

For cycling and running.

Returns:

  • (String)


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

Parameters:

  • uri (String)

Returns:

  • (String)

    the entry id



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

#createCoachClient::Entry

Creates the entry on the CyberCoach service.

Returns:

Raises:



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

#deletetrue

Deletes the entry on the CyberCoach service.

Returns:

  • (true)

Raises:



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.

Returns:

  • (Boolean)


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

#saveCoachClient::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.

Returns:

Raises:



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_sString

Returns the string representation of the entry.

Returns:

  • (String)


214
215
216
# File 'lib/coach_client/entry.rb', line 214

def to_s
  @id.to_s
end

#updateCoachClient::User

Updates the entry with the data from the CyberCoach service.

Returns:

Raises:



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

#urlString

Returns the URL of the entry.

Returns:

  • (String)

    the url of the entry



207
208
209
# File 'lib/coach_client/entry.rb', line 207

def url
  "#{@subscription.url}/#{@id}"
end

#userCoachClient::User

Returns the user that is used for the authentication.

Returns:



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