osm 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +7 -0
- data/README.md +33 -1
- data/lib/osm/activity.rb +8 -8
- data/lib/osm/api.rb +152 -100
- data/lib/osm/api_access.rb +1 -1
- data/lib/osm/evening.rb +3 -3
- data/lib/osm/event.rb +2 -2
- data/lib/osm/grouping.rb +2 -2
- data/lib/osm/member.rb +8 -8
- data/lib/osm/role.rb +1 -1
- data/lib/osm/section.rb +6 -4
- data/lib/osm/term.rb +3 -3
- data/osm.gemspec +1 -2
- data/spec/osm/api_spec.rb +24 -0
- data/spec/spec_helper.rb +13 -25
- data/version.rb +1 -1
- metadata +12 -23
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
## Version 0.0.9
|
2
|
+
|
3
|
+
* Allow passing of Osm::Term objects as well as term IDs
|
4
|
+
* Allow passing of Osm::Section objects as well as section IDs
|
5
|
+
* Allow configuration of text prepended to keys used in the cache (:cache\_prepend\_to\_key option to configure method)
|
6
|
+
* Require setting of cache class to use caching (:cache option to configure method)
|
7
|
+
|
1
8
|
## version 0.0.8
|
2
9
|
|
3
10
|
* Fix unknown variable when updating evening
|
data/README.md
CHANGED
@@ -23,7 +23,7 @@ Add to your Gemfile and run the `bundle` command to install it.
|
|
23
23
|
gem 'osm'
|
24
24
|
```
|
25
25
|
|
26
|
-
Configure the gem during the initalization of the app (e.g.
|
26
|
+
Configure the gem during the initalization of the app (e.g. if using rails then config/initializers/osm.rb would look like):
|
27
27
|
|
28
28
|
```ruby
|
29
29
|
ActionDispatch::Callbacks.to_prepare do
|
@@ -32,6 +32,7 @@ Osm::Api.configure(
|
|
32
32
|
:api_token => 'YOU WILL BE GIVEN THIS BY ED AT OSM',
|
33
33
|
:api_name => 'YOU WILL GIVE THIS TO ED AT OSM',
|
34
34
|
:api_site => :scout,
|
35
|
+
:cache => Rails.cache
|
35
36
|
)
|
36
37
|
end
|
37
38
|
```
|
@@ -58,3 +59,34 @@ Documentation can be found on [rubydoc.info](http://rubydoc.info/github/robertga
|
|
58
59
|
|
59
60
|
We follow the [Semantic Versioning](http://semver.org/) concept,
|
60
61
|
however it should be noted that when the OSM API adds a feature it can be difficult to decide wether to bump the patch or minor version number up. A smaller change (such as adding score into the grouping object) will bump the patch whereas a larger change wil bump the minor version.
|
62
|
+
|
63
|
+
|
64
|
+
## Parts of the OSM API Supported:
|
65
|
+
|
66
|
+
### Read
|
67
|
+
* Activity
|
68
|
+
* API Access
|
69
|
+
* API Access for our app
|
70
|
+
* Due Badges
|
71
|
+
* Events
|
72
|
+
* Groupings (e.g. Sixes, Patrols)
|
73
|
+
* Members
|
74
|
+
* Notepad
|
75
|
+
* Notepads
|
76
|
+
* Programme
|
77
|
+
* Register
|
78
|
+
* Register Structure
|
79
|
+
* Roles
|
80
|
+
* Section
|
81
|
+
* Sections
|
82
|
+
* Term
|
83
|
+
* Terms
|
84
|
+
|
85
|
+
### Update
|
86
|
+
* Evening
|
87
|
+
|
88
|
+
### Create
|
89
|
+
* Evening
|
90
|
+
|
91
|
+
### Actions
|
92
|
+
* Authorise an app to use the API on a user's behalf
|
data/lib/osm/activity.rb
CHANGED
@@ -4,13 +4,13 @@ module Osm
|
|
4
4
|
|
5
5
|
attr_reader :id, :version, :group_id, :user_id, :title, :description, :resources, :instructions, :running_time, :location, :shared, :rating, :editable, :deletable, :used, :versions, :sections, :tags, :files, :badges
|
6
6
|
# @!attribute [r] id
|
7
|
-
# @return [
|
7
|
+
# @return [Fixnum] the id for the activity
|
8
8
|
# @!attribute [r] version
|
9
|
-
# @return [
|
9
|
+
# @return [Fixnum] the version of the activity
|
10
10
|
# @!attribute [r] group_id
|
11
|
-
# @return [
|
11
|
+
# @return [Fixnum] the group_id
|
12
12
|
# @!attribute [r] user_id
|
13
|
-
# @return [
|
13
|
+
# @return [Fixnum] the user_id of the creator of the activity
|
14
14
|
# @!attribute [r] title
|
15
15
|
# @return [String] the activity's title
|
16
16
|
# @!attribute [r] description
|
@@ -20,19 +20,19 @@ module Osm
|
|
20
20
|
# @!attribute [r] instructions
|
21
21
|
# @return [String] instructions for doing the activity
|
22
22
|
# @!attribute [r] running_time
|
23
|
-
# @return [
|
23
|
+
# @return [Fixnum] duration of the activity in minutes
|
24
24
|
# @!attribute [r] location
|
25
25
|
# @return [Symbol] :indoors, :outdoors or :both
|
26
26
|
# @!attribute [r] shared
|
27
|
-
# @return [
|
27
|
+
# @return [Fixnum] 2 - Public, 0 - Private
|
28
28
|
# @!attribute [r] rating
|
29
|
-
# @return [
|
29
|
+
# @return [Fixnum] ?
|
30
30
|
# @!attribute [r] editable
|
31
31
|
# @return [Boolean] Wether the current API user can edit this activity
|
32
32
|
# @!attribute [r] deletable
|
33
33
|
# @return [Boolean] Wether the current API user can delete this activity
|
34
34
|
# @!attribute [r] used
|
35
|
-
# @return [
|
35
|
+
# @return [Fixnum] How many times this activity has been used (total accross all of OSM)
|
36
36
|
# @!attribute [r] versions
|
37
37
|
# @return [Array<Hash>] ? (:value - version, :firstname - created by, :label - label, :user_id - OSM user ID of creator)
|
38
38
|
# @!attribute [r] sections
|
data/lib/osm/api.rb
CHANGED
@@ -18,6 +18,8 @@ module Osm
|
|
18
18
|
# Most items however will be cached for this time
|
19
19
|
|
20
20
|
@@user_access = Hash.new
|
21
|
+
@@cache_prepend_to_key = 'OSMAPI'
|
22
|
+
@@cache = nil
|
21
23
|
|
22
24
|
# Initialize a new API connection
|
23
25
|
# If passing user details then both must be passed
|
@@ -42,7 +44,9 @@ module Osm
|
|
42
44
|
# @option options [String] :api_token the token which goes with the above api
|
43
45
|
# @option options [String] :api_name the name displayed in the External Access tab of OSM
|
44
46
|
# @option options [Symbol] :api_sate wether to use OSM (if :scout) or OGM (if :guide)
|
45
|
-
# @option options [
|
47
|
+
# @option options [Class] :cache (optional) An instance of a cache class, must provide the methods (exist?, delete, write, read), for details see Rails.cache. Whilst this is optional you should remember that caching is required to use the OSM API.
|
48
|
+
# @option options [Fixnum] :default_cache_ttl (optional, default = 30.minutes) The default TTL value for the cache, note that some items are cached for twice this time and others are cached for half this time (in seconds)
|
49
|
+
# @option options [String] :cache_prepend_to_key (optional, default = 'OSMAPI') Text to prepend to the key used to store data in the cache
|
46
50
|
# @return nil
|
47
51
|
def self.configure(options)
|
48
52
|
raise ArgumentError, ':api_id does not exist in options hash' if options[:api_id].nil?
|
@@ -50,12 +54,19 @@ module Osm
|
|
50
54
|
raise ArgumentError, ':api_name does not exist in options hash' if options[:api_name].nil?
|
51
55
|
raise ArgumentError, ':api_site does not exist in options hash or is invalid, this should be set to either :scout or :guide' unless [:scout, :guide].include?(options[:api_site])
|
52
56
|
raise ArgumentError, ':default_cache_ttl must be greater than 0' unless (options[:default_cache_ttl].nil? || options[:default_cache_ttl].to_i > 0)
|
57
|
+
unless options[:cache].nil?
|
58
|
+
[:exist?, :delete, :write, :read].each do |method|
|
59
|
+
raise ArgumentError, ":cache must have a #{method} method" unless options[:cache].methods.include?(method)
|
60
|
+
end
|
61
|
+
end
|
53
62
|
|
54
63
|
@@api_id = options[:api_id].to_s
|
55
64
|
@@api_token = options[:api_token].to_s
|
56
65
|
@@api_name = options[:api_name].to_s
|
57
66
|
@@api_site = options[:api_site]
|
58
67
|
@@default_cache_ttl = options[:default_cache_ttl].to_i unless options[:default_cache_ttl].nil?
|
68
|
+
@@cache_prepend_to_key = options[:cache_prepend_to_key].to_s unless options[:cache_prepend_to_key].nil?
|
69
|
+
@@cache = options[:cache]
|
59
70
|
nil
|
60
71
|
end
|
61
72
|
|
@@ -94,8 +105,8 @@ module Osm
|
|
94
105
|
# @return [Array<Osm::Role>]
|
95
106
|
def get_roles(options={}, api_data={})
|
96
107
|
|
97
|
-
if !options[:no_cache] &&
|
98
|
-
return
|
108
|
+
if !options[:no_cache] && cache_exist?("roles-#{api_data[:userid] || @userid}")
|
109
|
+
return cache_read("roles-#{api_data[:userid] || @userid}")
|
99
110
|
end
|
100
111
|
|
101
112
|
data = perform_query('api.php?action=getUserRoles', api_data)
|
@@ -104,10 +115,10 @@ module Osm
|
|
104
115
|
data.each do |item|
|
105
116
|
role = Osm::Role.new(item)
|
106
117
|
result.push role
|
107
|
-
|
118
|
+
cache_write("section-#{role.section.id}", role.section, :expires_in => @@default_cache_ttl*2)
|
108
119
|
self.user_can_access :section, role.section.id, api_data
|
109
120
|
end
|
110
|
-
|
121
|
+
cache_write("roles-#{api_data[:userid] || @userid}", result, :expires_in => @@default_cache_ttl*2)
|
111
122
|
|
112
123
|
return result
|
113
124
|
end
|
@@ -117,8 +128,8 @@ module Osm
|
|
117
128
|
# @!macro options_api_data
|
118
129
|
# @return [Hash] a hash (keys are section IDs, values are a string)
|
119
130
|
def get_notepads(options={}, api_data={})
|
120
|
-
if !options[:no_cache] &&
|
121
|
-
return
|
131
|
+
if !options[:no_cache] && cache_exist?("notepads-#{api_data[:userid] || @userid}")
|
132
|
+
return cache_read("notepads-#{api_data[:userid] || @userid}")
|
122
133
|
end
|
123
134
|
|
124
135
|
notepads = perform_query('api.php?action=getNotepads', api_data)
|
@@ -127,22 +138,24 @@ module Osm
|
|
127
138
|
data = {}
|
128
139
|
notepads.each do |key, value|
|
129
140
|
data[key.to_i] = value
|
130
|
-
|
141
|
+
cache_write("notepad-#{key}", value, :expires_in => @@default_cache_ttl*2)
|
131
142
|
end
|
132
143
|
|
133
|
-
|
144
|
+
cache_write("notepads-#{api_data[:userid] || @userid}", data, :expires_in => @@default_cache_ttl*2)
|
134
145
|
return data
|
135
146
|
end
|
136
147
|
|
137
148
|
# Get the notepad for a specified section
|
138
|
-
# @param [
|
149
|
+
# @param [Osm:Section, Fixnum] section the section (or its ID) to get the notepad for
|
139
150
|
# @!macro options_get
|
140
151
|
# @!macro options_api_data
|
141
152
|
# @return nil if an error occured or the user does not have access to that section
|
142
153
|
# @return [String] the content of the notepad otherwise
|
143
|
-
def get_notepad(
|
144
|
-
|
145
|
-
|
154
|
+
def get_notepad(section, options={}, api_data={})
|
155
|
+
section_id = id_for_section(section)
|
156
|
+
|
157
|
+
if !options[:no_cache] && cache_exist?("notepad-#{section_id}") && self.user_can_access?(:section, section_id, api_data)
|
158
|
+
return cache_read("notepad-#{section_id}")
|
146
159
|
end
|
147
160
|
|
148
161
|
notepads = get_notepads(options, api_data)
|
@@ -156,15 +169,14 @@ module Osm
|
|
156
169
|
end
|
157
170
|
|
158
171
|
# Get the section (and its configuration)
|
159
|
-
# @param [
|
172
|
+
# @param [Fixnum] section_id the section id of the required section
|
160
173
|
# @!macro options_get
|
161
174
|
# @!macro options_api_data
|
162
175
|
# @return nil if an error occured or the user does not have access to that section
|
163
176
|
# @return [Osm::Section]
|
164
177
|
def get_section(section_id, options={}, api_data={})
|
165
|
-
|
166
|
-
|
167
|
-
return Rails.cache.read("OSMAPI-section-#{section_id}")
|
178
|
+
if !options[:no_cache] && cache_exist?("section-#{section_id}") && self.user_can_access?(:section, section_id, api_data)
|
179
|
+
return cache_read("section-#{section_id}")
|
168
180
|
end
|
169
181
|
|
170
182
|
roles = get_roles(options, api_data)
|
@@ -178,13 +190,15 @@ module Osm
|
|
178
190
|
end
|
179
191
|
|
180
192
|
# Get the groupings (e.g. patrols, sixes, lodges) for a given section
|
181
|
-
# @param [
|
193
|
+
# @param [Osm::Section, Fixnum] section the section to get the groupings for
|
182
194
|
# @!macro options_get
|
183
195
|
# @!macro options_api_data
|
184
196
|
# @return [Array<Osm::Grouping>]
|
185
|
-
def get_groupings(
|
186
|
-
|
187
|
-
|
197
|
+
def get_groupings(section, options={}, api_data={})
|
198
|
+
section_id = id_for_section(section)
|
199
|
+
|
200
|
+
if !options[:no_cache] && cache_exist?("groupings-#{section_id}") && self.user_can_access?(:section, section_id, api_data)
|
201
|
+
return cache_read("groupings-#{section_id}")
|
188
202
|
end
|
189
203
|
|
190
204
|
data = perform_query("users.php?action=getPatrols§ionid=#{section_id}", api_data)
|
@@ -193,10 +207,10 @@ module Osm
|
|
193
207
|
data['patrols'].each do |item|
|
194
208
|
grouping = Osm::Grouping.new(item)
|
195
209
|
result.push grouping
|
196
|
-
|
210
|
+
cache_write("grouping-#{grouping.id}", grouping, :expires_in => @@default_cache_ttl*2)
|
197
211
|
self.user_can_access :grouping, grouping.id, api_data
|
198
212
|
end
|
199
|
-
|
213
|
+
cache_write("groupings-#{section_id}", result, :expires_in => @@default_cache_ttl*2)
|
200
214
|
|
201
215
|
return result
|
202
216
|
end
|
@@ -206,8 +220,8 @@ module Osm
|
|
206
220
|
# @!macro options_api_data
|
207
221
|
# @return [Array<Osm::Term>]
|
208
222
|
def get_terms(options={}, api_data={})
|
209
|
-
if !options[:no_cache] &&
|
210
|
-
return
|
223
|
+
if !options[:no_cache] && cache_exist?("terms-#{api_data[:userid] || @userid}")
|
224
|
+
return cache_read("terms-#{api_data[:userid] || @userid}")
|
211
225
|
end
|
212
226
|
|
213
227
|
data = perform_query('api.php?action=getTerms', api_data)
|
@@ -217,24 +231,24 @@ module Osm
|
|
217
231
|
data[key].each do |item|
|
218
232
|
term = Osm::Term.new(item)
|
219
233
|
result.push term
|
220
|
-
|
234
|
+
cache_write("term-#{term.id}", term, :expires_in => @@default_cache_ttl*2)
|
221
235
|
self.user_can_access :term, term.id, api_data
|
222
236
|
end
|
223
237
|
end
|
224
238
|
|
225
|
-
|
239
|
+
cache_write("terms-#{api_data[:userid] || @userid}", result, :expires_in => @@default_cache_ttl*2)
|
226
240
|
return result
|
227
241
|
end
|
228
242
|
|
229
243
|
# Get a term
|
230
|
-
# @param [
|
244
|
+
# @param [Fixnum] term_id the id of the required term
|
231
245
|
# @!macro options_get
|
232
246
|
# @!macro options_api_data
|
233
247
|
# @return nil if an error occured or the user does not have access to that term
|
234
248
|
# @return [Osm::Term]
|
235
249
|
def get_term(term_id, options={}, api_data={})
|
236
|
-
if !options[:no_cache] &&
|
237
|
-
return
|
250
|
+
if !options[:no_cache] && cache_exist?("term-#{term_id}") && self.user_can_access?(:term, term_id, api_data)
|
251
|
+
return cache_read("term-#{term_id}")
|
238
252
|
end
|
239
253
|
|
240
254
|
terms = get_terms(options)
|
@@ -248,14 +262,17 @@ module Osm
|
|
248
262
|
end
|
249
263
|
|
250
264
|
# Get the programme for a given term
|
251
|
-
# @param [
|
252
|
-
# @param [
|
265
|
+
# @param [Osm:Section, Fixnum] section the section (or its ID) to get the programme for
|
266
|
+
# @param [Osm:term, Fixnum] term the term (or its ID) to get the programme for, passing nil causes the current term to be used
|
253
267
|
# @!macro options_get
|
254
268
|
# @!macro options_api_data
|
255
269
|
# @return [Array<Osm::Evening>]
|
256
|
-
def get_programme(
|
257
|
-
|
258
|
-
|
270
|
+
def get_programme(section, term, options={}, api_data={})
|
271
|
+
section_id = id_for_section(section)
|
272
|
+
term_id = id_for_term(term)
|
273
|
+
|
274
|
+
if !options[:no_cache] && cache_exist?("programme-#{section_id}-#{term_id}") && self.user_can_access?(:programme, section_id, api_data)
|
275
|
+
return cache_read("programme-#{section_id}-#{term_id}")
|
259
276
|
end
|
260
277
|
|
261
278
|
data = perform_query("programme.php?action=getProgramme§ionid=#{section_id}&termid=#{term_id}", api_data)
|
@@ -274,19 +291,19 @@ module Osm
|
|
274
291
|
end
|
275
292
|
end
|
276
293
|
|
277
|
-
|
294
|
+
cache_write("programme-#{section_id}-#{term_id}", result, :expires_in => @@default_cache_ttl)
|
278
295
|
return result
|
279
296
|
end
|
280
297
|
|
281
298
|
# Get activity details
|
282
|
-
# @param [
|
283
|
-
# @param [
|
299
|
+
# @param [Fixnum] activity_id the activity ID
|
300
|
+
# @param [Fixnum] version the version of the activity to retreive, if nil the latest version will be assumed
|
284
301
|
# @!macro options_get
|
285
302
|
# @!macro options_api_data
|
286
303
|
# @return [Osm::Activity]
|
287
304
|
def get_activity(activity_id, version=nil, options={}, api_data={})
|
288
|
-
if !options[:no_cache] &&
|
289
|
-
return
|
305
|
+
if !options[:no_cache] && cache_exist?("activity-#{activity_id}-#{version}") && self.user_can_access?(:activity, activity_id, api_data)
|
306
|
+
return cache_read("activity-#{activity_id}-#{version}")
|
290
307
|
end
|
291
308
|
|
292
309
|
data = nil
|
@@ -297,24 +314,25 @@ module Osm
|
|
297
314
|
end
|
298
315
|
|
299
316
|
activity = Osm::Activity.new(data)
|
300
|
-
|
301
|
-
|
317
|
+
cache_write("activity-#{activity_id}-#{nil}", activity, :expires_in => @@default_cache_ttl*2) if version.nil?
|
318
|
+
cache_write("activity-#{activity_id}-#{activity.version}", activity, :expires_in => @@default_cache_ttl/2)
|
302
319
|
self.user_can_access :activity, activity.id, api_data
|
303
320
|
|
304
321
|
return activity
|
305
322
|
end
|
306
323
|
|
307
|
-
# Get
|
308
|
-
# @param [
|
309
|
-
# @param [
|
324
|
+
# Get members
|
325
|
+
# @param [Osm:Section, Fixnum] section the section (or its ID) to get the members for
|
326
|
+
# @param [Osm:Term, Fixnum] term the term (or its ID) to get the members for, passing nil causes the current term to be used
|
310
327
|
# @!macro options_get
|
311
328
|
# @!macro options_api_data
|
312
329
|
# @return [Array<Osm::Member>]
|
313
|
-
def get_members(
|
314
|
-
|
330
|
+
def get_members(section, term=nil, options={}, api_data={})
|
331
|
+
section_id = id_for_section(section)
|
332
|
+
term_id = id_for_term(term)
|
315
333
|
|
316
|
-
if !options[:no_cache] &&
|
317
|
-
return
|
334
|
+
if !options[:no_cache] && cache_exist?("members-#{section_id}-#{term_id}") && self.user_can_access?(:member, section_id, api_data)
|
335
|
+
return cache_read("members-#{section_id}-#{term_id}")
|
318
336
|
end
|
319
337
|
|
320
338
|
data = perform_query("users.php?action=getUserDetails§ionid=#{section_id}&termid=#{term_id}", api_data)
|
@@ -324,19 +342,21 @@ module Osm
|
|
324
342
|
result.push Osm::Member.new(item)
|
325
343
|
end
|
326
344
|
self.user_can_access :member, section_id, api_data
|
327
|
-
|
345
|
+
cache_write("members-#{section_id}-#{term_id}", result, :expires_in => @@default_cache_ttl)
|
328
346
|
|
329
347
|
return result
|
330
348
|
end
|
331
349
|
|
332
350
|
# Get API access details for a given section
|
333
|
-
# @param [
|
351
|
+
# @param [Osm:Section, Fixnum] section the section (or its ID) to get the details for
|
334
352
|
# @!macro options_get
|
335
353
|
# @!macro options_api_data
|
336
354
|
# @return [Array<Osm::ApiAccess>]
|
337
|
-
def get_api_access(
|
338
|
-
|
339
|
-
|
355
|
+
def get_api_access(section, options={}, api_data={})
|
356
|
+
section_id = id_for_section(section)
|
357
|
+
|
358
|
+
if !options[:no_cache] && cache_exist?("api_access-#{api_data['userid'] || @userid}-#{section_id}")
|
359
|
+
return cache_read("api_access-#{api_data['userid'] || @userid}-#{section_id}")
|
340
360
|
end
|
341
361
|
|
342
362
|
data = perform_query("users.php?action=getAPIAccess§ionid=#{section_id}", api_data)
|
@@ -348,20 +368,22 @@ module Osm
|
|
348
368
|
self.user_can_access(:programme, section_id, api_data) if this_item.can_read?(:programme)
|
349
369
|
self.user_can_access(:member, section_id, api_data) if this_item.can_read?(:member)
|
350
370
|
self.user_can_access(:badge, section_id, api_data) if this_item.can_read?(:badge)
|
351
|
-
|
371
|
+
cache_write("api_access-#{api_data['userid'] || @userid}-#{section_id}-#{this_item.id}", this_item, :expires_in => @@default_cache_ttl*2)
|
352
372
|
end
|
353
373
|
|
354
374
|
return result
|
355
375
|
end
|
356
376
|
|
357
377
|
# Get our API access details for a given section
|
358
|
-
# @param [
|
378
|
+
# @param [Osm:Section, Fixnum] section the section (or its ID) to get the details for
|
359
379
|
# @!macro options_get
|
360
380
|
# @!macro options_api_data
|
361
381
|
# @return [Osm::ApiAccess]
|
362
|
-
def get_our_api_access(
|
363
|
-
|
364
|
-
|
382
|
+
def get_our_api_access(section, options={}, api_data={})
|
383
|
+
section_id = id_for_section(section)
|
384
|
+
|
385
|
+
if !options[:no_cache] && cache_exist?("api_access-#{api_data['userid'] || @userid}-#{section_id}-#{Osm::Api.api_id}")
|
386
|
+
return cache_read("api_access-#{api_data['userid'] || @userid}-#{section_id}-#{Osm::Api.api_id}")
|
365
387
|
end
|
366
388
|
|
367
389
|
data = get_api_access(section_id, options)
|
@@ -374,13 +396,15 @@ module Osm
|
|
374
396
|
end
|
375
397
|
|
376
398
|
# Get events
|
377
|
-
# @param [
|
399
|
+
# @param [Osm:Section, Fixnum] section the section (or its ID) to get the events for
|
378
400
|
# @!macro options_get
|
379
401
|
# @!macro options_api_data
|
380
402
|
# @return [Array<Osm::Event>]
|
381
|
-
def get_events(
|
382
|
-
|
383
|
-
|
403
|
+
def get_events(section, options={}, api_data={})
|
404
|
+
section_id = id_for_section(section)
|
405
|
+
|
406
|
+
if !options[:no_cache] && cache_exist?("events-#{section_id}") && self.user_can_access?(:programme, section_id, api_data)
|
407
|
+
return cache_read("events-#{section_id}")
|
384
408
|
end
|
385
409
|
|
386
410
|
data = perform_query("events.php?action=getEvents§ionid=#{section_id}", api_data)
|
@@ -392,21 +416,22 @@ module Osm
|
|
392
416
|
end
|
393
417
|
end
|
394
418
|
self.user_can_access :programme, section_id, api_data
|
395
|
-
|
419
|
+
cache_write("events-#{section_id}", result, :expires_in => @@default_cache_ttl)
|
396
420
|
|
397
421
|
return result
|
398
422
|
end
|
399
423
|
|
400
424
|
# Get due badges
|
401
|
-
# @param [
|
425
|
+
# @param [Osm:Section, Fixnum] section the section (or its ID) to get the due badges for
|
402
426
|
# @!macro options_get
|
403
427
|
# @!macro options_api_data
|
404
428
|
# @return [Osm::DueBadges]
|
405
|
-
def get_due_badges(
|
406
|
-
|
429
|
+
def get_due_badges(section, term=nil, options={}, api_data={})
|
430
|
+
section_id = id_for_section(section)
|
431
|
+
term_id = id_for_term(term)
|
407
432
|
|
408
|
-
if !options[:no_cache] &&
|
409
|
-
return
|
433
|
+
if !options[:no_cache] && cache_exist?("due_badges-#{section_id}-#{term_id}") && self.user_can_access?(:badge, section_id, api_data)
|
434
|
+
return cache_read("due_badges-#{section_id}-#{term_id}")
|
410
435
|
end
|
411
436
|
|
412
437
|
section_type = get_section(section_id, api_data).type.to_s
|
@@ -414,21 +439,23 @@ module Osm
|
|
414
439
|
|
415
440
|
data = Osm::DueBadges.new(data)
|
416
441
|
self.user_can_access :badge, section_id, api_data
|
417
|
-
|
442
|
+
cache_write("due_badges-#{section_id}-#{term_id}", data, :expires_in => @@default_cache_ttl*2)
|
418
443
|
|
419
444
|
return data
|
420
445
|
end
|
421
446
|
|
422
447
|
# Get register structure
|
423
|
-
# @param [
|
448
|
+
# @param [Osm:Section, Fixnum] section the section (or its ID) to get the structure for
|
449
|
+
# @param [Osm:Term, Fixnum] section the term (or its ID) to get the structure for, passing nil causes the current term to be used
|
424
450
|
# @!macro options_get
|
425
451
|
# @!macro options_api_data
|
426
452
|
# @return [Array<Hash>] representing the rows of the register
|
427
|
-
def get_register_structure(
|
428
|
-
|
453
|
+
def get_register_structure(section, term=nil, options={}, api_data={})
|
454
|
+
section_id = id_for_section(section)
|
455
|
+
term_id = id_for_term(term)
|
429
456
|
|
430
|
-
if !options[:no_cache] &&
|
431
|
-
return
|
457
|
+
if !options[:no_cache] && cache_exist?("register_structure-#{section_id}-#{term_id}") && self.user_can_access?(:register, section_id, api_data)
|
458
|
+
return cache_read("register_structure-#{section_id}-#{term_id}")
|
432
459
|
end
|
433
460
|
|
434
461
|
data = perform_query("users.php?action=registerStructure§ionid=#{section_id}&termid=#{term_id}", api_data)
|
@@ -440,21 +467,23 @@ module Osm
|
|
440
467
|
end
|
441
468
|
end
|
442
469
|
self.user_can_access :register, section_id, api_data
|
443
|
-
|
470
|
+
cache_write("register_structure-#{section_id}-#{term_id}", data, :expires_in => @@default_cache_ttl/2)
|
444
471
|
|
445
472
|
return data
|
446
473
|
end
|
447
474
|
|
448
475
|
# Get register
|
449
|
-
# @param [
|
476
|
+
# @param [Osm:Section, Fixnum] section the section (or its ID) to get the register for
|
477
|
+
# @param [Osm:Term, Fixnum] section the term (or its ID) to get the register for, passing nil causes the current term to be used
|
450
478
|
# @!macro options_get
|
451
479
|
# @!macro options_api_data
|
452
480
|
# @return [Array<Hash>] representing the attendance of each member
|
453
|
-
def get_register(
|
454
|
-
|
481
|
+
def get_register(section, term=nil, options={}, api_data={})
|
482
|
+
section_id = id_for_section(section)
|
483
|
+
term_id = id_for_term(term)
|
455
484
|
|
456
|
-
if !options[:no_cache] &&
|
457
|
-
return
|
485
|
+
if !options[:no_cache] && cache_exist?("register-#{section_id}-#{term_id}") && self.user_can_access?(:register, section_id, api_data)
|
486
|
+
return cache_read("register-#{section_id}-#{term_id}")
|
458
487
|
end
|
459
488
|
|
460
489
|
data = perform_query("users.php?action=register§ionid=#{section_id}&termid=#{term_id}", api_data)
|
@@ -467,17 +496,17 @@ module Osm
|
|
467
496
|
item[:patrolid] = item[:patrolid].to_i
|
468
497
|
end
|
469
498
|
self.user_can_access :register, section_id, api_data
|
470
|
-
|
499
|
+
cache_write("register-#{section_id}-#{term_id}", data, :expires_in => @@default_cache_ttl/2)
|
471
500
|
return data
|
472
501
|
end
|
473
502
|
|
474
503
|
# Create an evening in OSM
|
475
|
-
# @param [
|
504
|
+
# @param [Fixnum] section_id the id of the section to add the term to
|
476
505
|
# @param [Date] meeting_date the date of the meeting
|
477
506
|
# @!macro options_api_data
|
478
507
|
# @return [Boolean] if the operation suceeded or not
|
479
|
-
def create_evening(
|
480
|
-
section_id =
|
508
|
+
def create_evening(section, meeting_date, api_data={})
|
509
|
+
section_id = id_for_section(section)
|
481
510
|
evening_api_data = {
|
482
511
|
'meetingdate' => meeting_date.strftime('%Y-%m-%d'),
|
483
512
|
'sectionid' => section_id,
|
@@ -488,7 +517,7 @@ module Osm
|
|
488
517
|
|
489
518
|
# The cached programmes for the section will be out of date - remove them
|
490
519
|
get_terms(api_data).each do |term|
|
491
|
-
|
520
|
+
cache_delete("programme-#{term.section_id}-#{term.id}") if term.section_id == section_id
|
492
521
|
end
|
493
522
|
|
494
523
|
return data.is_a?(Hash) && (data['result'] == 0)
|
@@ -503,7 +532,7 @@ module Osm
|
|
503
532
|
|
504
533
|
# The cached programmes for the section will be out of date - remove them
|
505
534
|
get_terms(api_data).each do |term|
|
506
|
-
|
535
|
+
cache_delete("programme-#{term.section_id}-#{term.id}") if term.section_id == evening.section_id
|
507
536
|
end
|
508
537
|
|
509
538
|
return response.is_a?(Hash) && (response['result'] == 0)
|
@@ -513,7 +542,7 @@ module Osm
|
|
513
542
|
protected
|
514
543
|
# Set access permission for the current user on a resource stored in the cache
|
515
544
|
# @param [Symbol] resource_type a symbol representing the resource type (:section, :grouping, :term, :activity, :programme, :member, :badge, :register)
|
516
|
-
# @param [
|
545
|
+
# @param [Fixnum] resource_id the id of the resource being checked
|
517
546
|
# @param [Hash] api_data the data hash used in accessing the api
|
518
547
|
# @param [Boolean] permission wether the user can access the resource
|
519
548
|
# @return [Boolean] the permission which was set
|
@@ -530,7 +559,7 @@ module Osm
|
|
530
559
|
|
531
560
|
# Get access permission for the current user on a resource stored in the cache
|
532
561
|
# @param [Symbol] resource_type a symbol representing the resource type (:section, :grouping, :term, :activity, :programme, :member, :badge, :register)
|
533
|
-
# @param [
|
562
|
+
# @param [Fixnum] resource_id the id of the resource being checked
|
534
563
|
# @param [Hash] api_data the data hash used in accessing the api
|
535
564
|
# @return nil if the combination of user and resource has not been set
|
536
565
|
# @return [Boolean] if the user can access the resource
|
@@ -569,11 +598,6 @@ module Osm
|
|
569
598
|
end
|
570
599
|
end
|
571
600
|
|
572
|
-
if Rails.env.development?
|
573
|
-
puts "Making OSM API request to #{url}"
|
574
|
-
puts api_data.to_s
|
575
|
-
end
|
576
|
-
|
577
601
|
begin
|
578
602
|
result = HTTParty.post("#{@base_url}/#{url}", {:body => api_data})
|
579
603
|
rescue SocketError, TimeoutError, OpenSSL::SSL::SSLError
|
@@ -581,11 +605,6 @@ module Osm
|
|
581
605
|
end
|
582
606
|
raise ConnectionError.new("HTTP Status code was #{result.response.code}") if !result.response.code.eql?('200')
|
583
607
|
|
584
|
-
if Rails.env.development?
|
585
|
-
puts "Result from OSM request to #{url}"
|
586
|
-
puts result.response.body
|
587
|
-
end
|
588
|
-
|
589
608
|
raise Error.new(result.response.body) unless looks_like_json?(result.response.body)
|
590
609
|
decoded = ActiveSupport::JSON.decode(result.response.body)
|
591
610
|
osm_error = get_osm_error(decoded)
|
@@ -608,10 +627,43 @@ module Osm
|
|
608
627
|
return false unless data.is_a?(Hash)
|
609
628
|
to_return = data['error'] || data['err'] || false
|
610
629
|
to_return = false if to_return.blank?
|
611
|
-
puts "OSM API ERROR: #{to_return}" if Rails.env.development? && to_return
|
612
630
|
return to_return
|
613
631
|
end
|
614
632
|
|
633
|
+
# Wrap cache calls
|
634
|
+
def cache_read(key)
|
635
|
+
return @@cache.nil? ? nil : @@cache.read("#{@@cache_prepend_to_key}-#{key}")
|
636
|
+
end
|
637
|
+
def cache_write(key, data, options={})
|
638
|
+
return @@cache.nil? ? false : @@cache.write("#{@@cache_prepend_to_key}-#{key}", data, options)
|
639
|
+
end
|
640
|
+
def cache_exist?(key)
|
641
|
+
return @@cache.nil? ? false : @@cache.exist?("#{@@cache_prepend_to_key}-#{key}")
|
642
|
+
end
|
643
|
+
def cache_delete(key)
|
644
|
+
return @@cache.nil? ? true : @@cache.delete("#{@@cache_prepend_to_key}-#{key}")
|
645
|
+
end
|
646
|
+
|
647
|
+
# Get the ID from an object or fixnum
|
648
|
+
# @param cl the Class being used (e.g. Osm::Section)
|
649
|
+
# @param value the value to get the ID from
|
650
|
+
# @param [String] error_name the name of the class to use in error messages
|
651
|
+
# @param [String, Symbol] id_method the method to call on cl to get the ID
|
652
|
+
# @return [Fixnum] the ID
|
653
|
+
def id_for(cl, value, error_name, id_method=:id)
|
654
|
+
raise(ArgumentError, "Invalid type for #{error_name}") unless value.is_a?(cl) || value.is_a?(Fixnum)
|
655
|
+
id = value.is_a?(cl) ? value.send(id_method) : value
|
656
|
+
raise(ArgumentError, "Invalid #{error_name} ID") unless id > 0
|
657
|
+
return id
|
658
|
+
end
|
659
|
+
|
660
|
+
def id_for_section(section)
|
661
|
+
id_for(Osm::Section, section, 'section')
|
662
|
+
end
|
663
|
+
def id_for_term(term)
|
664
|
+
return term.nil? ? Osm::find_current_term_id(self, section_id, api_data) : id_for(Osm::Term, term, 'term')
|
665
|
+
end
|
666
|
+
|
615
667
|
end
|
616
668
|
|
617
669
|
end
|
data/lib/osm/api_access.rb
CHANGED
data/lib/osm/evening.rb
CHANGED
@@ -5,9 +5,9 @@ module Osm
|
|
5
5
|
attr_accessor :evening_id, :section_id, :title, :notes_for_parents, :games, :pre_notes, :post_notes, :leaders, :meeting_date, :activities
|
6
6
|
attr_reader :start_time, :end_time
|
7
7
|
# @!attribute [rw] evening_id
|
8
|
-
# @return [
|
8
|
+
# @return [Fixnum] the id of the evening
|
9
9
|
# @!attribute [rw] sectionid
|
10
|
-
# @return [
|
10
|
+
# @return [Fixnum] the section the evening belongs to
|
11
11
|
# @!attribute [rw] title
|
12
12
|
# @return [String] the title of the evening
|
13
13
|
# @!attribute [rw] notes_for_parents
|
@@ -105,7 +105,7 @@ module Osm
|
|
105
105
|
|
106
106
|
attr_reader :activity_id, :title, :notes
|
107
107
|
# @!attribute [r] activity_id
|
108
|
-
# @return [
|
108
|
+
# @return [Fixnum] the activity being done
|
109
109
|
# @!attribute [r] title
|
110
110
|
# @return [String] the activity's title
|
111
111
|
# @!attribute [r] notes
|
data/lib/osm/event.rb
CHANGED
@@ -4,9 +4,9 @@ module Osm
|
|
4
4
|
|
5
5
|
attr_reader :id, :section_id, :name, :start, :end, :cost, :location, :notes
|
6
6
|
# @!attribute [r] id
|
7
|
-
# @return [
|
7
|
+
# @return [Fixnum] the id for the event
|
8
8
|
# @!attribute [r] section_id
|
9
|
-
# @return [
|
9
|
+
# @return [Fixnum] the id for the section
|
10
10
|
# @!attribute [r] name
|
11
11
|
# @return [String] the name of the event
|
12
12
|
# @!attribute [r] start
|
data/lib/osm/grouping.rb
CHANGED
@@ -4,13 +4,13 @@ module Osm
|
|
4
4
|
|
5
5
|
attr_reader :id, :name, :active, :points
|
6
6
|
# @!attribute [r] id
|
7
|
-
# @return [
|
7
|
+
# @return [Fixnum] the id for grouping
|
8
8
|
# @!attribute [r] name
|
9
9
|
# @return [String] the name of the grouping
|
10
10
|
# @!attribute [r] id
|
11
11
|
# @return [Boolean] wether the grouping is active
|
12
12
|
# @!attribute [r] points
|
13
|
-
# @return [
|
13
|
+
# @return [Fixnum] the points awarded to the grouping
|
14
14
|
|
15
15
|
# Initialize a new Grouping using the hash returned by the API call
|
16
16
|
# @param data the hash of data for the object returned by the API
|
data/lib/osm/member.rb
CHANGED
@@ -4,9 +4,9 @@ module Osm
|
|
4
4
|
|
5
5
|
attr_reader :id, :section_id, :type, :first_name, :last_name, :email1, :email2, :email3, :email4, :phone1, :phone2, :phone3, :phone4, :address, :address2, :date_of_birth, :started, :joined_in_years, :parents, :notes, :medical, :religion, :school, :ethnicity, :subs, :grouping_id, :grouping_leader, :joined, :age, :joined_years
|
6
6
|
# @!attribute [r] id
|
7
|
-
# @return [
|
7
|
+
# @return [Fixnum] the id for the member
|
8
8
|
# @!attribute [r] section_id
|
9
|
-
# @return [
|
9
|
+
# @return [Fixnum] the section the member belongs to
|
10
10
|
# @!attribute [r] type
|
11
11
|
# @return [?] ?
|
12
12
|
# @!attribute [r] first_name
|
@@ -38,7 +38,7 @@ module Osm
|
|
38
38
|
# @!attribute [r] started
|
39
39
|
# @return [Date] when the member started Scouting
|
40
40
|
# @!attribute [r] joined_in_years
|
41
|
-
# @return [
|
41
|
+
# @return [Fixnum] ?
|
42
42
|
# @!attribute [r] parents
|
43
43
|
# @return [String] the member's parent's names
|
44
44
|
# @!attribute [r] notes
|
@@ -54,15 +54,15 @@ module Osm
|
|
54
54
|
# @!attribute [r] subs
|
55
55
|
# @return [String] details about the member's subs
|
56
56
|
# @!attribute [r] grouping_id
|
57
|
-
# @return [
|
57
|
+
# @return [Fixnum] the grouping within the section that the member belongs to
|
58
58
|
# @!attribute [r] grouping_leader
|
59
|
-
# @return [
|
59
|
+
# @return [Fixnum] wether the member is the grouping leader (0=no, 1=seconder/APL, 2=sixer/PL)
|
60
60
|
# @!attribute [r] joined
|
61
61
|
# @return [Date] when the member joined the section
|
62
62
|
# @!attribute [r] age
|
63
63
|
# @return [String] the member's current age (yy/mm)
|
64
64
|
# @!attribute [r] joined_years
|
65
|
-
# @return [
|
65
|
+
# @return [Fixnum] how many years the member has been in Scouting
|
66
66
|
|
67
67
|
|
68
68
|
# Initialize a new Member using the hash returned by the API call
|
@@ -101,13 +101,13 @@ module Osm
|
|
101
101
|
end
|
102
102
|
|
103
103
|
# Get the years element of this scout's age
|
104
|
-
# @return [
|
104
|
+
# @return [Fixnum] the number of years this scout has been alive
|
105
105
|
def age_years
|
106
106
|
return @age[0..1].to_i
|
107
107
|
end
|
108
108
|
|
109
109
|
# Get the months element of this scout's age
|
110
|
-
# @return [
|
110
|
+
# @return [Fixnum] the number of months since this scout's last birthday
|
111
111
|
def age_months
|
112
112
|
return @age[-2..-1].to_i
|
113
113
|
end
|
data/lib/osm/role.rb
CHANGED
@@ -8,7 +8,7 @@ module Osm
|
|
8
8
|
# @!attribute [r] group_name
|
9
9
|
# @return [String] the name of the group the section is in
|
10
10
|
# @!attribute [r] group_id
|
11
|
-
# @return [
|
11
|
+
# @return [Fixnum] the group the section is in
|
12
12
|
# @!attribute [r] permissions
|
13
13
|
# @return [Hash] the permissions the user has in this role
|
14
14
|
|
data/lib/osm/section.rb
CHANGED
@@ -4,7 +4,7 @@ module Osm
|
|
4
4
|
|
5
5
|
attr_reader :id, :name, :subscription_level, :subscription_expires, :type, :num_scouts, :column_names, :fields, :intouch_fields, :mobile_fields, :extra_records, :role
|
6
6
|
# @!attribute [r] id
|
7
|
-
# @return [
|
7
|
+
# @return [Fixnum] the id for the section
|
8
8
|
# @!attribute [r] name
|
9
9
|
# @return [String] the section name
|
10
10
|
# @!attribute [r] subscription_level
|
@@ -14,7 +14,7 @@ module Osm
|
|
14
14
|
# @!attribute [r] type
|
15
15
|
# @return [Symbol] the section type (:beavers, :cubs, :scouts, :exporers, :adults, :waiting, :unknown)
|
16
16
|
# @!attribute [r] num_scouts
|
17
|
-
# @return [
|
17
|
+
# @return [Fixnum] how many members the section has
|
18
18
|
# @!attribute [r] column_names
|
19
19
|
# @return [Hash] custom names to use for the data columns
|
20
20
|
# @!attribute [r] fields
|
@@ -31,7 +31,9 @@ module Osm
|
|
31
31
|
|
32
32
|
# Initialize a new SectionConfig using the hash returned by the API call
|
33
33
|
# @param id the section ID used by the API to refer to this section
|
34
|
+
# @param name the name given to the sction in OSM
|
34
35
|
# @param data the hash of data for the object returned by the API
|
36
|
+
# @param role the Osm::Role linked with this section
|
35
37
|
def initialize(id, name, data, role)
|
36
38
|
subscription_levels = [:bronze, :silver, :gold]
|
37
39
|
subscription_level = data['subscription_level'].to_i - 1
|
@@ -51,8 +53,8 @@ module Osm
|
|
51
53
|
|
52
54
|
# Symbolise the keys in each hash of the extra_records array
|
53
55
|
@extra_records.each do |item|
|
54
|
-
# Expect item to be: {:name=>String, :extraid=>
|
55
|
-
# Sometimes get item as: [String, {"name"=>String, "extraid"=>
|
56
|
+
# Expect item to be: {:name=>String, :extraid=>Fixnum}
|
57
|
+
# Sometimes get item as: [String, {"name"=>String, "extraid"=>Fixnum}]
|
56
58
|
if item.is_a?(Array)
|
57
59
|
item = Osm::symbolize_hash(item[1])
|
58
60
|
else
|
data/lib/osm/term.rb
CHANGED
@@ -4,11 +4,11 @@ module Osm
|
|
4
4
|
|
5
5
|
attr_reader :id, :section_id, :name, :start, :end
|
6
6
|
# @!attribute [r] id
|
7
|
-
# @return [
|
7
|
+
# @return [Fixnum] the id for the term
|
8
8
|
# @!attribute [r] section_id
|
9
|
-
# @return [
|
9
|
+
# @return [Fixnum] the section the term belongs to
|
10
10
|
# @!attribute [r] name
|
11
|
-
# @return [
|
11
|
+
# @return [Fixnum] the name of the term
|
12
12
|
# @!attribute [r] start
|
13
13
|
# @return [Date] when the term starts
|
14
14
|
# @!attribute [r] end
|
data/osm.gemspec
CHANGED
@@ -18,9 +18,8 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
s.add_runtime_dependency 'rails', '>= 3.2.5' # Used to provide the cache (and the environment controls slightly different behavior in perform_query)
|
22
21
|
s.add_runtime_dependency 'activesupport', '>= 3.2' # Used to parse JSON from OSM
|
23
|
-
s.add_runtime_dependency 'httparty'
|
22
|
+
s.add_runtime_dependency 'httparty' # Used to make web requests to the API
|
24
23
|
|
25
24
|
s.add_development_dependency 'rake'
|
26
25
|
s.add_development_dependency 'rspec'
|
data/spec/osm/api_spec.rb
CHANGED
@@ -31,6 +31,7 @@ describe "API" do
|
|
31
31
|
:api_token => 'API TOKEN',
|
32
32
|
:api_name => 'API NAME',
|
33
33
|
:api_site => :scout,
|
34
|
+
:cache => OsmTest::Cache,
|
34
35
|
}.freeze
|
35
36
|
Osm::Api.configure(@api_config)
|
36
37
|
end
|
@@ -111,6 +112,17 @@ describe "API" do
|
|
111
112
|
it "Fetch the notepad for a section" do
|
112
113
|
FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads", :body => {"1" => "Section 1", "2" => "Section 2"}.to_json)
|
113
114
|
Osm::Api.new('1', '2').get_notepad(1).should == 'Section 1'
|
115
|
+
Osm::Api.new('1', '2').get_notepad(Osm::Section.new(2, '', {}, nil)).should == 'Section 2'
|
116
|
+
end
|
117
|
+
|
118
|
+
it "Fetch the notepad for a section (invalid section/id)" do
|
119
|
+
FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads", :body => {"1" => "Section 1"}.to_json)
|
120
|
+
|
121
|
+
expect{ Osm::Api.new('1', '2').get_notepad(-10) }.to raise_error(ArgumentError, 'Invalid section ID')
|
122
|
+
expect{ Osm::Api.new('1', '2').get_notepad(0) }.to raise_error(ArgumentError, 'Invalid section ID')
|
123
|
+
|
124
|
+
expect{ Osm::Api.new('1', '2').get_notepad(Osm::Section.new(-10, '', {}, nil)) }.to raise_error(ArgumentError, 'Invalid section ID')
|
125
|
+
expect{ Osm::Api.new('1', '2').get_notepad('1') }.to raise_error(ArgumentError, 'Invalid type for section')
|
114
126
|
end
|
115
127
|
|
116
128
|
|
@@ -531,6 +543,18 @@ describe "API" do
|
|
531
543
|
HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"1":"New content."}'}) }
|
532
544
|
Osm::Api.new('user', 'secret').get_notepad(1, {:no_cache => true}).should == 'New content.'
|
533
545
|
end
|
546
|
+
|
547
|
+
|
548
|
+
it "Uses the provided cache_prepend_to_key text" do
|
549
|
+
FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getNotepads", :body => {"1" => "Section 1"}.to_json)
|
550
|
+
|
551
|
+
OsmTest::Cache.should_receive('exist?').with('OSMAPI-notepads-1')
|
552
|
+
Osm::Api.new('1', '2').get_notepads.should == {1 => 'Section 1'}
|
553
|
+
|
554
|
+
Osm::Api.configure(@api_config.merge(:cache_prepend_to_key => 'AB'))
|
555
|
+
OsmTest::Cache.should_receive('exist?').with('AB-notepads-1')
|
556
|
+
Osm::Api.new('1', '2').get_notepads.should == {1 => 'Section 1'}
|
557
|
+
end
|
534
558
|
end
|
535
559
|
|
536
560
|
|
data/spec/spec_helper.rb
CHANGED
@@ -19,40 +19,28 @@ RSpec.configure do |config|
|
|
19
19
|
|
20
20
|
config.before(:each) do
|
21
21
|
FakeWeb.clean_registry
|
22
|
-
|
22
|
+
OsmTest::Cache.clear
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
|
27
|
-
module
|
28
|
-
def self.cache
|
29
|
-
@cache ||= Cache.new
|
30
|
-
end
|
31
|
-
def self.env
|
32
|
-
Env.new
|
33
|
-
end
|
34
|
-
|
35
|
-
class Env
|
36
|
-
def development?
|
37
|
-
false
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
27
|
+
module OsmTest
|
41
28
|
class Cache
|
42
|
-
|
43
|
-
|
29
|
+
@@cache = {}
|
30
|
+
def self.write(key, data, options={})
|
31
|
+
@@cache[key] = data
|
44
32
|
end
|
45
|
-
def
|
46
|
-
|
33
|
+
def self.read(key)
|
34
|
+
@@cache[key]
|
47
35
|
end
|
48
|
-
def
|
49
|
-
|
36
|
+
def self.exist?(key)
|
37
|
+
@@cache.include?(key)
|
50
38
|
end
|
51
|
-
def
|
52
|
-
|
39
|
+
def self.delete(key)
|
40
|
+
@@cache.delete(key)
|
53
41
|
end
|
54
|
-
def clear
|
55
|
-
|
42
|
+
def self.clear
|
43
|
+
@@cache = {}
|
56
44
|
end
|
57
45
|
end
|
58
46
|
end
|
data/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: osm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,22 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-19 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: rails
|
16
|
-
requirement: &76581020 !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: 3.2.5
|
22
|
-
type: :runtime
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: *76581020
|
25
14
|
- !ruby/object:Gem::Dependency
|
26
15
|
name: activesupport
|
27
|
-
requirement: &
|
16
|
+
requirement: &85718650 !ruby/object:Gem::Requirement
|
28
17
|
none: false
|
29
18
|
requirements:
|
30
19
|
- - ! '>='
|
@@ -32,10 +21,10 @@ dependencies:
|
|
32
21
|
version: '3.2'
|
33
22
|
type: :runtime
|
34
23
|
prerelease: false
|
35
|
-
version_requirements: *
|
24
|
+
version_requirements: *85718650
|
36
25
|
- !ruby/object:Gem::Dependency
|
37
26
|
name: httparty
|
38
|
-
requirement: &
|
27
|
+
requirement: &85718390 !ruby/object:Gem::Requirement
|
39
28
|
none: false
|
40
29
|
requirements:
|
41
30
|
- - ! '>='
|
@@ -43,10 +32,10 @@ dependencies:
|
|
43
32
|
version: '0'
|
44
33
|
type: :runtime
|
45
34
|
prerelease: false
|
46
|
-
version_requirements: *
|
35
|
+
version_requirements: *85718390
|
47
36
|
- !ruby/object:Gem::Dependency
|
48
37
|
name: rake
|
49
|
-
requirement: &
|
38
|
+
requirement: &85718050 !ruby/object:Gem::Requirement
|
50
39
|
none: false
|
51
40
|
requirements:
|
52
41
|
- - ! '>='
|
@@ -54,10 +43,10 @@ dependencies:
|
|
54
43
|
version: '0'
|
55
44
|
type: :development
|
56
45
|
prerelease: false
|
57
|
-
version_requirements: *
|
46
|
+
version_requirements: *85718050
|
58
47
|
- !ruby/object:Gem::Dependency
|
59
48
|
name: rspec
|
60
|
-
requirement: &
|
49
|
+
requirement: &85715520 !ruby/object:Gem::Requirement
|
61
50
|
none: false
|
62
51
|
requirements:
|
63
52
|
- - ! '>='
|
@@ -65,10 +54,10 @@ dependencies:
|
|
65
54
|
version: '0'
|
66
55
|
type: :development
|
67
56
|
prerelease: false
|
68
|
-
version_requirements: *
|
57
|
+
version_requirements: *85715520
|
69
58
|
- !ruby/object:Gem::Dependency
|
70
59
|
name: fakeweb
|
71
|
-
requirement: &
|
60
|
+
requirement: &85714410 !ruby/object:Gem::Requirement
|
72
61
|
none: false
|
73
62
|
requirements:
|
74
63
|
- - ! '>='
|
@@ -76,7 +65,7 @@ dependencies:
|
|
76
65
|
version: '0'
|
77
66
|
type: :development
|
78
67
|
prerelease: false
|
79
|
-
version_requirements: *
|
68
|
+
version_requirements: *85714410
|
80
69
|
description: Use the Online Scout Manager API (https://www.onlinescoutmanager.co.uk)
|
81
70
|
to retrieve and save data.
|
82
71
|
email:
|