Class: CoachClient::Subscription

Inherits:
Resource
  • Object
show all
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

PartnershipSubscription, UserSubscription

Instance Attribute Summary collapse

Attributes inherited from Resource

#client

Instance Method Summary collapse

Methods inherited from Resource

#exist?, #to_h

Constructor Details

#initialize(client, sport, publicvisible: nil) ⇒ CoachClient::Subscription

Creates a new subscription.

Parameters:



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

#datesubscribedInteger (readonly)

Returns:

  • (Integer)


9
10
11
# File 'lib/coach_client/subscription.rb', line 9

def datesubscribed
  @datesubscribed
end

#entriesArray<CoachClient::Entry> (readonly)

Returns:



12
13
14
# File 'lib/coach_client/subscription.rb', line 12

def entries
  @entries
end

#idInteger (readonly)

Returns:

  • (Integer)


9
10
11
# File 'lib/coach_client/subscription.rb', line 9

def id
  @id
end

#publicvisibleInteger

Returns:

  • (Integer)


18
19
20
# File 'lib/coach_client/subscription.rb', line 18

def publicvisible
  @publicvisible
end

#sportCoachClient::Sport

Returns:



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.

Parameters:

Returns:

  • (true)

Raises:



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.

Parameters:

Returns:

Raises:



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.

Parameters:

  • user (CoachClient::User)
  • size (Integer) (defaults to: 20)

    the number of entries

  • start (Integer) (defaults to: 0)

    the start of entries list

  • all (Boolean) (defaults to: false)

    whether all entries are retrieved

Returns:

Raises:



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