Class: CoachClient::Partnership

Inherits:
Resource
  • Object
show all
Defined in:
lib/coach_client/partnership.rb

Overview

A partnership resource of the CyberCoach service.

Instance Attribute Summary collapse

Attributes inherited from Resource

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#exist?, #to_h

Constructor Details

#initialize(client, user1, user2, publicvisible: nil) ⇒ CoachClient::Partnership

Creates a new partnership.

Parameters:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/coach_client/partnership.rb', line 86

def initialize(client, user1, user2, publicvisible: nil)
  super(client)
  @user1 = if user1.is_a?(CoachClient::User)
             user1
           else
             CoachClient::User.new(client, user1)
           end
  @user2 = if user2.is_a?(CoachClient::User)
             user2
           else
             CoachClient::User.new(client, user2)
           end
  @publicvisible = publicvisible
end

Instance Attribute Details

#datecreatedInteger (readonly)

Returns:

  • (Integer)


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

def datecreated
  @datecreated
end

#idInteger (readonly)

Returns:

  • (Integer)


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

def id
  @id
end

#publicvisibleInteger

Returns:

  • (Integer)


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

def publicvisible
  @publicvisible
end

#subscriptionsArray<CoachClient::Subscription> (readonly)

Returns:



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

def subscriptions
  @subscriptions
end

#user1CoachClient::User

Returns:



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

def user1
  @user1
end

#user1_confirmedBoolean (readonly)

Returns:

  • (Boolean)


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

def user1_confirmed
  @user1_confirmed
end

#user2CoachClient::User

Returns:



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

def user2
  @user2
end

#user2_confirmedBoolean (readonly)

Returns:

  • (Boolean)


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

def user2_confirmed
  @user2_confirmed
end

Class Method Details

.extract_users_from_uri(uri) ⇒ Array<String>

Extracts the usernames from the partnership URI

Parameters:

  • uri (String)

Returns:

  • (Array<String>)

    the usernames



30
31
32
33
# File 'lib/coach_client/partnership.rb', line 30

