coach_client 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +248 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/coach_client.gemspec +32 -0
- data/lib/coach_client/client.rb +84 -0
- data/lib/coach_client/entry.rb +248 -0
- data/lib/coach_client/exceptions.rb +78 -0
- data/lib/coach_client/partnership.rb +294 -0
- data/lib/coach_client/partnership_subscription.rb +99 -0
- data/lib/coach_client/request.rb +99 -0
- data/lib/coach_client/resource.rb +48 -0
- data/lib/coach_client/response.rb +37 -0
- data/lib/coach_client/sport.rb +88 -0
- data/lib/coach_client/subscription.rb +116 -0
- data/lib/coach_client/user.rb +207 -0
- data/lib/coach_client/user_subscription.rb +79 -0
- data/lib/coach_client/version.rb +5 -0
- data/lib/coach_client.rb +21 -0
- metadata +186 -0
@@ -0,0 +1,99 @@
|
|
1
|
+
module CoachClient
|
2
|
+
# A partnership subscription resource of the CyberCoach service.
|
3
|
+
class PartnershipSubscription < CoachClient::Subscription
|
4
|
+
# @return [Integer]
|
5
|
+
attr_reader :id, :datesubscribed
|
6
|
+
|
7
|
+
# @return [CoachClient::Partnership]
|
8
|
+
attr_accessor :partnership
|
9
|
+
|
10
|
+
# Returns the relative path to the partnership subscription resource.
|
11
|
+
#
|
12
|
+
# @return [String] the relative path
|
13
|
+
def self.path
|
14
|
+
'partnerships/'
|
15
|
+
end
|
16
|
+
|
17
|
+
# Creates a new partnership subscription.
|
18
|
+
#
|
19
|
+
# @param [CoachClient::Client] client
|
20
|
+
# @param [String, CoachClient::Partnership] partnership
|
21
|
+
# @param [String, Symbol, CoachClient::Sport] sport
|
22
|
+
# @param [Integer] publicvisible
|
23
|
+
# @return [CoachClient::PartnershipSubscription]
|
24
|
+
def initialize(client, partnership, sport, publicvisible: nil)
|
25
|
+
super(client, sport, publicvisible: publicvisible)
|
26
|
+
@partnership = if partnership.is_a?(CoachClient::Partnership)
|
27
|
+
partnership
|
28
|
+
else
|
29
|
+
uri = "partnerships/#{partnership}/"
|
30
|
+
users = CoachClient::Partnership.extract_users_from_uri(uri)
|
31
|
+
CoachClient::Partnership.new(client, *users)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Updates the partnership subscription with the data from the CyberCoach
|
36
|
+
# service.
|
37
|
+
#
|
38
|
+
# @raise [CoachClient::NotFound] if the partnership subscription does not
|
39
|
+
# exist
|
40
|
+
# @return [CoachClient::PartnershipSubscription] the updated partnership
|
41
|
+
# subscription
|
42
|
+
def update
|
43
|
+
if @partnership.user1.authenticated?
|
44
|
+
super(@partnership.user1)
|
45
|
+
else
|
46
|
+
super(@partnership.user2)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Saves the partnership subscription to the CyberCoach service.
|
51
|
+
#
|
52
|
+
# The partnership subscription is created if it does not exist on the
|
53
|
+
# CyberCoach service, otherwise it tries to overwrite it.
|
54
|
+
#
|
55
|
+
# @raise [CoachClient::Unauthorized] if not authorized
|
56
|
+
# @raise [CoachClient::IncompleteInformation] if not all needed information
|
57
|
+
# is given
|
58
|
+
# @raise [CoachClient::NotSaved] if the partnership subscription could not
|
59
|
+
# be saved
|
60
|
+
# @return [CoachClient::PartnershipSubscription] the saved partnership
|
61
|
+
# subscription
|
62
|
+
def save
|
63
|
+
if @partnership.user1.authenticated?
|
64
|
+
super(@partnership.user1)
|
65
|
+
else
|
66
|
+
super(@partnership.user2)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Deletes the partnership subscription on the CyberCoach service.
|
71
|
+
#
|
72
|
+
# @raise [CoachClient::NotFound] if the partnership subscription does not
|
73
|
+
# exist
|
74
|
+
# @raise [CoachClient::Unauthorized] if not authorized
|
75
|
+
# @return [true]
|
76
|
+
def delete
|
77
|
+
if @partnership.user1.authenticated?
|
78
|
+
super(@partnership.user1)
|
79
|
+
else
|
80
|
+
super(@partnership.user2)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Returns the URL of the partnership subscription.
|
85
|
+
#
|
86
|
+
# @return [String] the url of the partnership subscription
|
87
|
+
def url
|
88
|
+
"#{@partnership.url}/#{@sport}"
|
89
|
+
end
|
90
|
+
|
91
|
+
# Returns the string representation of the partnership subscription.
|
92
|
+
#
|
93
|
+
# @return [String]
|
94
|
+
def to_s
|
95
|
+
"#{@partnership}/#{@sport}"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
@@ -0,0 +1,99 @@
|
|
1
|
+
module CoachClient
|
2
|
+
# Request methods for the HTTP verbs GET, PUT, POST, DELETE.
|
3
|
+
module Request
|
4
|
+
# The default header.
|
5
|
+
DEFAULT_HEADER = { accept: :json }
|
6
|
+
|
7
|
+
# GET request to the RESTful service.
|
8
|
+
#
|
9
|
+
# @param [String] url
|
10
|
+
# @param [String] username
|
11
|
+
# @param [String] password
|
12
|
+
# @param [Hash] header
|
13
|
+
# @return [CoachClient::Response]
|
14
|
+
def self.get(url, username: nil, password: nil, **header)
|
15
|
+
header.merge!(DEFAULT_HEADER)
|
16
|
+
begin
|
17
|
+
response = RestClient::Request.execute(method: :get, url: url,
|
18
|
+
user: username,
|
19
|
+
password: password,
|
20
|
+
headers: header)
|
21
|
+
rescue RestClient::ResourceNotFound => e
|
22
|
+
raise CoachClient::NotFound, e.message
|
23
|
+
rescue RestClient::Unauthorized => e
|
24
|
+
raise CoachClient::Unauthorized, e.message
|
25
|
+
end
|
26
|
+
CoachClient::Response.new(response.headers, response.body, response.code)
|
27
|
+
end
|
28
|
+
|
29
|
+
# PUT request to the RESTful service.
|
30
|
+
#
|
31
|
+
# @param [String] url
|
32
|
+
# @param [String] username
|
33
|
+
# @param [String] password
|
34
|
+
# @param [String] payload required
|
35
|
+
# @param [Hash] header
|
36
|
+
# @return [CoachClient::Response]
|
37
|
+
def self.put(url, username: nil, password: nil, payload:, **header)
|
38
|
+
header.merge!(DEFAULT_HEADER)
|
39
|
+
begin
|
40
|
+
response = RestClient::Request.execute(method: :put, url: url,
|
41
|
+
user: username,
|
42
|
+
password: password,
|
43
|
+
payload: payload,
|
44
|
+
headers: header)
|
45
|
+
rescue RestClient::ResourceNotFound => e
|
46
|
+
raise CoachClient::NotFound, e.message
|
47
|
+
rescue RestClient::Unauthorized => e
|
48
|
+
raise CoachClient::Unauthorized, e.message
|
49
|
+
end
|
50
|
+
CoachClient::Response.new(response.headers, response.body, response.code)
|
51
|
+
end
|
52
|
+
|
53
|
+
# POST request to the RESTful service.
|
54
|
+
#
|
55
|
+
# @param [String] url
|
56
|
+
# @param [String] username
|
57
|
+
# @param [String] password
|
58
|
+
# @param [String] payload required
|
59
|
+
# @param [Hash] header
|
60
|
+
# @return [CoachClient::Response]
|
61
|
+
def self.post(url, username: nil, password: nil, payload:, **header)
|
62
|
+
header.merge!(DEFAULT_HEADER)
|
63
|
+
begin
|
64
|
+
response = RestClient::Request.execute(method: :post, url: url,
|
65
|
+
user: username,
|
66
|
+
password: password,
|
67
|
+
payload: payload,
|
68
|
+
headers: header)
|
69
|
+
rescue RestClient::ResourceNotFound => e
|
70
|
+
raise CoachClient::NotFound, e.message
|
71
|
+
rescue RestClient::Unauthorized => e
|
72
|
+
raise CoachClient::Unauthorized, e.message
|
73
|
+
end
|
74
|
+
CoachClient::Response.new(response.headers, response.body, response.code)
|
75
|
+
end
|
76
|
+
|
77
|
+
# DELETE request to the RESTful service.
|
78
|
+
#
|
79
|
+
# @param [String] url
|
80
|
+
# @param [String] username
|
81
|
+
# @param [String] password
|
82
|
+
# @param [Hash] header
|
83
|
+
# @return [CoachClient::Response]
|
84
|
+
def self.delete(url, username: nil, password: nil, **header)
|
85
|
+
begin
|
86
|
+
response = RestClient::Request.execute(method: :delete, url: url,
|
87
|
+
user: username,
|
88
|
+
password: password,
|
89
|
+
headers: header)
|
90
|
+
rescue RestClient::ResourceNotFound => e
|
91
|
+
raise CoachClient::NotFound, e.message
|
92
|
+
rescue RestClient::Unauthorized => e
|
93
|
+
raise CoachClient::Unauthorized, e.message
|
94
|
+
end
|
95
|
+
CoachClient::Response.new(response.headers, response.body, response.code)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module CoachClient
|
2
|
+
# A resource of the CyberCoach service.
|
3
|
+
class Resource
|
4
|
+
# @return [CoachClient::Client]
|
5
|
+
attr_accessor :client
|
6
|
+
|
7
|
+
# Creates a new resource.
|
8
|
+
#
|
9
|
+
# @param [CoachClient::Client] client
|
10
|
+
# @return [CoachClient::Resource]
|
11
|
+
def initialize(client)
|
12
|
+
@client = client
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns whether the resource exists on the CyberCoach service.
|
16
|
+
#
|
17
|
+
# @param [String] username
|
18
|
+
# @param [String] password
|
19
|
+
# @return [Boolean]
|
20
|
+
def exist?(username: nil, password: nil)
|
21
|
+
begin
|
22
|
+
CoachClient::Request.get(url, username: username, password: password)
|
23
|
+
true
|
24
|
+
rescue CoachClient::NotFound
|
25
|
+
false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Returns the hash representation of the resource.
|
30
|
+
#
|
31
|
+
# @return [Hash]
|
32
|
+
def to_h
|
33
|
+
hash = {}
|
34
|
+
instance_variables.each do |var|
|
35
|
+
next if var.to_s == '@client'
|
36
|
+
value = instance_variable_get(var)
|
37
|
+
hash[var.to_s.delete('@').to_sym] =
|
38
|
+
if value && value.respond_to?(:to_h) && !value.is_a?(Array)
|
39
|
+
value.to_h
|
40
|
+
else
|
41
|
+
value
|
42
|
+
end
|
43
|
+
end
|
44
|
+
hash
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module CoachClient
|
2
|
+
# A response from RESTful request to the CyberCoach service.
|
3
|
+
class Response
|
4
|
+
# @return [Integer] the HTTP code
|
5
|
+
attr_reader :code
|
6
|
+
|
7
|
+
# @return [String] the HTTP headers
|
8
|
+
attr_reader :header
|
9
|
+
|
10
|
+
# Creates a new response.
|
11
|
+
#
|
12
|
+
# @param [String] header the headers of the HTTP response
|
13
|
+
# @param [String] body the body of the HTTP response
|
14
|
+
# @param [Integer] code the HTTP response code
|
15
|
+
# @return [CoachClient::Response]
|
16
|
+
def initialize(header, body, code)
|
17
|
+
@header = header
|
18
|
+
@body = body
|
19
|
+
@code = code
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns the body as Ruby Hash.
|
23
|
+
#
|
24
|
+
# @return [Hash]
|
25
|
+
def to_h
|
26
|
+
JSON.parse(@body, symbolize_names: true)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Returns the body as String.
|
30
|
+
#
|
31
|
+
# @return [String]
|
32
|
+
def to_s
|
33
|
+
@body.to_s
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module CoachClient
|
2
|
+
# A sport resource of the CyberCoach service.
|
3
|
+
class Sport < CoachClient::Resource
|
4
|
+
# @return [Integer]
|
5
|
+
attr_reader :id
|
6
|
+
|
7
|
+
# @return [Symbol]
|
8
|
+
attr_reader :sport
|
9
|
+
|
10
|
+
# @return [String]
|
11
|
+
attr_reader :name, :description
|
12
|
+
|
13
|
+
# Returns the relative path to the sport resource.
|
14
|
+
#
|
15
|
+
# @return [String] the relative path
|
16
|
+
def self.path
|
17
|
+
'sports/'
|
18
|
+
end
|
19
|
+
|
20
|
+
# Returns the total number of sports present on the CyberCoach service.
|
21
|
+
#
|
22
|
+
# @param [CoachClient::Client] client
|
23
|
+
# @return [Integer] the total number of sports
|
24
|
+
def self.total(client)
|
25
|
+
response = CoachClient::Request.get(client.url + path,
|
26
|
+
params: { size: 0 })
|
27
|
+
response.to_h[:available]
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns a list of sports from the CyberCoach service for which the given
|
31
|
+
# block returns a true value.
|
32
|
+
#
|
33
|
+
# If no block is given, the whole list is returned.
|
34
|
+
#
|
35
|
+
# @param [CoachClient::Client] client
|
36
|
+
# @yieldparam [CoachClient::Sport] sport the sport
|
37
|
+
# @yieldreturn [Boolean] whether the sport should be added to the list
|
38
|
+
# @return [Array<CoachClient::Sport>] the list of sports
|
39
|
+
def self.list(client)
|
40
|
+
sportlist = []
|
41
|
+
response = CoachClient::Request.get(client.url + path)
|
42
|
+
response.to_h[:sports].each do |s|
|
43
|
+
sport = self.new(client, s[:name])
|
44
|
+
sportlist << sport if !block_given? || yield(sport)
|
45
|
+
end
|
46
|
+
sportlist
|
47
|
+
end
|
48
|
+
|
49
|
+
# Creates a new sport.
|
50
|
+
#
|
51
|
+
# @param [CoachClient::Client] client
|
52
|
+
# @param [String, Symbol] sport
|
53
|
+
# @return [CoachClient::Sport]
|
54
|
+
def initialize(client, sport)
|
55
|
+
super(client)
|
56
|
+
@sport = sport.downcase.to_sym
|
57
|
+
end
|
58
|
+
|
59
|
+
# Updates the sport with the data from the CyberCoach service.
|
60
|
+
#
|
61
|
+
# @raise [CoachClient::NotFound] if the sport does not exist
|
62
|
+
# @return [CoachClient::Sport] the updated sport
|
63
|
+
def update
|
64
|
+
raise CoachClient::NotFound, 'Sport not found' unless exist?
|
65
|
+
response = CoachClient::Request.get(url)
|
66
|
+
response = response.to_h
|
67
|
+
@id = response[:id]
|
68
|
+
@name = response[:name]
|
69
|
+
@description = response[:description]
|
70
|
+
self
|
71
|
+
end
|
72
|
+
|
73
|
+
# Returns the URL of the sport.
|
74
|
+
#
|
75
|
+
# @return [String] the url of the sport
|
76
|
+
def url
|
77
|
+
@client.url + self.class.path + @sport.to_s
|
78
|
+
end
|
79
|
+
|
80
|
+
# Returns the string representation of the sport.
|
81
|
+
#
|
82
|
+
# @return [String]
|
83
|
+
def to_s
|
84
|
+
@sport.to_s
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
@@ -0,0 +1,116 @@
|
|
1
|
+
module CoachClient
|
2
|
+
# A subscription resource of the CyberCoach service.
|
3
|
+
#
|
4
|
+
# @note Use the subclass {CoachClient::UserSubscription} or
|
5
|
+
# {CoachClient::PartnershipSubscription} for a user or partnership
|
6
|
+
# subscription respectively.
|
7
|
+
class Subscription < CoachClient::Resource
|
8
|
+
# @return [Integer]
|
9
|
+
attr_reader :id, :datesubscribed
|
10
|
+
|
11
|
+
# @return [Array<CoachClient::Entry>]
|
12
|
+
attr_reader :entries
|
13
|
+
|
14
|
+
# @return [CoachClient::Sport]
|
15
|
+
attr_accessor :sport
|
16
|
+
|
17
|
+
# @return [Integer]
|
18
|
+
attr_accessor :publicvisible
|
19
|
+
|
20
|
+
# Creates a new subscription.
|
21
|
+
#
|
22
|
+
# @param [CoachClient::Client] client
|
23
|
+
# @param [String, Symbol, CoachClient::Sport] sport
|
24
|
+
# @param [Integer] publicvisible
|
25
|
+
# @return [CoachClient::Subscription]
|
26
|
+
def initialize(client, sport, publicvisible: nil)
|
27
|
+
super(client)
|
28
|
+
@sport = if sport.is_a?(CoachClient::Sport)
|
29
|
+
sport
|
30
|
+
else
|
31
|
+
CoachClient::Sport.new(client, sport)
|
32
|
+
end
|
33
|
+
@publicvisible = publicvisible
|
34
|
+
end
|
35
|
+
|
36
|
+
# Updates the subscription with the data from the CyberCoach service.
|
37
|
+
#
|
38
|
+
# @raise [CoachClient::NotFound] if the subscription does not exist
|
39
|
+
# @param [CoachClient::User] user
|
40
|
+
# @return [CoachClient::Subscription] the updated subscription
|
41
|
+
def update(user)
|
42
|
+
raise CoachClient::NotFound, 'Subscription not found' unless exist?
|
43
|
+
response = if user.authenticated?
|
44
|
+
CoachClient::Request.get(url, username: user.username,
|
45
|
+
password: user.password)
|
46
|
+
else
|
47
|
+
CoachClient::Request.get(url)
|
48
|
+
end
|
49
|
+
response = response.to_h
|
50
|
+
@id = response[:id]
|
51
|
+
@datesubscribed = response[:datesubscribed]
|
52
|
+
@publicvisible = response[:publicvisible]
|
53
|
+
@entries = []
|
54
|
+
unless response[:entries].nil?
|
55
|
+
response[:entries].each do |e|
|
56
|
+
tag = "entry#{@sport}"
|
57
|
+
id = CoachClient::Entry.extract_id_from_uri(e[tag.to_sym][:uri])
|
58
|
+
@entries << CoachClient::Entry.new(client, self, id: id)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
self
|
62
|
+
end
|
63
|
+
|
64
|
+
# Saves the subscription to the CyberCoach service.
|
65
|
+
#
|
66
|
+
# The subscription is created if it does not exist on the CyberCoach service,
|
67
|
+
# otherwise it tries to overwrite it.
|
68
|
+
#
|
69
|
+
# @raise [CoachClient::Unauthorized] if not authorized
|
70
|
+
# @raise [CoachClient::IncompleteInformation] if not all needed information
|
71
|
+
# is given
|
72
|
+
# @raise [CoachClient::NotSaved] if the subscription could not be saved
|
73
|
+
# @param [CoachClient::User] user
|
74
|
+
# @return [CoachClient::Subscription] the saved subscription
|
75
|
+
def save(user)
|
76
|
+
vals = self.to_h
|
77
|
+
vals.delete(:user)
|
78
|
+
vals.delete(:partnership)
|
79
|
+
vals.delete(:sport)
|
80
|
+
vals.delete_if { |_k, v| v.nil? || v.to_s.empty? }
|
81
|
+
payload = Gyoku.xml(subscription: vals)
|
82
|
+
unless user.authenticated?
|
83
|
+
raise CoachClient::Unauthorized.new(user), 'Unauthorized'
|
84
|
+
end
|
85
|
+
begin
|
86
|
+
response = CoachClient::Request.put(url, username: user.username,
|
87
|
+
password: user.password,
|
88
|
+
payload: payload,
|
89
|
+
content_type: :xml)
|
90
|
+
rescue RestClient::Conflict
|
91
|
+
raise CoachClient::IncompleteInformation.new(self), 'Incomplete information'
|
92
|
+
end
|
93
|
+
unless response.code == 200 || response.code == 201
|
94
|
+
raise CoachClient::NotSaved.new(self), 'Could not save subscription'
|
95
|
+
end
|
96
|
+
self
|
97
|
+
end
|
98
|
+
|
99
|
+
# Deletes the subscription on the CyberCoach service.
|
100
|
+
#
|
101
|
+
# @raise [CoachClient::NotFound] if the subscription does not exist
|
102
|
+
# @raise [CoachClient::Unauthorized] if not authorized
|
103
|
+
# @param [CoachClient::User] user
|
104
|
+
# @return [true]
|
105
|
+
def delete(user)
|
106
|
+
raise CoachClient::NotFound unless exist?
|
107
|
+
unless user.authenticated?
|
108
|
+
raise CoachClient::Unauthorized.new(user), 'Unauthorized'
|
109
|
+
end
|
110
|
+
CoachClient::Request.delete(url, username: user.username,
|
111
|
+
password: user.password)
|
112
|
+
true
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|