Class: CoachClient::Partnership
- Defined in:
- lib/coach_client/partnership.rb
Overview
A partnership resource of the CyberCoach service.
Instance Attribute Summary collapse
- #datecreated ⇒ Integer readonly
- #id ⇒ Integer readonly
- #publicvisible ⇒ Integer
- #subscriptions ⇒ Array<CoachClient::Subscription> readonly
- #user1 ⇒ CoachClient::User
- #user1_confirmed ⇒ Boolean readonly
- #user2 ⇒ CoachClient::User
- #user2_confirmed ⇒ Boolean readonly
Attributes inherited from Resource
Class Method Summary collapse
-
.extract_users_from_uri(uri) ⇒ Array<String>
Extracts the usernames from the partnership URI.
-
.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.
-
.path ⇒ String
Returns the relative path to the partnership resource.
-
.total(client) ⇒ Integer
Returns the total number of partnerships present on the CyberCoach service.
Instance Method Summary collapse
-
#cancel ⇒ CoachClient::Partnership
Cancels the partnership on the CyberCoach service.
-
#confirm ⇒ CoachClient::Partnership
Confirms the partnership on the CyberCoach service.
-
#delete ⇒ true
Deletes the partnership on the CyberCoach service.
-
#initialize(client, user1, user2, publicvisible: nil) ⇒ CoachClient::Partnership
constructor
Creates a new partnership.
-
#invalidate ⇒ CoachClient::Partnership
Invalidates the partnership on the CyberCoach service.
-
#operational? ⇒ Boolean
Returns whether the partnership is operational.
-
#propose ⇒ CoachClient::Partnership
Proposes the partnership on the CyberCoach service.
-
#save ⇒ CoachClient::Partnership
Saves the partnership to the CyberCoach service.
-
#to_s ⇒ String
Returns the string representation of the user.
-
#update ⇒ CoachClient::Partnership
Updates the partnership with the data from the CyberCoach service.
-
#url ⇒ String
Returns the URL of the partnership.
Methods inherited from Resource
Constructor Details
#initialize(client, user1, user2, publicvisible: nil) ⇒ CoachClient::Partnership
Creates a new partnership.
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
#datecreated ⇒ Integer (readonly)
5 6 7 |
# File 'lib/coach_client/partnership.rb', line 5 def datecreated @datecreated end |
#id ⇒ Integer (readonly)
5 6 7 |
# File 'lib/coach_client/partnership.rb', line 5 def id @id end |
#publicvisible ⇒ Integer
17 18 19 |
# File 'lib/coach_client/partnership.rb', line 17 def publicvisible @publicvisible end |
#subscriptions ⇒ Array<CoachClient::Subscription> (readonly)
11 12 13 |
# File 'lib/coach_client/partnership.rb', line 11 def subscriptions @subscriptions end |
#user1 ⇒ CoachClient::User
14 15 16 |
# File 'lib/coach_client/partnership.rb', line 14 def user1 @user1 end |
#user1_confirmed ⇒ Boolean (readonly)
8 9 10 |
# File 'lib/coach_client/partnership.rb', line 8 def user1_confirmed @user1_confirmed end |
#user2 ⇒ CoachClient::User
14 15 16 |
# File 'lib/coach_client/partnership.rb', line 14 def user2 @user2 end |
#user2_confirmed ⇒ Boolean (readonly)
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
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.
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 |
.path ⇒ String
Returns the relative path to the partnership resource.
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.
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
#cancel ⇒ CoachClient::Partnership
Cancels the partnership on the CyberCoach service.
This sets the confirmation status of user1 to false.
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 |
#confirm ⇒ CoachClient::Partnership
Confirms the partnership on the CyberCoach service.
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 |
#delete ⇒ true
Deletes the partnership on the CyberCoach service.
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 |
#invalidate ⇒ CoachClient::Partnership
Invalidates the partnership on the CyberCoach service.
This sets the confirmation status of user2 to false.
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.
244 245 246 |
# File 'lib/coach_client/partnership.rb', line 244 def operational? @user1_confirmed && @user2_confirmed end |
#propose ⇒ CoachClient::Partnership
Proposes the partnership on the CyberCoach service.
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 |
#save ⇒ CoachClient::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.
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_s ⇒ String
Returns the string representation of the user.
258 259 260 |
# File 'lib/coach_client/partnership.rb', line 258 def to_s "#{@user1.username};#{@user2.username}" end |
#update ⇒ CoachClient::Partnership
Updates the partnership with the data from the CyberCoach service.
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 |
#url ⇒ String
Returns 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 |