coach4rb 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +16 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +31 -0
  6. data/Rakefile +8 -0
  7. data/coach4rb.gemspec +29 -0
  8. data/lib/coach4rb.rb +64 -0
  9. data/lib/coach4rb/builder.rb +142 -0
  10. data/lib/coach4rb/client.rb +116 -0
  11. data/lib/coach4rb/coach.rb +836 -0
  12. data/lib/coach4rb/json_response_parser.rb +11 -0
  13. data/lib/coach4rb/mixin/as_hash.rb +34 -0
  14. data/lib/coach4rb/mixin/auto_constructor.rb +71 -0
  15. data/lib/coach4rb/mixin/basic_auth.rb +23 -0
  16. data/lib/coach4rb/mixin/iterable.rb +40 -0
  17. data/lib/coach4rb/mixin/track_reader.rb +24 -0
  18. data/lib/coach4rb/mixin/track_writer.rb +19 -0
  19. data/lib/coach4rb/privacy.rb +11 -0
  20. data/lib/coach4rb/proxy.rb +143 -0
  21. data/lib/coach4rb/resource/entity.rb +47 -0
  22. data/lib/coach4rb/resource/entry.rb +126 -0
  23. data/lib/coach4rb/resource/page.rb +74 -0
  24. data/lib/coach4rb/resource/partnership.rb +37 -0
  25. data/lib/coach4rb/resource/sport.rb +19 -0
  26. data/lib/coach4rb/resource/subscription.rb +47 -0
  27. data/lib/coach4rb/resource/user.rb +49 -0
  28. data/lib/coach4rb/response_parser.rb +11 -0
  29. data/lib/coach4rb/version.rb +3 -0
  30. data/test/test_access_proxy.rb +58 -0
  31. data/test/test_client.rb +41 -0
  32. data/test/test_coach_client.rb +21 -0
  33. data/test/test_coach_entry.rb +141 -0
  34. data/test/test_coach_partnership.rb +115 -0
  35. data/test/test_coach_subscription.rb +110 -0
  36. data/test/test_coach_user.rb +191 -0
  37. data/test/test_entity.rb +63 -0
  38. data/test/test_entry_resource.rb +36 -0
  39. data/test/test_helper.rb +3 -0
  40. data/test/test_page.rb +82 -0
  41. data/test/test_partnership_resource.rb +69 -0
  42. data/test/test_sport_resource.rb +33 -0
  43. data/test/test_subscription_resource.rb +74 -0
  44. data/test/test_user_resource.rb +104 -0
  45. data/tools/.keep +0 -0
  46. metadata +190 -0
