Class: CoachClient::Subscription
- Defined in:
- lib/coach_client/subscription.rb
Overview
Note:
Use the subclass UserSubscription or PartnershipSubscription for a user or partnership subscription respectively.
A subscription resource of the CyberCoach service.
Direct Known Subclasses
Instance Attribute Summary collapse
- #datesubscribed ⇒ Integer readonly
- #entries ⇒ Array<CoachClient::Entry> readonly
- #id ⇒ Integer readonly
- #publicvisible ⇒ Integer
- #sport ⇒ CoachClient::Sport
Attributes inherited from Resource
Instance Method Summary collapse
-
#delete(user) ⇒ true
Deletes the subscription on the CyberCoach service.
-
#initialize(client, sport, publicvisible: nil) ⇒ CoachClient::Subscription
constructor
Creates a new subscription.
-
#save(user) ⇒ CoachClient::Subscription
Saves the subscription to the CyberCoach service.
-
#update(user, size: 20, start: 0, all: false) ⇒ CoachClient::Subscription
Updates the subscription with the data from the CyberCoach service.
Methods inherited from Resource
Constructor Details
#initialize(client, sport, publicvisible: nil) ⇒ CoachClient::Subscription
Creates a new subscription.
26 27 28 29 30 31 32 33 34 |
# File 'lib/coach_client/subscription.rb', line 26 def initialize(client, sport, publicvisible: nil) super(client) @sport = if sport.is_a?(CoachClient::Sport) sport else CoachClient::Sport.new(client, sport) end @publicvisible = publicvisible end |
Instance Attribute Details
#datesubscribed ⇒ Integer (readonly)
9 10 11 |
# File 'lib/coach_client/subscription.rb', line 9 def datesubscribed @datesubscribed end |
#entries ⇒ Array<CoachClient::Entry> (readonly)
12 13 14 |
# File 'lib/coach_client/subscription.rb', line 12 def entries @entries end |
#id ⇒ Integer (readonly)
9 10 11 |
# File 'lib/coach_client/subscription.rb', line 9 def id @id end |
#publicvisible ⇒ Integer
18 19 20 |
# File 'lib/coach_client/subscription.rb', line 18 def publicvisible @publicvisible end |
#sport ⇒ CoachClient::Sport
15 16 17 |
# File 'lib/coach_client/subscription.rb', line 15 def sport @sport end |
Instance Method Details
#delete(user) ⇒ true
Deletes the subscription on the CyberCoach service.
105 106 107 108 109 |
# File 'lib/coach_client/subscription.rb', line 105 def delete(user) CoachClient::Request.delete(url, username: user.username, password: user.password) true end |
#save(user) ⇒ CoachClient::Subscription
Saves the subscription to the CyberCoach service.
The subscription is created if it does not exist on the CyberCoach service, otherwise it tries to overwrite it.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/coach_client/subscription.rb', line 82 def save(user) vals = to_h vals.delete(:user) vals.delete(:partnership) vals.delete(:sport) vals.delete_if { |_k, v| v.nil? || v.to_s.empty? } payload = Gyoku.xml(subscription: vals) 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 subscription' end self end |
#update(user, size: 20, start: 0, all: false) ⇒ CoachClient::Subscription
Updates the subscription with the data from the CyberCoach service.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/coach_client/subscription.rb', line 44 def update(user, size: 20, start: 0, all: false) response = {} if all start = 0 size = @client.max_size end @entries = [] loop do response = CoachClient::Request.get(url, username: user.username, password: user.password, params: { start: start, size: size }) response = response.to_h break if response[:entries].nil? response[:entries].each do |e| tag = "entry#{@sport}" id = CoachClient::Entry.extract_id_from_uri(e[tag.to_sym][:uri]) @entries << CoachClient::Entry.new(client, self, id: id) end break unless all && next?(response[:links]) start += size end @id = response[:id] @datesubscribed = response[:datesubscribed] @publicvisible = response[:publicvisible] self end |