coach_assist 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/coach/client.rb +58 -0
- data/lib/coach/entity.rb +29 -21
- data/lib/coach/finders/finder.rb +11 -0
- data/lib/coach/finders/partnership_finder.rb +31 -0
- data/lib/coach/finders/subscription_finder.rb +27 -0
- data/lib/coach/finders/user_finder.rb +49 -0
- data/lib/coach/partnership.rb +13 -2
- data/lib/coach/subscription.rb +5 -1
- data/lib/coach/user.rb +31 -48
- data/lib/coach_assist.rb +11 -7
- data/lib/coach_assist/version.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 278bc2ac79ab1f23248370336dc8263ad042dc5e
|
4
|
+
data.tar.gz: 9ce647e23df24610214a04f09612ce5e7175a550
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50c6d6f01ad4ec1f2b1930665dba59c5245f75790596e5c9724e87d4aa544fb1efb405a2c8feb10a25a82b61170642f1c63673160f826283650bcb91bc1474f3
|
7
|
+
data.tar.gz: edeb36fe187eb95091c29e4216db029bcf3285d2ee73cb52974a2e6ab21b8eab63b03ef7d68adfbd6acac9602d5c91ac3c5ef3af7a870ed5c02555e761b8518d
|
data/lib/coach/client.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
module Coach
|
2
|
+
class Client
|
3
|
+
include HTTParty
|
4
|
+
|
5
|
+
debug_output $stdout
|
6
|
+
format :json
|
7
|
+
|
8
|
+
headers 'Accept' => 'application/json'
|
9
|
+
base_uri 'http://diufvm31.unifr.ch:8090/CyberCoachServer/resources'
|
10
|
+
|
11
|
+
def get(path, options={})
|
12
|
+
Client.get(path, options.merge(basic_auth: @auth))
|
13
|
+
end
|
14
|
+
|
15
|
+
def post(path, options={})
|
16
|
+
Client.post(path, options.merge(basic_auth: @auth))
|
17
|
+
end
|
18
|
+
|
19
|
+
def put(path, options={})
|
20
|
+
Client.put(path, options.merge(basic_auth: @auth))
|
21
|
+
end
|
22
|
+
|
23
|
+
def delete(path, options={})
|
24
|
+
Client.delete(path, options.merge(basic_auth: @auth))
|
25
|
+
end
|
26
|
+
|
27
|
+
def authenticate(username, password)
|
28
|
+
@auth = { username: username, password: password }
|
29
|
+
end
|
30
|
+
|
31
|
+
def authenticated(username, password, &block)
|
32
|
+
authenticate(username, password)
|
33
|
+
block.call
|
34
|
+
@auth = nil
|
35
|
+
end
|
36
|
+
|
37
|
+
def has_auth?
|
38
|
+
!!@auth
|
39
|
+
end
|
40
|
+
|
41
|
+
def assert_authenticated!
|
42
|
+
raise 'Method must be authenticated first!' unless has_auth?
|
43
|
+
end
|
44
|
+
|
45
|
+
def users
|
46
|
+
@user_finder || (@user_finder = Finders::UserFinder.new(self))
|
47
|
+
end
|
48
|
+
|
49
|
+
def partnerships
|
50
|
+
@ps_finder || (@ps_finder = Finders::PartnershipFinder.new(self))
|
51
|
+
end
|
52
|
+
|
53
|
+
def subscriptions
|
54
|
+
@sub_finder || (@sub_finder = Finders::SubscriptionFinder.new(self))
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
data/lib/coach/entity.rb
CHANGED
@@ -1,40 +1,48 @@
|
|
1
1
|
module Coach
|
2
2
|
class Entity < Hashie::Trash
|
3
3
|
include Hashie::Extensions::IndifferentAccess
|
4
|
-
|
4
|
+
|
5
|
+
attr_accessor :client
|
5
6
|
|
6
7
|
property :uri
|
7
8
|
property :id
|
8
9
|
property :created_at, from: :datecreated
|
9
|
-
property :visibility, from: :publicvisible
|
10
|
-
|
11
|
-
headers 'Accept' => 'application/json'
|
12
|
-
base_uri 'http://diufvm31.unifr.ch:8090/CyberCoachServer/resources'
|
13
|
-
|
14
|
-
class << self
|
15
|
-
def authenticated(username, password, &block)
|
16
|
-
@auth = { username: username, password: password }
|
17
|
-
res = instance_eval &block
|
18
|
-
@auth = nil
|
19
|
-
res
|
20
|
-
end
|
21
|
-
end
|
10
|
+
property :visibility, from: :publicvisible, default: 2
|
22
11
|
|
23
12
|
# Fetch an entity based on its uri. Each entity containing a valid uri will be retrievable through this method
|
24
13
|
def fetch
|
25
|
-
|
26
|
-
raise 'Entity has not been loaded yet and does not have "uri" property' if self.uri.blank?
|
14
|
+
assert_has_uri!
|
27
15
|
|
28
|
-
response =
|
16
|
+
response = client.get clean_uri
|
29
17
|
update_attributes! JSON.parse(response.body)
|
30
18
|
|
31
|
-
@fetched = true
|
32
19
|
self
|
33
20
|
end
|
34
21
|
|
35
|
-
|
36
|
-
|
37
|
-
|
22
|
+
def destroy
|
23
|
+
assert_has_uri!
|
24
|
+
client.delete clean_uri
|
25
|
+
end
|
26
|
+
|
27
|
+
def update(attributes={})
|
28
|
+
assert_has_uri!
|
29
|
+
response = client.put clean_uri, body: attributes
|
30
|
+
update_attributes! JSON.parse(response.body) if response.code == 200
|
38
31
|
end
|
32
|
+
|
33
|
+
def set_client(client)
|
34
|
+
self.client = client
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
# Remove overlapping url parts
|
40
|
+
def clean_uri
|
41
|
+
self.uri.gsub('/CyberCoachServer/resources', '')
|
42
|
+
end
|
43
|
+
|
44
|
+
def assert_has_uri!
|
45
|
+
raise 'Entity has no URI associated with it' unless self.uri
|
46
|
+
end
|
39
47
|
end
|
40
48
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Coach
|
2
|
+
module Finders
|
3
|
+
class PartnershipFinder < Finder
|
4
|
+
def all
|
5
|
+
eager, search = options.values_at(:eager, :search)
|
6
|
+
response = client.get '/partnerships', query: { start: 0, size: 10000, searchCriteria: search }
|
7
|
+
JSON.parse(response.body)['partnerships'].map { |ps| eager ? build(ps).fetch : build(ps) }
|
8
|
+
end
|
9
|
+
|
10
|
+
def find(username1, username2)
|
11
|
+
response = client.get "/partnerships/#{username1};#{username2}"
|
12
|
+
response.code == 200 ? build(JSON.parse(response.body)) : nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def exists?(username1, username2)
|
16
|
+
!!find(username1, username2)
|
17
|
+
end
|
18
|
+
|
19
|
+
def create(username1, username2, attributes={})
|
20
|
+
response = client.put "/partnerships/#{username1};#{username2}", body: attributes
|
21
|
+
response.code == 201 ? build(JSON.parse(response.body)) : nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def build(attributes={})
|
25
|
+
u = Coach::Partnership.new(attributes)
|
26
|
+
u.set_client(client)
|
27
|
+
u
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Coach
|
2
|
+
module Finders
|
3
|
+
class SubscriptionFinder < Finder
|
4
|
+
|
5
|
+
def find(username, sport)
|
6
|
+
response = client.get "/users/#{username}/#{sport}"
|
7
|
+
response.code == 200 ? build(JSON.parse(response.body)) : nil
|
8
|
+
end
|
9
|
+
|
10
|
+
def exists?(username1, username2)
|
11
|
+
!!find(username1, username2)
|
12
|
+
end
|
13
|
+
|
14
|
+
def create(username, sport, attributes={})
|
15
|
+
response = client.put "/users/#{username}/#{sport}", body: attributes
|
16
|
+
response.code == 201 ? build(JSON.parse(response.body)) : nil
|
17
|
+
end
|
18
|
+
|
19
|
+
def build(attributes={})
|
20
|
+
s = Coach::Subscription.new(attributes)
|
21
|
+
s.set_client(client)
|
22
|
+
s
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Coach
|
2
|
+
module Finders
|
3
|
+
class UserFinder < Finder
|
4
|
+
|
5
|
+
# Find all users on server. Pass :eager option to return results eagerly (slow)
|
6
|
+
# Pass :search option to only include users matching the search query
|
7
|
+
def all(options={})
|
8
|
+
eager, search = options.values_at(:eager, :search)
|
9
|
+
response = client.get '/users', query: { start: 0, size: 10000, searchCriteria: search }
|
10
|
+
JSON.parse(response.body)['users'].map { |u| eager ? build(u).fetch : build(u) }
|
11
|
+
end
|
12
|
+
|
13
|
+
# Find user by username
|
14
|
+
def find(username)
|
15
|
+
response = client.get "/users/#{username}"
|
16
|
+
response.code == 200 ? build(JSON.parse(response.body)) : nil
|
17
|
+
end
|
18
|
+
|
19
|
+
# Check whether user exists
|
20
|
+
def exists?(username)
|
21
|
+
!!find(username)
|
22
|
+
end
|
23
|
+
|
24
|
+
def authenticated?(username, password)
|
25
|
+
authenticated(username, password) do
|
26
|
+
response = client.get '/authenticateduser'
|
27
|
+
response.code == 200 ? build(JSON.parse(response.body)) : nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Create new user
|
32
|
+
def create(username, attributes={})
|
33
|
+
response = client.put "/users/#{username}", body: attributes
|
34
|
+
[200, 201].include?(response.code) ? build(JSON.parse(response.body)) : nil
|
35
|
+
end
|
36
|
+
|
37
|
+
def find_or_create(username, attributes={})
|
38
|
+
u = find(username)
|
39
|
+
u || create(username, attributes)
|
40
|
+
end
|
41
|
+
|
42
|
+
def build(attributes={})
|
43
|
+
u = Coach::User.new(attributes)
|
44
|
+
u.set_client(client)
|
45
|
+
u
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/coach/partnership.rb
CHANGED
@@ -4,20 +4,31 @@ module Coach
|
|
4
4
|
property :confirmed_by_user2, from: :userconfirmed2
|
5
5
|
property :user1
|
6
6
|
property :user2
|
7
|
+
property :subscriptions
|
7
8
|
|
8
9
|
alias_method :old_user1, :user1
|
9
10
|
alias_method :old_user2, :user2
|
11
|
+
alias_method :subscriptions_orig, :subscriptions
|
10
12
|
|
11
13
|
def user1
|
12
|
-
|
14
|
+
client.users.build(old_user1).fetch
|
13
15
|
end
|
14
16
|
|
15
17
|
def user2
|
16
|
-
|
18
|
+
client.users.build(old_user2).fetch
|
17
19
|
end
|
18
20
|
|
19
21
|
def users
|
20
22
|
[user1, user2]
|
21
23
|
end
|
24
|
+
|
25
|
+
def subscriptions
|
26
|
+
(subscriptions_orig || []).map { |s| client.subscriptions.build(s).fetch }
|
27
|
+
end
|
28
|
+
|
29
|
+
def confirm
|
30
|
+
client.assert_authenticated!
|
31
|
+
client.put clean_uri
|
32
|
+
end
|
22
33
|
end
|
23
34
|
end
|
data/lib/coach/subscription.rb
CHANGED
data/lib/coach/user.rb
CHANGED
@@ -5,63 +5,46 @@ module Coach
|
|
5
5
|
property :password
|
6
6
|
property :real_name, from: :realname
|
7
7
|
property :email
|
8
|
-
property :partnerships
|
9
|
-
property :subscriptions
|
8
|
+
property :partnerships, default: []
|
9
|
+
property :subscriptions, default: []
|
10
10
|
|
11
|
-
alias_method :
|
12
|
-
alias_method :
|
11
|
+
alias_method :partnerships_orig, :partnerships
|
12
|
+
alias_method :subscriptions_orig, :subscriptions
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
def all(options={})
|
18
|
-
eager, search = options.values_at(:eager, :search)
|
19
|
-
response = get '/users', query: { start: 0, size: 10000, searchCriteria: search }
|
20
|
-
JSON.parse(response.body)['users'].map { |u| eager ? Coach::User.new(u).fetch : Coach::User.new(u) }
|
21
|
-
end
|
22
|
-
|
23
|
-
# Find user by username
|
24
|
-
def find(username)
|
25
|
-
response = get "/users/#{username}", basic_auth: @auth
|
26
|
-
response.code == 200 ? Coach::User.new(JSON.parse(response.body)) : nil
|
27
|
-
end
|
28
|
-
|
29
|
-
# Check whether user exists
|
30
|
-
def exists?(username)
|
31
|
-
!!find(username)
|
32
|
-
end
|
33
|
-
|
34
|
-
def authenticated?(username, password)
|
35
|
-
authenticated(username, password) do
|
36
|
-
response = get '/authenticateduser', basic_auth: @auth
|
37
|
-
response.code == 200 ? Coach::User.new(JSON.parse(response.body)) : nil
|
38
|
-
end
|
39
|
-
end
|
14
|
+
def partnerships
|
15
|
+
(partnerships_orig || []).map { |p| client.partnerships.build(p).fetch }
|
16
|
+
end
|
40
17
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
response.code == 201 ? Coach::User.new(JSON.parse(response.body)) : nil
|
45
|
-
end
|
18
|
+
def subscriptions
|
19
|
+
(subscriptions_orig || []).map { |p| client.subscriptions.build(p).fetch }
|
20
|
+
end
|
46
21
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
response.code == 200 ? Coach::User.new(JSON.parse(response.body)) : nil
|
51
|
-
end
|
22
|
+
def pending_partnerships
|
23
|
+
partnerships.select { |ps| ps.user2.username == self.username && !ps.confirmed_by_user2}
|
24
|
+
end
|
52
25
|
|
53
|
-
|
54
|
-
|
55
|
-
HTTParty::delete "/users/#{username}", basic_auth: @auth
|
56
|
-
end
|
26
|
+
def confirm_partnership(ps)
|
27
|
+
ps.confirm
|
57
28
|
end
|
58
29
|
|
59
|
-
def
|
60
|
-
|
30
|
+
def subscribe_to(sport)
|
31
|
+
raise 'Can only subscribe to :running or :cycling' unless Coach::Subscription.types.include? sport.to_sym
|
32
|
+
client.subscriptions.create(self.username, sport, publicvisible: 2)
|
61
33
|
end
|
62
34
|
|
63
|
-
def
|
64
|
-
|
35
|
+
def destroy
|
36
|
+
destroy_partnerships
|
37
|
+
destroy_subscriptions
|
38
|
+
super
|
65
39
|
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def destroy_partnerships
|
43
|
+
partnerships.each(&:destroy)
|
44
|
+
end
|
45
|
+
|
46
|
+
def destroy_subscriptions
|
47
|
+
subscriptions.each(&:destroy)
|
48
|
+
end
|
66
49
|
end
|
67
50
|
end
|
data/lib/coach_assist.rb
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
require 'httparty'
|
2
2
|
require 'hashie'
|
3
|
-
require 'active_support/all'
|
4
|
-
require "coach_assist/version"
|
5
|
-
require "coach/entity"
|
6
|
-
require "coach/user"
|
7
|
-
require "coach/subscription"
|
8
|
-
require "coach/partnership"
|
9
|
-
|
10
3
|
|
4
|
+
require 'active_support/all'
|
5
|
+
require 'coach_assist/version'
|
6
|
+
require 'coach/entity'
|
7
|
+
require 'coach/user'
|
8
|
+
require 'coach/subscription'
|
9
|
+
require 'coach/partnership'
|
10
|
+
require 'coach/client'
|
11
|
+
require 'coach/finders/finder'
|
12
|
+
require 'coach/finders/user_finder'
|
13
|
+
require 'coach/finders/subscription_finder'
|
14
|
+
require 'coach/finders/partnership_finder'
|
data/lib/coach_assist/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coach_assist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Urs Gerber
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -111,7 +111,12 @@ files:
|
|
111
111
|
- bin/console
|
112
112
|
- bin/setup
|
113
113
|
- coach_assist.gemspec
|
114
|
+
- lib/coach/client.rb
|
114
115
|
- lib/coach/entity.rb
|
116
|
+
- lib/coach/finders/finder.rb
|
117
|
+
- lib/coach/finders/partnership_finder.rb
|
118
|
+
- lib/coach/finders/subscription_finder.rb
|
119
|
+
- lib/coach/finders/user_finder.rb
|
115
120
|
- lib/coach/partnership.rb
|
116
121
|
- lib/coach/subscription.rb
|
117
122
|
- lib/coach/user.rb
|