@@ -0,0 +1,74 @@
1
+ module Coach4rb
2
+
3
+ module Resource
4
+
5
+ class Page
6
+
7
+ include Mixin::AutoConstructor
8
+ include Mixin::Iterable
9
+ include Mixin::AsHash
10
+ include Enumerable
11
+
12
+ attr_accessor :start, :end, :available, :links, :type, :uri
13
+
14
+ def self.from_coach(a_hash, entity_class=Entity)
15
+ new_hash = a_hash.dup
16
+ type_plural = pluralize(new_hash[:type]) if new_hash[:type]
17
+ type_plural ||= false
18
+ new_hash[:links] ||= []
19
+ new_hash[:type] = type_plural
20
+ new_hash[:entity_class] ||= entity_class
21
+ self.new new_hash
22
+ end
23
+
24
+
25
+ def entities
26
+ if @type && @entities.nil?
27
+ hashes = instance_variable_get("@#{@type}")
28
+ @entities = hashes.map { |a_hash| @entity_class.from_coach a_hash }
29
+ else
30
+ @entities
31
+ end
32
+ end
33
+
34
+ alias_method :items, :entities
35
+
36
+ def size
37
+ entities.size
38
+ end
39
+
40
+
41
+ def [](key)
42
+ case key
43
+ when Integer
44
+ entities[key]
45
+ when Symbol
46
+ super(key)
47
+ else
48
+ raise 'Error: param not supported!'
49
+ end
50
+ end
51
+
52
+
53
+ def each(&block)
54
+ entities.each do |item|
55
+ block.call item
56
+ end
57
+ end
58
+
59
+ def to_a
60
+ entities
61
+ end
62
+
63
+ private
64
+
65
+ def self.pluralize(type)
66
+ type[-1] == 's' ? type : type + 's' # pluralize
67
+ end
68
+
69
+
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,37 @@
1
+ module Coach4rb
2
+
3
+ module Resource
4
+
5
+ class Partnership < Entity
6
+
7
+ attr_accessor :uri, :id, :userconfirmed1, :userconfirmed2,
8
+ :datecreated, :publicvisible, :user1, :user2,
9
+ :links, :subscriptions
10
+
11
+ alias_method :created, :datecreated
12
+ alias_method :visible, :publicvisible
13
+ alias_method :first_user, :user1
14
+ alias_method :second_user, :user2
15
+ alias_method :first_user_confirmed, :userconfirmed1
16
+ alias_method :second_user_confirmed, :userconfirmed2
17
+
18
+ def self.from_coach(a_hash)
19
+ new_hash = a_hash.dup
20
+ new_hash[:datecreated] = Time.at(new_hash[:datecreated]/1000).to_datetime rescue nil
21
+ new_hash[:subscriptions] ||= []
22
+ new_hash[:subscriptions] = new_hash[:subscriptions].map {|a_hash| Resource::Subscription.from_coach a_hash }
23
+ new_hash[:user1] = Resource::User.from_coach(new_hash[:user1]) rescue nil
24
+ new_hash[:user2] = Resource::User.from_coach(new_hash[:user2]) rescue nil
25
+ super new_hash
26
+ end
27
+
28
+
29
+ def entity_path
30
+ "/partnerships/#{first_user.username};#{second_user.username}"
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,19 @@
1
+ module Coach4rb
2
+
3
+ module Resource
4
+
5
+ class Sport < Entity
6
+
7
+ attr_accessor :id, :name, :description
8
+
9
+
10
+ def self.from_coach(a_hash)
11
+ new_hash = a_hash.dup
12
+ self.new(new_hash)
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,47 @@
1
+ module Coach4rb
2
+
3
+ module Resource
4
+
5
+ class Subscription < Entity
6
+ attr_accessor :id, :uri, :datesubscribed, :publicvisble,
7
+ :partnership, :sport, :entries
8
+
9
+ alias_method :created, :datesubscribed
10
+ alias_method :visible, :publicvisble
11
+
12
+
13
+ def self.from_coach(a_hash)
14
+ new_hash = a_hash.dup
15
+ new_hash[:datesubscribed] = Time.at(a_hash[:datesubscribed]/1000).to_datetime rescue nil
16
+ new_hash[:partnership] = Resource::Partnership.from_coach(new_hash[:partnership]) rescue nil
17
+ new_hash[:user] = Resource::User.from_coach(new_hash[:user]) rescue nil
18
+ new_hash[:entries] = new_hash[:entries].map {|a_hash| Resource::Entry.from_coach a_hash} rescue []
19
+ new_hash[:sport] = Resource::Sport.from_coach(new_hash[:sport]) rescue nil
20
+ super new_hash
21
+ end
22
+
23
+
24
+ def sport?(sport)
25
+ raise 'Error: Invalid sport param!' if sport.nil?
26
+ *, subscription_sport = uri.split('/') # get last part of the uri
27
+ subscription_sport = subscription_sport.downcase.to_sym
28
+ sport = sport.to_s.downcase.to_sym
29
+ sport == subscription_sport
30
+ end
31
+
32
+
33
+ def entity_path
34
+ if @user
35
+ "/users/#{@user.username}/#{@sport}"
36
+ elsif @partnership
37
+ "/partnerships/#{@partnership.first_user.username};#{@partnership.second_user.username}/#{@sport}"
38
+ else
39
+ raise 'Error: cannot create url!'
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,49 @@
1
+ module Coach4rb
2
+
3
+ module Resource
4
+
5
+ class User < Entity
6
+
7
+ attr_accessor :username, :password, :email, :realname,
8
+ :datecreated, :publicvisible, :partnerships,
9
+ :subscriptions
10
+
11
+ alias_method :real_name, :realname
12
+ alias_method :created, :datecreated
13
+ alias_method :public_visible, :publicvisible
14
+
15
+
16
+ def self.from_coach(a_hash)
17
+ new_hash = a_hash.dup
18
+ new_hash[:password] = nil # avoid having a star password
19
+ new_hash[:partnerships] ||= []
20
+ new_hash[:partnerships] = new_hash[:partnerships].map {|a_hash| Resource::Partnership.from_coach a_hash }
21
+ new_hash[:subscriptions] ||= []
22
+ new_hash[:subscriptions] = new_hash[:subscriptions].map {|a_hash| Resource::Subscription.from_coach a_hash }
23
+ new_hash[:datecreated] = Time.at(new_hash[:datecreated]/1000).to_datetime rescue nil
24
+ super(new_hash)
25
+ end
26
+
27
+
28
+ def entity_path
29
+ "/users/#{username}"
30
+ end
31
+
32
+
33
+ private
34
+
35
+ def to_flat_hash
36
+ a_hash = {
37
+ username: username,
38
+ password: password,
39
+ email: email,
40
+ publicvisible: public_visible,
41
+ realname: real_name
42
+ }
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,11 @@
1
+ module Coach4rb
2
+
3
+ class ResponseParser
4
+
5
+ def parse
6
+ raise 'Not implemented!'
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,3 @@
1
+ module Coach4rb
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,58 @@
1
+ require 'test/test_helper'
2
+
3
+ class TestAccessProxy < MiniTest::Test
4
+ include Coach4rb::Mixin::BasicAuth
5
+
6
+ def setup
7
+ @coach = Coach4rb.configure(
8
+ scheme: 'http',
9
+ host: 'diufvm31.unifr.ch',
10
+ port: 8090,
11
+ path: '/CyberCoachServer/resources'
12
+ )
13
+ @proxy = Coach4rb::Proxy::Access.new'privatealex', 'scareface', @coach
14
+ @invalid_proxy = Coach4rb::Proxy::InvalidAccess.new @coach
15
+ end
16
+
17
+
18
+ def test_should_still_work_using_a_proxy
19
+ partnerships = @proxy.partnerships query: {start: 0, size: 10}
20
+ assert partnerships
21
+ assert_equal 10, partnerships.size
22
+ end
23
+
24
+ def test_access_on_private_resource_without_a_proxy
25
+ user = @coach.user 'privatealex', {authorization: basic_auth_encription('privatealex','test')}
26
+ assert user
27
+ assert user.username
28
+ end
29
+
30
+ def test_access_on_private_resource_without_a_proxy
31
+ users = @coach.users query: { start: 0, size: 2000 }, authorization: basic_auth_encryption('privatealex','test')
32
+ user = users.detect { |u| u.username == 'privatealex' }
33
+ assert user
34
+ assert user.username
35
+
36
+ end
37
+
38
+ def test_should_test_proxy_access
39
+ assert @proxy.valid?
40
+ assert @proxy.available?
41
+ user = @proxy.user 'privatealex'
42
+ assert user
43
+ assert user.real_name
44
+ assert user.email
45
+ assert user.username
46
+ assert_equal Coach4rb::Privacy::Private, user.public_visible
47
+ end
48
+
49
+
50
+
51
+ def test_should_test_invalid_proxy_access
52
+ assert @invalid_proxy.available?
53
+ refute @invalid_proxy.valid?
54
+ end
55
+
56
+
57
+
58
+ end
@@ -0,0 +1,41 @@
1
+ require 'test/test_helper'
2
+
3
+ class TestClient < MiniTest::Test
4
+
5
+ def setup
6
+ @client = Coach4rb::Client.new(
7
+ scheme: 'http',
8
+ host: 'diufvm31.unifr.ch',
9
+ port: 8090,
10
+ path: '/CyberCoachServer/resources'
11
+ )
12
+ end
13
+
14
+
15
+ def test_should_have_path
16
+ assert @client.path
17
+ assert_equal '/CyberCoachServer/resources', @client.path
18
+ end
19
+
20
+
21
+ def test_should_have_site
22
+ assert 'http://diufvm31.unifr.ch/CyberCoachServer/resources', @client.site
23
+ end
24
+
25
+
26
+ def test_should_have_port
27
+ assert 8090, @client.port
28
+ end
29
+
30
+
31
+ def test_should_have_host
32
+ assert 'diufvm31.unifr.ch', @client.host
33
+ end
34
+
35
+
36
+ def test_should_have_scheme
37
+ assert 'http', @client.scheme
38
+ end
39
+
40
+
41
+ end
@@ -0,0 +1,21 @@
1
+ require 'test/test_helper'
2
+
3
+ class CoachTestRetrieval < MiniTest::Test
4
+
5
+ def setup
6
+ @coach = Coach4rb.configure(
7
+ scheme: 'http',
8
+ host: 'diufvm31.unifr.ch',
9
+ port: 8090,
10
+ path: '/CyberCoachServer/resources',
11
+ debug: true
12
+ )
13
+ end
14
+
15
+
16
+ def test_if_cyber_coach_is_available
17
+ assert @coach.available?
18
+ end
19
+
20
+
21
+ end
@@ -0,0 +1,141 @@
1
+ require 'test/test_helper'
2
+
3
+ class TestCoachEntry < MiniTest::Test
4
+
5
+ def setup
6
+ @coach = Coach4rb.configure(
7
+ scheme: 'http',
8
+ host: 'diufvm31.unifr.ch',
9
+ port: 8090,
10
+ path: '/CyberCoachServer/resources',
11
+ debug: true
12
+ )
13
+ @proxy = Coach4rb::Proxy::Access.new 'wantsomemoney', 'test', @coach
14
+ @user = @coach.user 'wantsomemoney'
15
+ assert @proxy.valid?
16
+ end
17
+
18
+
19
+ def test_should_get_entry_by_its_uri
20
+ uri = '/CyberCoachServer/resources/users/wantsomemoney/Running/1132/'
21
+ entry = @coach.entry_by_uri uri
22
+ assert entry
23
+ end
24
+
25
+
26
+ def test_should_get_entry
27
+ entry = @coach.entry_by_uri '/CyberCoachServer/resources/users/wantsomemoney/Running/1138/'
28
+ entry = @coach.entry entry
29
+ assert entry
30
+ end
31
+
32
+
33
+ def test_should_get_an_entry_of_a_partnership
34
+ partnership = @coach.partnership 'arueedlinger', 'asarteam5'
35
+ assert partnership.subscriptions.size > 0
36
+ running = partnership.subscriptions.detect { |s| s.sport? :running }
37
+ assert running
38
+ subscription = @coach.subscription running
39
+ assert subscription
40
+ subscription.entries.each do |entry|
41
+ assert @coach.entry entry
42
+ end
43
+
44
+ end
45
+
46
+
47
+ def test_should_get_an_entry_of_a_user
48
+ user = @coach.user 'arueedlinger'
49
+ assert user.subscriptions.size > 0
50
+ running = user.subscriptions.detect { |s| s.sport? :running }
51
+ assert running
52
+ subscription = @coach.subscription running
53
+ assert subscription
54
+ subscription.entries.each do |entry|
55
+ assert @coach.entry entry
56
+ end
57
+
58
+ end
59
+
60
+
61
+ def test_should_create_an_entry_for_a_user
62
+ res = @proxy.create_entry(@user, :running) do |entry|
63
+ entry.comment = 'test'
64
+ entry.number_of_rounds = 10
65
+ entry.public_visible = Coach4rb::Privacy::Public
66
+ end
67
+ assert res
68
+ assert res.uri
69
+ end
70
+
71
+
72
+ def test_should_create_an_entry_using_a_uri
73
+ res = @proxy.create_entry(@user.uri, :running) do |entry|
74
+ entry.comment = 'test'
75
+ entry.number_of_rounds = 10
76
+ entry.public_visible = Coach4rb::Privacy::Public
77
+ end
78
+ assert res
79
+ assert res.uri
80
+ end
81
+
82
+
83
+ def test_should_create_an_entry_for_a_partnership
84
+ proxy = Coach4rb::Proxy::Access.new 'arueedlinger', 'test', @coach
85
+ partnership = @coach.partnership 'arueedlinger', 'asarteam5'
86
+ res = proxy.create_entry(partnership, :running) do |entry|
87
+ entry.comment = 'test'
88
+ entry.number_of_rounds = 10
89
+ entry.public_visible = Coach4rb::Privacy::Public
90
+ end
91
+ assert res
92
+ assert res.uri
93
+ end
94
+
95
+
96
+
97
+ def test_should_update_an_entry_using_a_uri
98
+ uri = '/CyberCoachServer/resources/users/wantsomemoney/Running/1138/'
99
+ res = @proxy.update_entry(uri) do |entry|
100
+ entry.comment = 'Test!!'
101
+ entry.entry_location = 'sajfsjdjasd'
102
+ entry.number_of_rounbds = 10000
103
+ entry.entry_date = DateTime.now
104
+ entry.track = 'jhasjdhjashjkdasjdjkasndj'
105
+ end
106
+ assert res
107
+ assert res.uri
108
+ end
109
+
110
+
111
+ def test_should_update_an_entry
112
+ entry = @proxy.entry_by_uri '/CyberCoachServer/resources/users/wantsomemoney/Running/1139/'
113
+ updated_entry = @proxy.update_entry(entry) do |entry|
114
+ entry.comment = 'Test!!'
115
+ entry.track = 'muhaa!!!!'
116
+ end
117
+ assert updated_entry
118
+ assert updated_entry.uri
119
+ assert_equal 'Test!!', updated_entry.comment
120
+ pp updated_entry.read_track
121
+ assert_equal 'muhaa!!!!', updated_entry.read_track
122
+ end
123
+
124
+
125
+ def test_should_create_and_delete_an_entry
126
+ entry = @proxy.create_entry(@user, :running) do |e|
127
+ e.comment = 'test'
128
+ e.number_of_rounds = 10
129
+ e.public_visible = Coach4rb::Privacy::Public
130
+ end
131
+
132
+ assert entry
133
+ assert @proxy.entry_by_uri entry.uri
134
+ assert @proxy.delete_entry(entry)
135
+ sleep 2
136
+ refute @proxy.entry_by_uri(entry.uri)
137
+
138
+ end
139
+
140
+
141
+ end