def self.extract_users_from_uri(uri)
  match = uri.match(/partnerships\/(\w+);(\w+)\//)
  match.captures
end

.list(client, size: 20, start: 0, all: false) {|partnership| ... } ⇒ Array<CoachClient::Partnership>

Returns a list of partnerships from the CyberCoach service for which the given block returns a true value.

If no block is given, the whole list is returned.

Parameters:

  • client (CoachClient::Client)
  • size (Integer) (defaults to: 20)
  • start (Integer) (defaults to: 0)
  • all (Boolean) (defaults to: false)

Yield Parameters:

Yield Returns:

  • (Boolean)

    whether the partnership should be added to the list

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/coach_client/partnership.rb', line 57

def self.list(client, size: 20, start: 0, all: false)
  list = []
  if all
    total = self.total(client)
    start = 0
    size = client.max_size
  end
  loop do
    response = CoachClient::Request.get(client.url + path,
                                        params: { start: start, size: size })
    response.to_h[:partnerships].each do |p|
      user1, user2 = extract_users_from_uri(p[:uri])
      partnership = new(client, user1, user2)
      list << partnership if !block_given? || yield(partnership)
    end
    break unless all
    start += size
    break if start >= total
  end
  list
end

.pathString

Returns the relative path to the partnership resource.

Returns:

  • (String)

    the relative path



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

def self.path
  'partnerships/'
end

.total(client) ⇒ Integer

Returns the total number of partnerships present on the CyberCoach service.

Parameters:

Returns:

  • (Integer)

    the total number of partnerships



39
40
41
42
43
# File 'lib/coach_client/partnership.rb', line 39

def self.total(client)
  response = CoachClient::Request.get(client.url + path,
                                      params: { size: 0 })
  response.to_h[:available]
end

Instance Method Details

#cancelCoachClient::Partnership

Cancels the partnership on the CyberCoach service.

This sets the confirmation status of user1 to false.

Returns:

Raises:



205
206
207
208
209
210
# File 'lib/coach_client/partnership.rb', line 205

def cancel
  response = CoachClient::Request.delete(url, username: @user1.username,
                                         password: @user1.password)
  set_user_confirmed(response.to_h)
  self
end

#confirmCoachClient::Partnership

Confirms the partnership on the CyberCoach service.

Returns:

Raises:



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/coach_client/partnership.rb', line 187

def confirm
  response = CoachClient::Request.put(url, username: @user2.username,
                                      password: @user2.password,
                                      payload: payload,
                                      content_type: :xml)
  unless response.code == 200 || response.code == 201
    fail CoachClient::NotConfirmed.new(self), 'Could not confirm partnership'
  end
  set_user_confirmed(response.to_h)
  self
end

#deletetrue

Deletes the partnership on the CyberCoach service.

Returns:

  • (true)

Raises:



230
231
232
233
234
235
236
237
238
239
# File 'lib/coach_client/partnership.rb', line 230

def delete
  fail CoachClient::NotFound unless exist?
  invalidate if @user2_confirmed
  if @user1_confirmed
    response = CoachClient::Request.delete(url, username: @user1.username,
                                          password: @user1.password)
    set_user_confirmed(response.to_h)
  end
  true
end

#invalidateCoachClient::Partnership

Invalidates the partnership on the CyberCoach service.

This sets the confirmation status of user2 to false.

Returns:

Raises:



218
219
220
221
222
223
# File 'lib/coach_client/partnership.rb', line 218

def invalidate
  response = CoachClient::Request.delete(url, username: @user2.username,
                                         password: @user2.password)
  set_user_confirmed(response.to_h)
  self
end

#operational?Boolean

Returns whether the partnership is operational.

Returns:

  • (Boolean)


244
245
246
# File 'lib/coach_client/partnership.rb', line 244

def operational?
  @user1_confirmed && @user2_confirmed
end

#proposeCoachClient::Partnership

Proposes the partnership on the CyberCoach service.

Returns:

Raises:



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/coach_client/partnership.rb', line 168

def propose
  response = CoachClient::Request.put(url, username: @user1.username,
                                      password: @user1.password,
                                      payload: payload,
                                      content_type: :xml)
  unless response.code == 200 || response.code == 201
    fail CoachClient::NotProposed.new(self), 'Could not propose partnership'
  end
  set_user_confirmed(response.to_h)
  self
end

#saveCoachClient::Partnership

Saves the partnership to the CyberCoach service.

The partnership is created if it does not exist on the CyberCoach service, otherwise it tries to overwrite it.

Returns:

Raises:



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/coach_client/partnership.rb', line 139

def save
  unless operational?
    propose unless @user1_confirmed
    return confirm
  end
  response = begin
               CoachClient::Request.put(url, username: @user1.username,
                                        password: @user1.password,
                                        payload: payload,
                                        content_type: :xml)
             rescue CoachClient::Exception
               CoachClient::Request.put(url, username: @user2.username,
                                        password: @user2.password,
                                        payload: payload,
                                        content_type: :xml)
             end
  unless response.code == 200 || response.code == 201
    fail CoachClient::NotSaved.new(self), 'Could not save partnership'
  end
  self
end

#to_sString

Returns the string representation of the user.

Returns:

  • (String)


258
259
260
# File 'lib/coach_client/partnership.rb', line 258

def to_s
  "#{@user1.username};#{@user2.username}"
end

#updateCoachClient::Partnership

Updates the partnership with the data from the CyberCoach service.

Returns:

Raises:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/coach_client/partnership.rb', line 105

def update
  response = begin
               CoachClient::Request.get(url, username: @user1.username,
                                        password: @user1.password)
             rescue CoachClient::Exception
               CoachClient::Request.get(url, username: @user2.username,
                                        password: @user2.password)
             end
  response = response.to_h
  @id = response[:id]
  @datecreated = response[:datecreated]
  @publicvisible = response[:publicvisible]
  set_user_confirmed(response)
  @subscriptions = []
  unless response[:subscriptions].nil?
    response[:subscriptions].each do |s|
      sport = s[:uri].match(/\/(\w+)\/\z/).captures.first
      @subscriptions << CoachClient::PartnershipSubscription.new(client, self,
                                                                 sport)
    end
  end
  self
end

#urlString

Returns the URL of the partnership.

Returns:

  • (String)

    the url of the partnership



251
252
253
# File 'lib/coach_client/partnership.rb', line 251

def url
  "#{@client.url}#{self.class.path}#{@user1.username};#{@user2.username}"
end