Class: CoachClient::Partnership
- Defined in:
- lib/coach_client/partnership.rb
Overview
A partnership resource of the CyberCoach service.
Constant Summary collapse
- LIST_ALL_SIZE =
The size of the requests for the list with all = true
1000
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.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/coach_client/partnership.rb', line 89 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)
8 9 10 |
# File 'lib/coach_client/partnership.rb', line 8 def datecreated @datecreated end |
#id ⇒ Integer (readonly)
8 9 10 |
# File 'lib/coach_client/partnership.rb', line 8 def id @id end |
#publicvisible ⇒ Integer
20 21 22 |
# File 'lib/coach_client/partnership.rb', line 20 def publicvisible @publicvisible end |
#subscriptions ⇒ Array<CoachClient::Subscription> (readonly)
14 15 16 |
# File 'lib/coach_client/partnership.rb', line 14 def subscriptions @subscriptions end |
#user1 ⇒ CoachClient::User
17 18 19 |
# File 'lib/coach_client/partnership.rb', line 17 def user1 @user1 end |
#user1_confirmed ⇒ Boolean (readonly)
11 12 13 |
# File 'lib/coach_client/partnership.rb', line 11 def user1_confirmed @user1_confirmed end |
#user2 ⇒ CoachClient::User
17 18 19 |
# File 'lib/coach_client/partnership.rb', line 17 def user2 @user2 end |
#user2_confirmed ⇒ Boolean (readonly)
11 12 13 |
# File 'lib/coach_client/partnership.rb', line 11 def user2_confirmed @user2_confirmed end |
Class Method Details
.extract_users_from_uri(uri) ⇒ Array<String>
Extracts the usernames from the partnership URI
33 34 35 36 |
# File 'lib/coach_client/partnership.rb', line 33 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.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/coach_client/partnership.rb', line 60 def self.list(client, size: 20, start: 0, all: false) list = [] if all total = self.total(client) start = 0 size = LIST_ALL_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.
25 26 27 |
# File 'lib/coach_client/partnership.rb', line 25 def self.path 'partnerships/' end |
.total(client) ⇒ Integer
Returns the total number of partnerships present on the CyberCoach service.
42 43 44 45 46 |
# File 'lib/coach_client/partnership.rb', line 42 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.
208 209 210 211 212 213 |
# File 'lib/coach_client/partnership.rb', line 208 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.
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/coach_client/partnership.rb', line 190 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.
233 234 235 236 237 238 239 240 241 242 |
# File 'lib/coach_client/partnership.rb', line 233 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.
221 222 223 224 225 226 |
# File 'lib/coach_client/partnership.rb', line 221 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.
247 248 249 |
# File 'lib/coach_client/partnership.rb', line 247 def operational? @user1_confirmed && @user2_confirmed end |
#propose ⇒ CoachClient::Partnership
Proposes the partnership on the CyberCoach service.
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/coach_client/partnership.rb', line 171 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.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/coach_client/partnership.rb', line 142 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.
261 262 263 |
# File 'lib/coach_client/partnership.rb', line 261 def to_s "#{@user1.username};#{@user2.username}" end |
#update ⇒ CoachClient::Partnership
Updates the partnership with the data from the CyberCoach service.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/coach_client/partnership.rb', line 108 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.
254 255 256 |
# File 'lib/coach_client/partnership.rb', line 254 def url "#{@client.url}#{self.class.path}#{@user1.username};#{@user2.username}" end |