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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 59526a9235348317412b0f880b808b23357827aa
4
- data.tar.gz: 77285e272d541197cd65e1752fae2d96f0ff78ce
3
+ metadata.gz: a673d760d6a53d29163f737519c2c1abd8d45301
4
+ data.tar.gz: efcda8eca1e90172b343bc89e8ba2704ef71af10
5
5
  SHA512:
6
- metadata.gz: fe5ffcf504246b9f3ac0b9203f3d5457676ab3cdee654b6cc4d5c85cabac93e63e6f08d1b9e3b9f4f7384254be23e8b57ca4a3bbae0a8bce4d4b84c0bd323be5
7
- data.tar.gz: 29f7d4a1542fccd1ce2ec78c14112c8aa55ec34b0492ff2963c82e347135e8acdb6fac6bd2bf5eef4a4888db5b659a92988687330f2de93499e1bbcf6127544a
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 CyberCoach::Settings::SERVER_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
- invalidate_uri
47
- invalidate_options
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
- raise HttpError.new(response.response)
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
- raise FormatNotSupportedError.new
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
- raise SubclassResponsibilityError.new
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
- raise SubclassResponsibilityError.new
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
@@ -162,5 +162,9 @@ module CyberCoach
162
162
  @uri = "#{resource_base_uri}#{@id}/"
163
163
  end
164
164
  end
165
+
166
+ def initializable_with
167
+ super + [:subscription, :comment, :location, :duration, :privacy_level]
168
+ end
165
169
  end
166
170
  end
@@ -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
@@ -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
@@ -147,5 +147,9 @@ module CyberCoach
147
147
  @uri = "#{resource_base_uri}#{@proposer.username};#{@proposed.username}/"
148
148
  end
149
149
  end
150
+
151
+ def initializable_with
152
+ super + [:proposer, :proposed, :privacy_level]
153
+ end
150
154
  end
151
155
  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
- invalidate_uri
35
- invalidate_options
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
- raise HttpError.new(response.response)
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
- invalidate_uri
35
- invalidate_options
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
- raise HttpError.new(response.response)
45
+ fail HttpError, response.response
44
46
  end
45
47
  end
46
48
  end
@@ -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(_options = {})
20
- raise SubclassResponsibilityError.new
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
- invalidate_uri
33
- invalidate_options
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
- raise HttpError.new(response.response)
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
- invalidate_uri
55
- invalidate_options
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
- raise HttpError.new(response.response)
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
- # type:: The class of resource to page.
52
+ # hash:: A hash of values to set on instance variables. :type is required.
53
53
  #
54
- def initialize(type)
55
- super()
56
- @type = type
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
- raise NoNextPageError.new
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
- raise HttpError.new(response.response)
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
- raise NoPreviousPageError.new
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
- raise HttpError.new(response.response)
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
@@ -100,5 +100,9 @@ module CyberCoach
100
100
  @uri = "#{resource_base_uri}#{@name}/"
101
101
  end
102
102
  end
103
+
104
+ def initializable_with
105
+ super + [:name, :description]
106
+ end
103
107
  end
104
108
  end
@@ -141,5 +141,9 @@ module CyberCoach
141
141
  @uri = resource_base_uri
142
142
  end
143
143
  end
144
+
145
+ def initializable_with
146
+ super + [:subscriber, :sport, :privacy_level]
147
+ end
144
148
  end
145
149
  end