cybercoach 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -1
- data/lib/cybercoach.rb +1 -0
- data/lib/cybercoach/abstract_resource.rb +33 -11
- data/lib/cybercoach/entry.rb +4 -0
- data/lib/cybercoach/hash_initializable.rb +50 -0
- data/lib/cybercoach/pageable.rb +4 -3
- data/lib/cybercoach/partnership.rb +4 -0
- data/lib/cybercoach/post_createable.rb +6 -4
- data/lib/cybercoach/put_createable.rb +6 -4
- data/lib/cybercoach/resource.rb +19 -10
- data/lib/cybercoach/resource_page.rb +26 -14
- data/lib/cybercoach/sport.rb +4 -0
- data/lib/cybercoach/subscription.rb +4 -0
- data/lib/cybercoach/user.rb +9 -5
- data/lib/cybercoach/version.rb +1 -1
- data/spec/lib/cybercoach/entry_spec.rb +97 -22
- data/spec/lib/cybercoach/partnership_spec.rb +82 -14
- data/spec/lib/cybercoach/privacy_level_spec.rb +1 -1
- data/spec/lib/cybercoach/resource_page_spec.rb +41 -2
- data/spec/lib/cybercoach/settings_spec.rb +1 -1
- data/spec/lib/cybercoach/sport_spec.rb +34 -4
- data/spec/lib/cybercoach/subscription_spec.rb +97 -28
- data/spec/lib/cybercoach/user_helper.rb +0 -9
- data/spec/lib/cybercoach/user_spec.rb +87 -12
- data/spec/lib/integration_spec.rb +15 -15
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a673d760d6a53d29163f737519c2c1abd8d45301
|
4
|
+
data.tar.gz: efcda8eca1e90172b343bc89e8ba2704ef71af10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c648893b1b4f85f161fa54233a114d05ca20c81cbda73627e5539d112feaec2ed4fce19e00824b7c155542ae476b068810fb3516cfe60e3ad6430c8bf3dfed4a
|
7
|
+
data.tar.gz: f860054778668ae3f3f80658e11281175e56f622b18140e04f5d7cd7ece05f2250e1d20f2e1d0e4baab70a3c5f2a9c2d3536a980bebc7df376cc16f046a56e3e
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## v0.4.0
|
4
|
+
|
5
|
+
- resources can be initialized with a hash
|
6
|
+
- resource invalidation is optional
|
7
|
+
|
3
8
|
## v0.3.0
|
4
9
|
|
5
10
|
- `public_visble` -> `privacy_level`
|
6
11
|
- `PrivacyLevel`s introduced
|
7
12
|
- added `User.authentication()` to get authentication options
|
8
|
-
- Gem is actually a Gem...
|
13
|
+
- Gem is actually a Gem...
|
data/lib/cybercoach.rb
CHANGED
@@ -6,6 +6,7 @@ require 'cybercoach/settings'
|
|
6
6
|
require 'cybercoach/format_not_supported_error'
|
7
7
|
require 'cybercoach/http_error'
|
8
8
|
require 'cybercoach/subclass_responsibility_error'
|
9
|
+
require 'cybercoach/hash_initializable'
|
9
10
|
require 'cybercoach/abstract_resource'
|
10
11
|
require 'cybercoach/resource'
|
11
12
|
require 'cybercoach/resource_page'
|
@@ -11,6 +11,11 @@ module CyberCoach
|
|
11
11
|
#
|
12
12
|
include HTTParty
|
13
13
|
|
14
|
+
#
|
15
|
+
# Instance variables can be initialized with a hash.
|
16
|
+
#
|
17
|
+
include HashInitializable
|
18
|
+
|
14
19
|
#
|
15
20
|
# :private_attr: options
|
16
21
|
# The options that are always sent with each request, e.g. :basic_auth.
|
@@ -23,15 +28,26 @@ module CyberCoach
|
|
23
28
|
|
24
29
|
headers 'content-type' => 'application/json', 'accept' => 'application/json'
|
25
30
|
format :json
|
26
|
-
base_uri
|
31
|
+
base_uri Settings::SERVER_URI
|
27
32
|
|
28
33
|
#
|
29
34
|
# Creates an AbstractResource.
|
35
|
+
# hash:: A hash of values to set on instance variables.
|
30
36
|
#
|
31
|
-
def initialize
|
32
|
-
super
|
33
|
-
@uri = nil
|
37
|
+
def initialize(hash = {})
|
38
|
+
super()
|
34
39
|
@options = {}
|
40
|
+
initialize_with(hash)
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# :category: Invalidation
|
45
|
+
#
|
46
|
+
# Invalidates the uri and the options.
|
47
|
+
#
|
48
|
+
def invalidate
|
49
|
+
invalidate_uri
|
50
|
+
invalidate_options
|
35
51
|
end
|
36
52
|
|
37
53
|
#
|
@@ -41,16 +57,18 @@ module CyberCoach
|
|
41
57
|
# Gets the URI from the response and reads itself again.
|
42
58
|
# Raises HttpError if the request is unsuccessful.
|
43
59
|
# options:: A hash of options to send with the request.
|
60
|
+
# invalidate:: Invalidates it when true, skips invalidation when false.
|
44
61
|
#
|
45
|
-
def read(options = {})
|
46
|
-
|
47
|
-
|
62
|
+
def read(options = {}, invalidate = true)
|
63
|
+
if invalidate
|
64
|
+
self.invalidate
|
65
|
+
end
|
48
66
|
options = @options.merge(options)
|
49
67
|
response = self.class.get(@uri, options)
|
50
68
|
if response.success?
|
51
69
|
deserialize(response)
|
52
70
|
else
|
53
|
-
|
71
|
+
fail HttpError, response.response
|
54
72
|
end
|
55
73
|
end
|
56
74
|
|
@@ -66,7 +84,7 @@ module CyberCoach
|
|
66
84
|
elsif format == :json
|
67
85
|
to_serializable.to_json
|
68
86
|
else
|
69
|
-
|
87
|
+
fail FormatNotSupportedError
|
70
88
|
end
|
71
89
|
end
|
72
90
|
|
@@ -115,7 +133,7 @@ module CyberCoach
|
|
115
133
|
# Must be overridden in a subclass.
|
116
134
|
#
|
117
135
|
def singular_name
|
118
|
-
|
136
|
+
fail SubclassResponsibilityError
|
119
137
|
end
|
120
138
|
|
121
139
|
#
|
@@ -127,7 +145,7 @@ module CyberCoach
|
|
127
145
|
# Must be overridden in a subclass.
|
128
146
|
#
|
129
147
|
def plural_name
|
130
|
-
|
148
|
+
fail SubclassResponsibilityError
|
131
149
|
end
|
132
150
|
|
133
151
|
#
|
@@ -158,5 +176,9 @@ module CyberCoach
|
|
158
176
|
#
|
159
177
|
def invalidate_options
|
160
178
|
end
|
179
|
+
|
180
|
+
def initializable_with
|
181
|
+
[:uri]
|
182
|
+
end
|
161
183
|
end
|
162
184
|
end
|
data/lib/cybercoach/entry.rb
CHANGED
@@ -0,0 +1,50 @@
|
|
1
|
+
module CyberCoach
|
2
|
+
#
|
3
|
+
# Mixin for an object that be initialized with a hash.
|
4
|
+
#
|
5
|
+
module HashInitializable
|
6
|
+
#
|
7
|
+
# Installs class and instance methods in the class it is included in.
|
8
|
+
#
|
9
|
+
def self.included(base)
|
10
|
+
base.extend ClassMethods
|
11
|
+
base.send :include, InstanceMethods
|
12
|
+
end
|
13
|
+
|
14
|
+
#
|
15
|
+
# The class methods to install.
|
16
|
+
#
|
17
|
+
module ClassMethods
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# The instance methods to install.
|
22
|
+
#
|
23
|
+
module InstanceMethods
|
24
|
+
#
|
25
|
+
# :category: Initialization
|
26
|
+
#
|
27
|
+
# Returns a list of instance variables it can be initialized with.
|
28
|
+
#
|
29
|
+
def initializable_with
|
30
|
+
fail SubclassResponsibilityError
|
31
|
+
end
|
32
|
+
|
33
|
+
#
|
34
|
+
# :category: Initialization
|
35
|
+
#
|
36
|
+
# Initializes the instance variables with a name in the specified names to
|
37
|
+
# the values mapped in the specified hash.
|
38
|
+
# hash:: A hash of values to set on instance variables.
|
39
|
+
# names:: The names of the instance variables to set.
|
40
|
+
#
|
41
|
+
def initialize_with(hash)
|
42
|
+
initializable_with.each do |name|
|
43
|
+
if hash.key? name
|
44
|
+
instance_variable_set(:"@#{name}", hash[name])
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/cybercoach/pageable.rb
CHANGED
@@ -21,10 +21,11 @@ module CyberCoach
|
|
21
21
|
#
|
22
22
|
# Returns the first page of resources.
|
23
23
|
# options:: A hash of options to send with the request.
|
24
|
+
# invalidate:: Invalidates it when true, skips invalidation when false.
|
24
25
|
#
|
25
|
-
def read_all(options = {})
|
26
|
-
page = ResourcePage.new(self)
|
27
|
-
page.read(options)
|
26
|
+
def read_all(options = {}, invalidate = true)
|
27
|
+
page = ResourcePage.new(type: self)
|
28
|
+
page.read(options, invalidate)
|
28
29
|
page
|
29
30
|
end
|
30
31
|
end
|
@@ -29,10 +29,12 @@ module CyberCoach
|
|
29
29
|
# Gets the URI from the response and reads itself again.
|
30
30
|
# Raises HttpError if the request is unsuccessful.
|
31
31
|
# options:: A hash of options to send with the request.
|
32
|
+
# invalidate:: Invalidates it when true, skips invalidation when false.
|
32
33
|
#
|
33
|
-
def create(options = {})
|
34
|
-
|
35
|
-
|
34
|
+
def create(options = {}, invalidate = true)
|
35
|
+
if invalidate
|
36
|
+
self.invalidate
|
37
|
+
end
|
36
38
|
options = @options.merge(options).merge(
|
37
39
|
body: serialize
|
38
40
|
)
|
@@ -41,7 +43,7 @@ module CyberCoach
|
|
41
43
|
@uri = response.headers['location']
|
42
44
|
read(options)
|
43
45
|
else
|
44
|
-
|
46
|
+
fail HttpError, response.response
|
45
47
|
end
|
46
48
|
end
|
47
49
|
end
|
@@ -29,10 +29,12 @@ module CyberCoach
|
|
29
29
|
# Reads itself from the response.
|
30
30
|
# Raises HttpError if the request is unsuccessful.
|
31
31
|
# options:: A hash of options to send with the request.
|
32
|
+
# invalidate:: Invalidates it when true, skips invalidation when false.
|
32
33
|
#
|
33
|
-
def create(options = {})
|
34
|
-
|
35
|
-
|
34
|
+
def create(options = {}, invalidate = true)
|
35
|
+
if invalidate
|
36
|
+
self.invalidate
|
37
|
+
end
|
36
38
|
options = @options.merge(options).merge(
|
37
39
|
body: serialize
|
38
40
|
)
|
@@ -40,7 +42,7 @@ module CyberCoach
|
|
40
42
|
if response.success?
|
41
43
|
deserialize(response)
|
42
44
|
else
|
43
|
-
|
45
|
+
fail HttpError, response.response
|
44
46
|
end
|
45
47
|
end
|
46
48
|
end
|
data/lib/cybercoach/resource.rb
CHANGED
@@ -15,9 +15,10 @@ module CyberCoach
|
|
15
15
|
# Must be overridden in a subclass.
|
16
16
|
# May use PutCreateable or PostCreateable.
|
17
17
|
# options:: A hash of options to send with the request.
|
18
|
+
# invalidate:: Invalidates it when true, skips invalidation when false.
|
18
19
|
#
|
19
|
-
def create(
|
20
|
-
|
20
|
+
def create(options = {}, invalidate = true)
|
21
|
+
fail SubclassResponsibilityError
|
21
22
|
end
|
22
23
|
|
23
24
|
#
|
@@ -27,10 +28,12 @@ module CyberCoach
|
|
27
28
|
# Reads itself from the response.
|
28
29
|
# Raises HttpError if the request is unsuccessful.
|
29
30
|
# options:: A hash of options to send with the request.
|
31
|
+
# invalidate:: Invalidates it when true, skips invalidation when false.
|
30
32
|
#
|
31
|
-
def update(options = {})
|
32
|
-
|
33
|
-
|
33
|
+
def update(options = {}, invalidate = true)
|
34
|
+
if invalidate
|
35
|
+
self.invalidate
|
36
|
+
end
|
34
37
|
options = @options.merge(options).merge(
|
35
38
|
body: serialize
|
36
39
|
)
|
@@ -38,7 +41,7 @@ module CyberCoach
|
|
38
41
|
if response.success?
|
39
42
|
deserialize(response)
|
40
43
|
else
|
41
|
-
|
44
|
+
fail HttpError, response.response
|
42
45
|
end
|
43
46
|
end
|
44
47
|
|
@@ -49,16 +52,18 @@ module CyberCoach
|
|
49
52
|
# Reads itself from the response.
|
50
53
|
# Raises HttpError if the request is unsuccessful.
|
51
54
|
# options:: A hash of options to send with the request.
|
55
|
+
# invalidate:: Invalidates it when true, skips invalidation when false.
|
52
56
|
#
|
53
|
-
def delete(options = {})
|
54
|
-
|
55
|
-
|
57
|
+
def delete(options = {}, invalidate = true)
|
58
|
+
if invalidate
|
59
|
+
self.invalidate
|
60
|
+
end
|
56
61
|
options = @options.merge(options)
|
57
62
|
response = self.class.delete(@uri, options)
|
58
63
|
if response.success?
|
59
64
|
deserialize(response)
|
60
65
|
else
|
61
|
-
|
66
|
+
fail HttpError, response.response
|
62
67
|
end
|
63
68
|
end
|
64
69
|
|
@@ -90,5 +95,9 @@ module CyberCoach
|
|
90
95
|
serializable['id'] = @id
|
91
96
|
serializable
|
92
97
|
end
|
98
|
+
|
99
|
+
def initializable_with
|
100
|
+
super + [:id]
|
101
|
+
end
|
93
102
|
end
|
94
103
|
end
|
@@ -49,11 +49,13 @@ module CyberCoach
|
|
49
49
|
|
50
50
|
#
|
51
51
|
# Create a ResourcePage of the specified type.
|
52
|
-
#
|
52
|
+
# hash:: A hash of values to set on instance variables. :type is required.
|
53
53
|
#
|
54
|
-
def initialize(
|
55
|
-
|
56
|
-
|
54
|
+
def initialize(hash = {})
|
55
|
+
unless hash.key? :type
|
56
|
+
fail ArgumentError, 'requires a :type entry in the hash'
|
57
|
+
end
|
58
|
+
super(hash)
|
57
59
|
end
|
58
60
|
|
59
61
|
#
|
@@ -62,20 +64,23 @@ module CyberCoach
|
|
62
64
|
# Returns the next page.
|
63
65
|
# Raises NoNextPageError if the is none.
|
64
66
|
# options:: A hash of options to send with the request.
|
67
|
+
# invalidate:: Invalidates it when true, skips invalidation when false.
|
65
68
|
#
|
66
|
-
def next(options = {})
|
69
|
+
def next(options = {}, invalidate = true)
|
67
70
|
if @next.nil?
|
68
|
-
|
71
|
+
fail NoNextPageError
|
72
|
+
end
|
73
|
+
if invalidate
|
74
|
+
self.invalidate
|
69
75
|
end
|
70
|
-
invalidate_options
|
71
76
|
options = @options.merge(options)
|
72
77
|
response = self.class.get(@next['href'], options)
|
73
78
|
if response.success?
|
74
|
-
page = self.class.new(@type)
|
79
|
+
page = self.class.new(type: @type)
|
75
80
|
page.deserialize(response)
|
76
81
|
page
|
77
82
|
else
|
78
|
-
|
83
|
+
fail HttpError, response.response
|
79
84
|
end
|
80
85
|
end
|
81
86
|
|
@@ -85,20 +90,23 @@ module CyberCoach
|
|
85
90
|
# Returns the previous page.
|
86
91
|
# Raises NoPreviousPageError if the is none.
|
87
92
|
# options:: A hash of options to send with the request.
|
93
|
+
# invalidate:: Invalidates it when true, skips invalidation when false.
|
88
94
|
#
|
89
|
-
def previous(options = {})
|
95
|
+
def previous(options = {}, invalidate = true)
|
90
96
|
if @previous.nil?
|
91
|
-
|
97
|
+
fail NoPreviousPageError
|
98
|
+
end
|
99
|
+
if invalidate
|
100
|
+
self.invalidate
|
92
101
|
end
|
93
|
-
invalidate_options
|
94
102
|
options = @options.merge(options)
|
95
103
|
response = self.class.get(@previous['href'], options)
|
96
104
|
if response.success?
|
97
|
-
page = self.class.new(@type)
|
105
|
+
page = self.class.new(type: @type)
|
98
106
|
page.deserialize(response)
|
99
107
|
page
|
100
108
|
else
|
101
|
-
|
109
|
+
fail HttpError, response.response
|
102
110
|
end
|
103
111
|
end
|
104
112
|
|
@@ -187,5 +195,9 @@ module CyberCoach
|
|
187
195
|
def invalidate_uri
|
188
196
|
@uri = resource_base_uri
|
189
197
|
end
|
198
|
+
|
199
|
+
def initializable_with
|
200
|
+
super + [:type, :start, :end, :size]
|
201
|
+
end
|
190
202
|
end
|
191
203
|
end
|
data/lib/cybercoach/sport.rb
CHANGED