coach_client 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,207 @@
1
+ module CoachClient
2
+ # A user resource of the CyberCoach service.
3
+ class User < CoachClient::Resource
4
+ # The size of the requests for the {.list} with all = true
5
+ LIST_ALL_SIZE = 1000
6
+
7
+ # @return [String]
8
+ attr_reader :username
9
+
10
+ # @return [Integer]
11
+ attr_reader :datecreated
12
+
13
+ # @return [Array<CoachClient::Partnership>]
14
+ attr_reader :partnerships
15
+
16
+ # @return [Array<CoachClient::UserSubscription>]
17
+ attr_reader :subscriptions
18
+
19
+ # @return [String]
20
+ attr_accessor :password, :realname, :email, :newpassword
21
+
22
+ # @return [Integer]
23
+ attr_accessor :publicvisible
24
+
25
+ # Returns the relative path to the user resource.
26
+ #
27
+ # @return [String] the relative path
28
+ def self.path
29
+ 'users/'
30
+ end
31
+
32
+ # Returns the total number of users present on the CyberCoach service.
33
+ #
34
+ # @param [CoachClient::Client] client
35
+ # @return [Integer] the total number of users
36
+ def self.total(client)
37
+ response = CoachClient::Request.get(client.url + path,
38
+ params: { size: 0 })
39
+ response.to_h[:available]
40
+ end
41
+
42
+ # Returns a list of users from the CyberCoach service for which the given
43
+ # block returns a true value.
44
+ #
45
+ # If no block is given, the whole list is returned.
46
+ #
47
+ # @param [CoachClient::Client] client
48
+ # @param [Integer] size
49
+ # @param [Integer] start
50
+ # @param [Boolean] all
51
+ # @yieldparam [CoachClient::User] user the user
52
+ # @yieldreturn [Boolean] whether the user should be added to the list
53
+ # @return [Array<CoachClient::User>] the list of users
54
+ def self.list(client, size: 20, start: 0, all: false)
55
+ userlist = []
56
+ if all
57
+ total = self.total(client)
58
+ start = 0
59
+ size = LIST_ALL_SIZE
60
+ end
61
+ loop do
62
+ response = CoachClient::Request.get(client.url + path,
63
+ params: { start: start, size: size })
64
+ response.to_h[:users].each do |u|
65
+ user = self.new(client, u[:username])
66
+ userlist << user if !block_given? || yield(user)
67
+ end
68
+ break unless all
69
+ start += size
70
+ break if start >= total
71
+ end
72
+ userlist
73
+ end
74
+
75
+ # Creates a new user.
76
+ #
77
+ # @param [CoachClient::Client] client
78
+ # @param [String] username
79
+ # @param [Hash] info additional user informations
80
+ # @option info [String] :password
81
+ # @option info [String] :realname
82
+ # @option info [String] :email
83
+ # @option info [Integer] :publicvisible
84
+ # @return [CoachClient::User]
85
+ def initialize(client, username, info = {})
86
+ super(client)
87
+ @username = username
88
+ @password = info[:password]
89
+ @realname = info[:realname]
90
+ @email = info[:email]
91
+ @publicvisible = info[:publicvisible]
92
+ end
93
+
94
+ # Updates the user with the data from the CyberCoach service.
95
+ #
96
+ # @raise [CoachClient::NotFound] if the user does not exist
97
+ # @return [CoachClient::User] the updated user
98
+ def update
99
+ raise CoachClient::NotFound, 'User not found' unless exist?
100
+ response = if authenticated?
101
+ CoachClient::Request.get(url, username: @username,
102
+ password: @password)
103
+ else
104
+ CoachClient::Request.get(url)
105
+ end
106
+ response = response.to_h
107
+ @realname = response[:realname]
108
+ @email = response[:email]
109
+ @publicvisible = response[:publicvisible]
110
+ @datecreated = response[:datecreated]
111
+ @partnerships = []
112
+ unless response[:partnerships].nil?
113
+ response[:partnerships].each do |p|
114
+ users = CoachClient::Partnership.extract_users_from_uri(p[:uri])
115
+ users.reject! { |username| username == @username }
116
+ @partnerships << CoachClient::Partnership.new(client, self, users.first)
117
+ end
118
+ end
119
+ @subscriptions = []
120
+ unless response[:subscriptions].nil?
121
+ response[:subscriptions].each do |s|
122
+ sport = s[:uri].match(/\/(\w+)\/\z/).captures.first
123
+ @subscriptions << CoachClient::UserSubscription.new(client, self, sport)
124
+ end
125
+ end
126
+ self
127
+ end
128
+
129
+ # Saves the user to the CyberCoach service.
130
+ #
131
+ # The user is created if it does not exist on the CyberCoach service,
132
+ # otherwise it tries to overwrite it.
133
+ #
134
+ # @raise [CoachClient::Unauthorized] if the user is not authorized
135
+ # @raise [CoachClient::IncompleteInformation] if not all needed information
136
+ # is given
137
+ # @raise [CoachClient::NotSaved] if the user could not be saved
138
+ # @return [CoachClient::User] the saved user
139
+ def save
140
+ vals = self.to_h
141
+ vals.delete(:username)
142
+ vals.delete_if { |_k, v| v.nil? || v.to_s.empty? }
143
+ vals[:password] = vals.delete(:newpassword) if vals[:newpassword]
144
+ payload = Gyoku.xml(user: vals)
145
+ response = if exist?
146
+ unless authenticated?
147
+ raise CoachClient::Unauthorized.new(self), 'Unauthorized'
148
+ end
149
+ CoachClient::Request.put(url, username: @username,
150
+ password: @password,
151
+ payload: payload,
152
+ content_type: :xml)
153
+ else
154
+ begin
155
+ CoachClient::Request.put(url, payload: payload,
156
+ content_type: :xml)
157
+ rescue RestClient::Conflict
158
+ raise CoachClient::IncompleteInformation.new(self),
159
+ 'Incomplete user information'
160
+ end
161
+ end
162
+ unless response.code == 200 || response.code == 201
163
+ raise CoachClient::NotSaved.new(self), 'Could not save user'
164
+ end
165
+ @password = vals[:password]
166
+ @newpassword = nil
167
+ self
168
+ end
169
+
170
+ # Deletes the user on the CyberCoach service.
171
+ #
172
+ # @raise [CoachClient::NotFound] if the user does not exist
173
+ # @raise [CoachClient::Unauthorized] if the user is not authorized
174
+ # @return [true]
175
+ def delete
176
+ raise CoachClient::NotFound unless exist?
177
+ unless authenticated?
178
+ raise CoachClient::Unauthorized.new(self), 'Unauthorized'
179
+ end
180
+ CoachClient::Request.delete(url, username: @username, password: @password)
181
+ true
182
+ end
183
+
184
+ # Returns whether the user is authenticated.
185
+ #
186
+ # @return [Boolean]
187
+ def authenticated?
188
+ false if @password.nil?
189
+ @client.authenticated?(@username, @password)
190
+ end
191
+
192
+ # Returns the URL of the user.
193
+ #
194
+ # @return [String] the url of the user
195
+ def url
196
+ @client.url + self.class.path + @username
197
+ end
198
+
199
+ # Returns the string representation of the user.
200
+ #
201
+ # @return [String]
202
+ def to_s
203
+ @username.to_s
204
+ end
205
+ end
206
+ end
207
+
@@ -0,0 +1,79 @@
1
+ module CoachClient
2
+ # A user subscription resource of the CyberCoach service.
3
+ class UserSubscription < CoachClient::Subscription
4
+ # @return [Integer]
5
+ attr_reader :id, :datesubscribed
6
+
7
+ # @return [CoachClient::User]
8
+ attr_accessor :user
9
+
10
+ # Returns the relative path to the user subscription resource.
11
+ #
12
+ # @return [String] the relative path
13
+ def self.path
14
+ 'users/'
15
+ end
16
+
17
+ # Creates a new user subscription.
18
+ #
19
+ # @param [CoachClient::Client] client
20
+ # @param [String, CoachClient::User] user
21
+ # @param [String, Symbol, CoachClient::Sport] sport
22
+ # @param [Integer] publicvisible
23
+ # @return [CoachClient::UserSubscription]
24
+ def initialize(client, user, sport, publicvisible: nil)
25
+ super(client, sport, publicvisible: publicvisible)
26
+ @user = if user.is_a?(CoachClient::User)
27
+ user
28
+ else
29
+ CoachClient::User.new(client, user)
30
+ end
31
+ end
32
+
33
+ # Updates the user subscription with the data from the CyberCoach service.
34
+ #
35
+ # @raise [CoachClient::NotFound] if the user subscription does not exist
36
+ # @return [CoachClient::UserSubscription] the updated user subscription
37
+ def update
38
+ super(@user)
39
+ end
40
+
41
+ # Saves the user subscription to the CyberCoach service.
42
+ #
43
+ # The user subscription is created if it does not exist on the CyberCoach
44
+ # service, otherwise it tries to overwrite it.
45
+ #
46
+ # @raise [CoachClient::Unauthorized] if not authorized
47
+ # @raise [CoachClient::IncompleteInformation] if not all needed information
48
+ # is given
49
+ # @raise [CoachClient::NotSaved] if the user subscription could not be saved
50
+ # @return [CoachClient::UserSubscription] the saved user subscription
51
+ def save
52
+ super(@user)
53
+ end
54
+
55
+ # Deletes the user subscription on the CyberCoach service.
56
+ #
57
+ # @raise [CoachClient::NotFound] if the user subscription does not exist
58
+ # @raise [CoachClient::Unauthorized] if not authorized
59
+ # @return [true]
60
+ def delete
61
+ super(@user)
62
+ end
63
+
64
+ # Returns the URL of the user subscription.
65
+ #
66
+ # @return [String] the url of the user subscription
67
+ def url
68
+ "#{@user.url}/#{@sport}"
69
+ end
70
+
71
+ # Returns the string representation of the user subscription.
72
+ #
73
+ # @return [String]
74
+ def to_s
75
+ "#{@user.username}/#{@sport}"
76
+ end
77
+ end
78
+ end
79
+
@@ -0,0 +1,5 @@
1
+ module CoachClient
2
+ # The version of the gem
3
+ VERSION = '0.1.1'
4
+ end
5
+
@@ -0,0 +1,21 @@
1
+ require 'gyoku'
2
+ require 'rest-client'
3
+
4
+ require 'coach_client/client'
5
+ require 'coach_client/exceptions.rb'
6
+ require 'coach_client/resource'
7
+ require 'coach_client/entry'
8
+ require 'coach_client/partnership'
9
+ require 'coach_client/request'
10
+ require 'coach_client/response'
11
+ require 'coach_client/sport'
12
+ require "coach_client/subscription"
13
+ require 'coach_client/partnership_subscription'
14
+ require 'coach_client/user_subscription'
15
+ require 'coach_client/user'
16
+ require 'coach_client/version'
17
+
18
+ # A wrapper around the CyberCoach service of the University of Fribourg
19
+ module CoachClient
20
+ end
21
+
metadata ADDED
@@ -0,0 +1,186 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: coach_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Jungo
8
+ - Amanda Karavolia
9
+ - Andrea Liechti
10
+ - Jocelyn Thode
11
+ - Simon Brulhart
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+ date: 2015-12-10 00:00:00.000000000 Z
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: bundler
19
+ requirement: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - "~>"
22
+ - !ruby/object:Gem::Version
23
+ version: '1.10'
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - "~>"
29
+ - !ruby/object:Gem::Version
30
+ version: '1.10'
31
+ - !ruby/object:Gem::Dependency
32
+ name: pry
33
+ requirement: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ - !ruby/object:Gem::Dependency
46
+ name: rake
47
+ requirement: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - "~>"
50
+ - !ruby/object:Gem::Version
51
+ version: '10.0'
52
+ type: :development
53
+ prerelease: false
54
+ version_requirements: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - "~>"
57
+ - !ruby/object:Gem::Version
58
+ version: '10.0'
59
+ - !ruby/object:Gem::Dependency
60
+ name: rspec
61
+ requirement: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - "~>"
64
+ - !ruby/object:Gem::Version
65
+ version: 3.4.0
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - "~>"
71
+ - !ruby/object:Gem::Version
72
+ version: 3.4.0
73
+ - !ruby/object:Gem::Dependency
74
+ name: webmock
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: 1.22.3
80
+ type: :development
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - "~>"
85
+ - !ruby/object:Gem::Version
86
+ version: 1.22.3
87
+ - !ruby/object:Gem::Dependency
88
+ name: vcr
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: 2.9.3
94
+ type: :development
95
+ prerelease: false
96
+ version_requirements: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - "~>"
99
+ - !ruby/object:Gem::Version
100
+ version: 2.9.3
101
+ - !ruby/object:Gem::Dependency
102
+ name: gyoku
103
+ requirement: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - "~>"
106
+ - !ruby/object:Gem::Version
107
+ version: 1.3.1
108
+ type: :runtime
109
+ prerelease: false
110
+ version_requirements: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - "~>"
113
+ - !ruby/object:Gem::Version
114
+ version: 1.3.1
115
+ - !ruby/object:Gem::Dependency
116
+ name: rest-client
117
+ requirement: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - "~>"
120
+ - !ruby/object:Gem::Version
121
+ version: '1.8'
122
+ type: :runtime
123
+ prerelease: false
124
+ version_requirements: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - "~>"
127
+ - !ruby/object:Gem::Version
128
+ version: '1.8'
129
+ description:
130
+ email:
131
+ - michael.jungo@unifr.ch
132
+ executables:
133
+ - console
134
+ - setup
135
+ extensions: []
136
+ extra_rdoc_files: []
137
+ files:
138
+ - ".gitignore"
139
+ - ".rspec"
140
+ - ".travis.yml"
141
+ - Gemfile
142
+ - LICENSE.txt
143
+ - README.md
144
+ - Rakefile
145
+ - bin/console
146
+ - bin/setup
147
+ - coach_client.gemspec
148
+ - lib/coach_client.rb
149
+ - lib/coach_client/client.rb
150
+ - lib/coach_client/entry.rb
151
+ - lib/coach_client/exceptions.rb
152
+ - lib/coach_client/partnership.rb
153
+ - lib/coach_client/partnership_subscription.rb
154
+ - lib/coach_client/request.rb
155
+ - lib/coach_client/resource.rb
156
+ - lib/coach_client/response.rb
157
+ - lib/coach_client/sport.rb
158
+ - lib/coach_client/subscription.rb
159
+ - lib/coach_client/user.rb
160
+ - lib/coach_client/user_subscription.rb
161
+ - lib/coach_client/version.rb
162
+ homepage: https://github.com/jungomi/coach_client
163
+ licenses:
164
+ - MIT
165
+ metadata: {}
166
+ post_install_message:
167
+ rdoc_options: []
168
+ require_paths:
169
+ - lib
170
+ required_ruby_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: 2.1.0
175
+ required_rubygems_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ requirements: []
181
+ rubyforge_project:
182
+ rubygems_version: 2.4.8
183
+ signing_key:
184
+ specification_version: 4
185
+ summary: A wrapper around the CyberCoach API of unifr
186
+ test_files: []