osm 1.2.10 → 1.2.11

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZWU5MjViOTkxMDVjZDlkYjkwM2EzZDNkNDg1YzRiZjk4OGQxMmU2Mg==
4
+ NzVhNDE1ZTEwZGVlNDJkNjYzNjFlY2JjMGVmOThlNzExZDc0NWU1NA==
5
5
  data.tar.gz: !binary |-
6
- YjkzMjgzNGQyMjA2NjFkN2RhOTU1YjFkYzY1MzhkMzFlZTI5YTJjMA==
6
+ ZjI1YmNmOTEyMjFkMjJkYzUzZjI1YzUyNTUxYzRlN2E1ZmFlMzY0ZQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ODRiY2RiNjE2NjUxMGJhY2U2MDZhYzI2ZjAwYzQxOWFiNjYxN2ZjNzI1NzUy
10
- ZWI0NGFlNjk3Zjc4NTE3MTlkZmVhNzVkYzAxMDkzNDE0YmRiZjc1MGExM2U3
11
- YzhmNjhlYTJjOWRmZmE5ZmFkNjI4NGU4YWRiMTA2NjE3YTk2NzU=
9
+ ZGJhZjk3MDAyZTNhOTg0MjNkMTczMWE0MTc3ZDJlNmU4MDdkM2IyOTM2YTBi
10
+ MGIzNDllMDU2MzViNjQzMzcwYzEyN2ZmZmM1NmM4MjVkNmE0ZDZhZGRmY2Q3
11
+ MjkxNzE5ZDQwZTZlMzM0ZWU5NTIzZmRmNTA1N2RkZWUxN2JjYjA=
12
12
  data.tar.gz: !binary |-
13
- YTUxNzMzMTlhOTRhMDViNzNhYjU2MDNiYzM1YzQ2NjY4ZjFiMzQzMTRlYjc0
14
- NDY1ZmU3MDRjMjI3ZTc2ODNhNjg2OWZkZjk1YmEzYWUyMWE3NGRlYmRiYjQw
15
- ZWYwMjcxODcyYjY0ZGVhOTZhZWZjMTM3ZTg4YWE1YWQzNDA3ZTg=
13
+ MzcyMjU0OTczNjc3YmVmNmVjODA4YjI1N2ViZTcwMmU0MTQwYzg1YTMxMjcz
14
+ MDJhMjJkOTBlMDBmNTRkOTRkZWI4M2MwOGZiMzBmN2UyMzYzMDM1OTk4MWE3
15
+ NjgzNTk3YTg3N2I5YTA4MGI5YmZhYmQxZDYyODA2NmVhNWVjY2I=
@@ -1,3 +1,11 @@
1
+ ## Version 1.2.11
2
+
3
+ * Fix handling blank event config from OSM (JSON parse error)
4
+ * Add retrieving of badge links to events
5
+ * When creating an event in OSM badge links are also added
6
+ * When updating an event in OSM badge links are also updated
7
+ * Add get\_list method to Event (get basic event details, should cutout the need to get all events all the time)
8
+
1
9
  ## Version 1.2.10
2
10
 
3
11
  * Update levels for Hikes and Nights away staged badges (released April 2014)
@@ -264,6 +264,7 @@ module Osm
264
264
  # @return [String] the error message
265
265
  def self.get_osm_error(data)
266
266
  return false unless data.is_a?(Hash)
267
+ return false if data['ok']
267
268
  to_return = data['error'] || data['err'] || false
268
269
  to_return = false if to_return.blank?
269
270
  return to_return
@@ -1,8 +1,12 @@
1
1
  module Osm
2
2
 
3
3
  class Event < Osm::Model
4
+ class BadgeLink < Osm::Model; end # Ensure the constant exists for the validators
4
5
  class Column < Osm::Model; end # Ensure the constant exists for the validators
5
6
 
7
+ LIST_ATTRIBUTES = [:id, :section_id, :name, :start, :finish, :cost, :location, :notes, :archived, :public_notepad, :confirm_by_date, :allow_changes, :reminders, :attendance_limit, :attendance_limit_includes_leaders, :attendance_reminder, :allow_booking]
8
+ EXTRA_ATTRIBUTES = [:notepad, :columns, :badges]
9
+
6
10
  # @!attribute [rw] id
7
11
  # @return [Fixnum] the id for the event
8
12
  # @!attribute [rw] section_id
@@ -21,6 +25,8 @@ module Osm
21
25
  # @return [String] notes about the event
22
26
  # @!attribute [rw] archived
23
27
  # @return [Boolean] if the event has been archived
28
+ # @!attribute [rw] badges
29
+ # @return [Array<Osm::Event::BadgeLink>] the badge links for the event
24
30
  # @!attribute [rw] columns
25
31
  # @return [Array<Osm::Event::Column>] the custom columns for the event
26
32
  # @!attribute [rw] notepad
@@ -51,6 +57,7 @@ module Osm
51
57
  attribute :location, :type => String, :default => ''
52
58
  attribute :notes, :type => String, :default => ''
53
59
  attribute :archived, :type => Boolean, :default => false
60
+ attribute :badges, :default => []
54
61
  attribute :columns, :default => []
55
62
  attribute :notepad, :type => String, :default => ''
56
63
  attribute :public_notepad, :type => String, :default => ''
@@ -64,7 +71,7 @@ module Osm
64
71
 
65
72
  if ActiveModel::VERSION::MAJOR < 4
66
73
  attr_accessible :id, :section_id, :name, :start, :finish, :cost, :location, :notes, :archived,
67
- :fields, :columns, :notepad, :public_notepad, :confirm_by_date, :allow_changes,
74
+ :fields, :badges, :columns, :notepad, :public_notepad, :confirm_by_date, :allow_changes,
68
75
  :reminders, :attendance_limit, :attendance_limit_includes_leaders,
69
76
  :attendance_reminder, :allow_booking
70
77
  end
@@ -73,6 +80,7 @@ module Osm
73
80
  validates_numericality_of :section_id, :only_integer=>true, :greater_than=>0
74
81
  validates_numericality_of :attendance_limit, :only_integer=>true, :greater_than_or_equal_to=>0
75
82
  validates_presence_of :name
83
+ validates :badges, :array_of => {:item_type => Osm::Event::BadgeLink, :item_valid => true}
76
84
  validates :columns, :array_of => {:item_type => Osm::Event::Column, :item_valid => true}
77
85
  validates_inclusion_of :allow_changes, :in => [true, false]
78
86
  validates_inclusion_of :reminders, :in => [true, false]
@@ -126,6 +134,52 @@ module Osm
126
134
  end
127
135
  end
128
136
 
137
+ # Get event list for a section (not all details for each event)
138
+ # @param [Osm::Api] api The api to use to make the request
139
+ # @param [Osm::Section, Fixnum, #to_i] section The section (or its ID) to get the events for
140
+ # @!macro options_get
141
+ # @return [Array<Hash>]
142
+ def self.get_list(api, section, options={})
143
+ require_ability_to(api, :read, :events, section, options)
144
+ section_id = section.to_i
145
+ cache_key = ['events_list', section_id]
146
+ events_cache_key = ['events', section_id]
147
+ events = nil
148
+
149
+ unless options[:no_cache]
150
+
151
+ # Try getting from cache
152
+ if cache_exist?(api, cache_key)
153
+ return cache_read(api, cache_key)
154
+ end
155
+
156
+ # Try generating from cached events
157
+ if cache_exist?(api, events_cache_key)
158
+ ids = cache_read(api, events_cache_key)
159
+ events = get_from_ids(api, ids, 'event', section, options, :get_for_section).map do |e|
160
+ e.attributes.symbolize_keys.select do |k,v|
161
+ LIST_ATTRIBUTES.include?(k)
162
+ end
163
+ end
164
+ end
165
+
166
+ end
167
+
168
+ # Fetch from OSM
169
+ if events.nil?
170
+ data = api.perform_query("events.php?action=getEvents&sectionid=#{section_id}&showArchived=true")
171
+ events = Array.new
172
+ unless data['items'].nil?
173
+ data['items'].map do |event_data|
174
+ events.push(attributes_from_data(event_data))
175
+ end
176
+ end
177
+ end
178
+
179
+ cache_write(api, cache_key, events)
180
+ return events
181
+ end
182
+
129
183
  # Get an event
130
184
  # @param [Osm::Api] api The api to use to make the request
131
185
  # @param [Osm::Section, Fixnum, #to_i] section The section (or its ID) to get the events for
@@ -148,6 +202,7 @@ module Osm
148
202
 
149
203
 
150
204
  # Create an event in OSM
205
+ # If something goes wrong adding badges to OSM then the event returned will have been read from OSM
151
206
  # @param [Osm::Api] api The api to use to make the request
152
207
  # @return [Osm::Event, nil] the created event, nil if failed
153
208
  # @raise [Osm::ObjectIsInvalid] If the Event is invalid
@@ -174,13 +229,33 @@ module Osm
174
229
  'allowbooking' => event.allow_booking ? 'true' : 'false',
175
230
  })
176
231
 
177
- # The cached events for the section will be out of date - remove them
178
- cache_delete(api, ['events', event.section_id])
179
- cache_write(api, ['event', event.id], event)
180
-
181
232
  if (data.is_a?(Hash) && data.has_key?('id'))
182
233
  event.id = data['id'].to_i
183
- return event
234
+
235
+ # The cached events for the section will be out of date - remove them
236
+ cache_delete(api, ['events', event.section_id])
237
+ cache_write(api, ['event', event.id], event)
238
+
239
+ # Add badge links to OSM
240
+ badges_created = true
241
+ event.badges.each do |badge|
242
+ badge_data = data = api.perform_query("ext/events/event/index.php?action=badgeAddToEvent&sectionid=#{event.section_id}&eventid=#{event.id}", {
243
+ 'section' => badge.badge_section,
244
+ 'badgetype' => badge.badge_type,
245
+ 'badge' => badge.badge_key,
246
+ 'columnname' => badge.requirement_key,
247
+ 'data' => badge.data,
248
+ 'newcolumnname' => badge.label,
249
+ })
250
+ badges_created = false unless badge_data.is_a?(Hash) && badge_data['ok']
251
+ end
252
+
253
+ if badges_created
254
+ return event
255
+ else
256
+ # Someting went wrong adding badges so return what OSM has
257
+ return get(api, event.section_id, event.id)
258
+ end
184
259
  else
185
260
  return nil
186
261
  end
@@ -188,42 +263,107 @@ module Osm
188
263
 
189
264
  # Update event in OSM
190
265
  # @param [Osm::Api] api The api to use to make the request
191
- # @return [Boolean] whether the update succedded
266
+ # @return [Boolean] whether the update succedded (will return true if no updates needed to be made)
192
267
  def update(api)
193
268
  require_ability_to(api, :write, :events, section_id)
269
+ updated = true
270
+
271
+ # Update main attributes
272
+ update_main_attributes = false
273
+ %w{ id name location start finish cost cost_tbc notes confirm_by_date allow_changes reminders attendance_limit attendance_limit_includes_leaders allow_booking }.each do |a|
274
+ if changed_attributes.include?(a)
275
+ update_main_attributes = true
276
+ break # no use checking the others
277
+ end
278
+ end
279
+ if update_main_attributes
280
+ data = api.perform_query("events.php?action=addEvent&sectionid=#{section_id}", {
281
+ 'eventid' => id,
282
+ 'name' => name,
283
+ 'location' => location,
284
+ 'startdate' => start? ? start.strftime(Osm::OSM_DATE_FORMAT) : '',
285
+ 'enddate' => finish? ? finish.strftime(Osm::OSM_DATE_FORMAT) : '',
286
+ 'cost' => cost_tbc? ? '-1' : cost,
287
+ 'notes' => notes,
288
+ 'starttime' => start? ? start.strftime(Osm::OSM_TIME_FORMAT) : '',
289
+ 'endtime' => finish? ? finish.strftime(Osm::OSM_TIME_FORMAT) : '',
290
+ 'confdate' => confirm_by_date? ? confirm_by_date.strftime(Osm::OSM_DATE_FORMAT) : '',
291
+ 'allowChanges' => allow_changes ? 'true' : 'false',
292
+ 'disablereminders' => !reminders ? 'true' : 'false',
293
+ 'attendancelimit' => attendance_limit,
294
+ 'attendancereminder' => attendance_reminder,
295
+ 'limitincludesleaders' => attendance_limit_includes_leaders ? 'true' : 'false',
296
+ 'allowbooking' => allow_booking ? 'true' : 'false',
297
+ })
298
+ updated &= data.is_a?(Hash) && (data['id'].to_i == id)
299
+ end
194
300
 
195
- to_update = changed_attributes
196
-
197
- data = api.perform_query("events.php?action=addEvent&sectionid=#{section_id}", {
198
- 'eventid' => id,
199
- 'name' => name,
200
- 'location' => location,
201
- 'startdate' => start? ? start.strftime(Osm::OSM_DATE_FORMAT) : '',
202
- 'enddate' => finish? ? finish.strftime(Osm::OSM_DATE_FORMAT) : '',
203
- 'cost' => cost_tbc? ? '-1' : cost,
204
- 'notes' => notes,
205
- 'starttime' => start? ? start.strftime(Osm::OSM_TIME_FORMAT) : '',
206
- 'endtime' => finish? ? finish.strftime(Osm::OSM_TIME_FORMAT) : '',
207
- 'confdate' => confirm_by_date? ? confirm_by_date.strftime(Osm::OSM_DATE_FORMAT) : '',
208
- 'allowChanges' => allow_changes ? 'true' : 'false',
209
- 'disablereminders' => !reminders ? 'true' : 'false',
210
- 'attendancelimit' => attendance_limit,
211
- 'attendancereminder' => attendance_reminder,
212
- 'limitincludesleaders' => attendance_limit_includes_leaders ? 'true' : 'false',
213
- 'allowbooking' => allow_booking ? 'true' : 'false',
214
- })
301
+ # Private notepad
302
+ if changed_attributes.include?('notepad')
303
+ data = api.perform_query("events.php?action=saveNotepad&sectionid=#{section_id}", {
304
+ 'eventid' => id,
305
+ 'notepad' => notepad,
306
+ })
307
+ updated &= data.is_a?(Hash)
308
+ end
215
309
 
216
- api.perform_query("events.php?action=saveNotepad&sectionid=#{section_id}", {
217
- 'eventid' => id,
218
- 'notepad' => notepad,
219
- }) if to_update.include?('notepad')
310
+ # MySCOUT notepad
311
+ if changed_attributes.include?('public_notepad')
312
+ data = api.perform_query("events.php?action=saveNotepad&sectionid=#{section_id}", {
313
+ 'eventid' => id,
314
+ 'pnnotepad' => public_notepad,
315
+ })
316
+ updated &= data.is_a?(Hash)
317
+ end
318
+
319
+ # Badges
320
+ if changed_attributes.include?('badges')
321
+ original_badges = @original_attributes['badges'] || []
322
+
323
+ # Deleted badges
324
+ badges_to_delete = []
325
+ original_badges.each do |badge|
326
+ unless badges.include?(badge)
327
+ badges_to_delete.push({
328
+ 'section' => badge.badge_section,
329
+ 'badge' => badge.badge_key,
330
+ 'columnname' => badge.requirement_key,
331
+ 'data' => badge.data,
332
+ 'newcolumnname' => badge.label,
333
+ 'badgetype' => badge.badge_type,
334
+ })
335
+ end
336
+ end
337
+ unless badges_to_delete.empty?
338
+ data = api.perform_query("ext/events/event/index.php?action=badgeDeleteFromEvent&sectionid=#{section_id}&eventid=#{id}", {
339
+ 'badgelinks' => badges_to_delete,
340
+ })
341
+ updated &= data.is_a?(Hash) && data['ok']
342
+ end
220
343
 
221
- api.perform_query("events.php?action=saveNotepad&sectionid=#{section_id}", {
222
- 'eventid' => id,
223
- 'pnnotepad' => public_notepad,
224
- }) if to_update.include?('public_notepad')
344
+ # Added badges
345
+ badges_to_add = []
346
+ badges.each do |badge|
347
+ unless original_badges.include?(badge)
348
+ badges_to_add.push({
349
+ 'section' => badge.badge_section,
350
+ 'badge' => badge.badge_key,
351
+ 'columnname' => badge.requirement_key,
352
+ 'data' => badge.data,
353
+ 'newcolumnname' => badge.label,
354
+ 'badgetype' => badge.badge_type,
355
+ })
356
+ end
357
+ end
358
+ unless badges_to_add.empty?
359
+ data = api.perform_query("ext/events/event/index.php?action=badgeAddToEvent&sectionid=#{section_id}&eventid=#{id}", {
360
+ 'badgelinks' => badges_to_add,
361
+ })
362
+ updated &= data.is_a?(Hash) && data['ok']
363
+ end
364
+ end # includes badges
225
365
 
226
- if data.is_a?(Hash) && (data['id'].to_i == id)
366
+ if updated
227
367
  reset_changed_attributes
228
368
  # The cached event will be out of date - remove it
229
369
  cache_delete(api, ['event', id])
@@ -383,8 +523,8 @@ module Osm
383
523
  return attendees
384
524
  end
385
525
 
386
- def self.new_event_from_data(event_data)
387
- event = Osm::Event.new(
526
+ def self.attributes_from_data(event_data)
527
+ {
388
528
  :id => Osm::to_i_or_nil(event_data['eventid']),
389
529
  :section_id => Osm::to_i_or_nil(event_data['sectionid']),
390
530
  :name => event_data['name'],
@@ -394,7 +534,6 @@ module Osm
394
534
  :location => event_data['location'],
395
535
  :notes => event_data['notes'],
396
536
  :archived => event_data['archived'].eql?('1'),
397
- :notepad => event_data['notepad'],
398
537
  :public_notepad => event_data['publicnotes'],
399
538
  :confirm_by_date => Osm::parse_date(event_data['confdate']),
400
539
  :allow_changes => event_data['allowchanges'].eql?('1'),
@@ -403,19 +542,87 @@ module Osm
403
542
  :attendance_limit_includes_leaders => event_data['limitincludesleaders'].eql?('1'),
404
543
  :attendance_reminder => event_data['attendancereminder'].to_i,
405
544
  :allow_booking => event_data['allowbooking'].eql?('1'),
406
- )
545
+ }
546
+ end
547
+
548
+ def self.new_event_from_data(event_data)
549
+ event = Osm::Event.new(attributes_from_data(event_data))
550
+ event.notepad = event_data['notepad']
407
551
 
408
552
  columns = []
409
- column_data = ActiveSupport::JSON.decode(event_data['config'] || '[]')
553
+ config_raw = event_data['config']
554
+ config_raw = '[]' if config_raw.blank?
555
+ column_data = ActiveSupport::JSON.decode(config_raw)
410
556
  column_data = [] unless column_data.is_a?(Array)
411
557
  column_data.each do |field|
412
558
  columns.push Column.new(:id => field['id'], :name => field['name'], :label => field['pL'], :parent_required => field['pR'].to_s.eql?('1'), :event => event)
413
559
  end
414
560
  event.columns = columns
561
+
562
+ badges = []
563
+ badges_data = event_data['badgelinks']
564
+ badges_data = [] unless badges_data.is_a?(Array)
565
+ badges_data.each do |field|
566
+ badges.push BadgeLink.new(badge_key: field['badge'], badge_type: field['badgetype'].to_sym, badge_section: field['section'].to_sym, requirement_key: field['columnname'], label: field['columnnameLongName'], data: field['data'])
567
+ end
568
+ event.badges = badges
569
+
415
570
  return event
416
571
  end
417
572
 
418
573
 
574
+ # When creating a BadgeLink for an existing column in a hikes/nights badge the label is optional
575
+ # When creating a BadgeLink for a new column in a hikes/nights badge the requirement_key MUST be blank
576
+ # TODO : Add validation for above statements
577
+ class BadgeLink
578
+ include ActiveModel::MassAssignmentSecurity if ActiveModel::VERSION::MAJOR < 4
579
+ include ActiveAttr::Model
580
+
581
+ # @!attribute [rw] badge_key
582
+ # @return [String] the badge being done
583
+ # @!attribute [rw] badge_type
584
+ # @return [Symbol] the type of badge
585
+ # @!attribute [rw] requirement_key
586
+ # @return [String] the requirement being done
587
+ # @!attribute [rw] badge_section
588
+ # @return [Symbol] the section type that the badge belongs to
589
+ # @!attribute [rw] label
590
+ # @return [String] human firendly label for the badge and requirement
591
+ # @!attribute [rw] data
592
+ # @return [String] what to put in the column when the badge records are updated
593
+
594
+ attribute :badge_key, :type => String
595
+ attribute :badge_type, :type => Object
596
+ attribute :requirement_key, :type => String
597
+ attribute :badge_section, :type => Object
598
+ attribute :label, :type => String
599
+ attribute :data, :type => String
600
+
601
+ if ActiveModel::VERSION::MAJOR < 4
602
+ attr_accessible :badge_key, :badge_type, :requirement_key, :badge_section, :label, :data
603
+ end
604
+
605
+ validates_presence_of :badge_key
606
+ validates_format_of :requirement_key, :with => /\A(?:[a-z]_\d{2})|(?:custom_\d+)\Z/, :allow_blank => true, :message => 'is not in the correct format (e.g. "a_01")'
607
+ validates_inclusion_of :badge_section, :in => [:beavers, :cubs, :scouts, :explorers, :staged]
608
+ validates_inclusion_of :badge_type, :in => [:core, :staged, :activity, :challenge]
609
+
610
+ # @!method initialize
611
+ # Initialize a new Meeting::Activity
612
+ # @param [Hash] attributes The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
613
+
614
+ # Compare BadgeLink based on section, type, key, requirement, data
615
+ def <=>(another)
616
+ [:badge_section, :badge_type, :badge_key, :requirement_key].each do |attribute|
617
+ result = self.try(:data) <=> another.try(:data)
618
+ return result unless result == 0
619
+ end
620
+ return self.try(:data) <=> another.try(:data)
621
+ end
622
+
623
+ end # Class Event::BadgeLink
624
+
625
+
419
626
  class Column < Osm::Model
420
627
  # @!attribute [rw] id
421
628
  # @return [String] OSM id for the column
@@ -377,7 +377,7 @@ module Osm
377
377
 
378
378
  validates_presence_of :badge_key
379
379
  validates_format_of :requirement_key, :with => /\A[a-z]_\d{2}\Z/, :message => 'is not in the correct format (e.g. "a_01")'
380
- validates_inclusion_of :badge_section, :in => [:beavers, :cubs, :scouts, :explorers]
380
+ validates_inclusion_of :badge_section, :in => [:beavers, :cubs, :scouts, :explorers, :staged]
381
381
  validates_inclusion_of :badge_type, :in => [:core, :staged, :activity, :challenge]
382
382
 
383
383
  # @!method initialize
@@ -74,7 +74,11 @@ module Osm
74
74
 
75
75
  # Reset the list of attributes which have changed
76
76
  def reset_changed_attributes
77
- @original_attributes = attributes
77
+ classes_to_clone = [Array, Hash]
78
+ attributes_now = attributes.map do |k,v|
79
+ [k, (classes_to_clone.include?(v.class) ? v.clone : v)]
80
+ end # Deep(ish) clone
81
+ @original_attributes = Hash[attributes_now]
78
82
  end
79
83
 
80
84
 
@@ -82,7 +86,7 @@ module Osm
82
86
  old_initialize = instance_method(:initialize)
83
87
  define_method :initialize do |*args|
84
88
  ret_val = old_initialize.bind(self).call(*args)
85
- @original_attributes = attributes
89
+ reset_changed_attributes
86
90
  return ret_val
87
91
  end
88
92
 
@@ -252,7 +256,7 @@ module Osm
252
256
  # @param [Symbol] get_all_method The method to get all items (either :get_all or :get_for_section)
253
257
  # @return [Array] An array of the items
254
258
  def self.get_from_ids(api, ids, key, arguments=[], options, get_all_method)
255
- raise ArgumentError, "get_al_method is invalid" unless [:get_all, :get_for_section].include?(get_all_method)
259
+ raise ArgumentError, "get_all_method is invalid" unless [:get_all, :get_for_section].include?(get_all_method)
256
260
  items = Array.new
257
261
  ids.each do |id|
258
262
  if cache_exist?(api, [key, id])
@@ -15,6 +15,7 @@ describe "Event" do
15
15
  :location => 'Somewhere',
16
16
  :notes => 'None',
17
17
  :archived => '0',
18
+ :badges => [],
18
19
  :columns => [],
19
20
  :notepad => 'notepad',
20
21
  :public_notepad => 'public notepad',
@@ -37,6 +38,7 @@ describe "Event" do
37
38
  event.location.should == 'Somewhere'
38
39
  event.notes.should == 'None'
39
40
  event.archived.should be_false
41
+ event.badges.should == []
40
42
  event.columns.should == []
41
43
  event.notepad.should == 'notepad'
42
44
  event.public_notepad.should == 'public notepad'
@@ -114,7 +116,30 @@ describe "Event" do
114
116
 
115
117
  event_attendances.sort.should == [ea1, ea2, ea3]
116
118
  end
119
+
120
+ end
117
121
 
122
+ describe "Event::BadgeLink" do
123
+
124
+ it "Create" do
125
+ bl = Osm::Event::BadgeLink.new(
126
+ :badge_key => 'artist',
127
+ :badge_type => :activity,
128
+ :requirement_key => 'a_01',
129
+ :badge_section => :cubs,
130
+ :label => 'Cubs Artist Activity - A: Poster',
131
+ :data => 'abc'
132
+ )
133
+
134
+ bl.badge_key.should == 'artist'
135
+ bl.badge_type.should == :activity
136
+ bl.requirement_key.should == 'a_01'
137
+ bl.badge_section.should == :cubs
138
+ bl.label.should == 'Cubs Artist Activity - A: Poster'
139
+ bl.data.should == 'abc'
140
+ bl.valid?.should be_true
141
+ end
142
+
118
143
  end
119
144
 
120
145
  describe "Using the API" do
@@ -159,6 +184,10 @@ describe "Event" do
159
184
  'notepad' => 'notepad',
160
185
  'publicnotes' => 'public notepad',
161
186
  'config' => '[{"id":"f_1","name":"Name","pL":"Label","pR":"1"}]',
187
+ 'badgelinks' => [
188
+ {"section"=>"cubs", "badgetype"=>"activity", "badge"=>"athletics", "columnname"=>"b_06", "data"=>"", "badgeLongName"=>"Athletics", "columnnameLongName"=>"B: Run", "sectionLongName"=>"Cubs", "badgetypeLongName"=>"Activity"},
189
+ {"section"=>"staged", "badgetype"=>"staged", "badge"=>"hikes", "columnname"=>"custom_69153", "data"=>"1", "badgeLongName"=>"Hikes", "columnnameLongName"=>"C: Hike name = 1", "sectionLongName"=>"Staged", "badgetypeLongName"=>"Staged"},
190
+ ],
162
191
  'sectionid' => '1',
163
192
  'googlecalendar' => nil,
164
193
  'archived' => '0',
@@ -206,9 +235,27 @@ describe "Event" do
206
235
  event.columns[0].name.should == 'Name'
207
236
  event.columns[0].label.should == 'Label'
208
237
  event.columns[0].parent_required.should be_true
238
+ event.badges[0].badge_key.should == 'athletics'
239
+ event.badges[0].badge_section.should == :cubs
240
+ event.badges[0].badge_type.should == :activity
241
+ event.badges[0].requirement_key.should == 'b_06'
242
+ event.badges[0].data.should == ''
243
+ event.badges[0].label.should == 'B: Run'
244
+ event.badges[1].badge_key.should == 'hikes'
245
+ event.badges[1].badge_section.should == :staged
246
+ event.badges[1].badge_type.should == :staged
247
+ event.badges[1].requirement_key.should == 'custom_69153'
248
+ event.badges[1].data.should == '1'
209
249
  event.valid?.should be_true
210
250
  end
211
251
 
252
+ it "Handles a blank config" do
253
+ @event_body['config'] = ''
254
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEvent&sectionid=1&eventid=2", :body => @event_body.to_json,:content_type => 'application/json')
255
+ expect { @event = Osm::Event.get(@api, 1, 2) }.to_not raise_error
256
+ @event.columns.should == []
257
+ end
258
+
212
259
  it 'Handles cost of "-1" for TBC' do
213
260
  FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/events.php?action=getEvent&sectionid=1&eventid=2", :body => @event_body.merge({'cost' => '-1'}).to_json, :content_type => 'application/json')
214
261
 
@@ -271,6 +318,28 @@ describe "Event" do
271
318
  end
272
319
  end
273
320
 
321
+ describe "Get events list for section" do
322
+ it "From OSM" do
323
+ events = Osm::Event.get_list(@api, 1)
324
+ events.map{ |e| e[:id]}.should == [2]
325
+ end
326
+
327
+ it "From cache" do
328
+ events = Osm::Event.get_list(@api, 1)
329
+ HTTParty.should_not_receive(:post)
330
+ Osm::Event.get_list(@api, 1).should == events
331
+ end
332
+
333
+ it "From cached events" do
334
+ Osm::Event.get_for_section(@api, 1)
335
+ HTTParty.should_not_receive(:post)
336
+ events = Osm::Event.get_list(@api, 1)
337
+ events.map{ |e| e[:id]}.should == [2]
338
+ end
339
+
340
+ end
341
+
342
+
274
343
  it "Get event" do
275
344
  event = Osm::Event.get(@api, 1, 2)
276
345
  event.should_not be_nil
@@ -417,6 +486,7 @@ describe "Event" do
417
486
  :cost => '1.23',
418
487
  :location => 'Somewhere',
419
488
  :notes => 'none',
489
+ :badges => [],
420
490
  :columns => [],
421
491
  :notepad => '',
422
492
  :public_notepad => '',
@@ -467,6 +537,7 @@ describe "Event" do
467
537
  :cost => 'TBC',
468
538
  :location => 'Somewhere',
469
539
  :notes => 'none',
540
+ :badges => [],
470
541
  :columns => [],
471
542
  :notepad => '',
472
543
  :public_notepad => '',
@@ -481,6 +552,144 @@ describe "Event" do
481
552
  event.id.should == 2
482
553
  end
483
554
 
555
+ describe "With badges" do
556
+
557
+ before :each do
558
+ url = 'https://www.onlinescoutmanager.co.uk/events.php?action=addEvent&sectionid=1'
559
+ post_data = {
560
+ 'apiid' => @CONFIGURATION[:api][:osm][:id],
561
+ 'token' => @CONFIGURATION[:api][:osm][:token],
562
+ 'userid' => 'user_id',
563
+ 'secret' => 'secret',
564
+ 'name' => 'Test event',
565
+ 'startdate' => '2000-01-02',
566
+ 'enddate' => '2001-02-03',
567
+ 'starttime' => '03:04:05',
568
+ 'endtime' => '04:05:06',
569
+ 'cost' => '1.23',
570
+ 'location' => 'Somewhere',
571
+ 'notes' => 'none',
572
+ 'confdate' => '2000-01-01',
573
+ 'allowChanges' => 'true',
574
+ 'disablereminders' => 'false',
575
+ 'attendancelimit' => 3,
576
+ 'limitincludesleaders' => 'true',
577
+ 'allowbooking' => 'true',
578
+ 'attendancereminder' => 1,
579
+ }
580
+
581
+ Osm::Event.stub(:get_for_section) { [] }
582
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"id":2}'}) }
583
+
584
+ @attributes = {
585
+ :section_id => 1,
586
+ :name => 'Test event',
587
+ :start => DateTime.new(2000, 1, 2, 3, 4, 5),
588
+ :finish => DateTime.new(2001, 2, 3, 4, 5, 6),
589
+ :cost => '1.23',
590
+ :location => 'Somewhere',
591
+ :notes => 'none',
592
+ :badges => [],
593
+ :columns => [],
594
+ :notepad => '',
595
+ :public_notepad => '',
596
+ :confirm_by_date => Date.new(2000, 1, 1),
597
+ :allow_changes => true,
598
+ :reminders => true,
599
+ :attendance_limit => 3,
600
+ :attendance_limit_includes_leaders => true,
601
+ :attendance_reminder => 1,
602
+ :allow_booking => true,
603
+ }
604
+ @badge_url = 'https://www.onlinescoutmanager.co.uk/ext/events/event/index.php?action=badgeAddToEvent&sectionid=1&eventid=2'
605
+ end
606
+
607
+ it "'Normal badge'" do
608
+ post_data = {
609
+ 'apiid' => @CONFIGURATION[:api][:osm][:id],
610
+ 'token' => @CONFIGURATION[:api][:osm][:token],
611
+ 'userid' => 'user_id',
612
+ 'secret' => 'secret',
613
+ 'section' => :beavers,
614
+ 'badgetype' => :activity,
615
+ 'badge' => 'test',
616
+ 'columnname' => 'a_01',
617
+ 'data' => '',
618
+ 'newcolumnname' => '',
619
+ }
620
+ HTTParty.should_receive(:post).with(@badge_url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"ok":true}'}) }
621
+
622
+ @attributes[:badges] = [Osm::Event::BadgeLink.new(
623
+ badge_key: 'test',
624
+ badge_type: :activity,
625
+ requirement_key: 'a_01',
626
+ badge_section: :beavers,
627
+ label: '',
628
+ data: '',
629
+ )]
630
+ event = Osm::Event.create(@api, @attributes)
631
+ event.should_not be_nil
632
+ event.id.should == 2
633
+ end
634
+
635
+ it "Add a hikes column" do
636
+ post_data = {
637
+ 'apiid' => @CONFIGURATION[:api][:osm][:id],
638
+ 'token' => @CONFIGURATION[:api][:osm][:token],
639
+ 'userid' => 'user_id',
640
+ 'secret' => 'secret',
641
+ 'section' => :staged,
642
+ 'badgetype' => :staged,
643
+ 'badge' => 'hikes',
644
+ 'columnname' => '',
645
+ 'data' => '1',
646
+ 'newcolumnname' => 'Label for added column',
647
+ }
648
+ HTTParty.should_receive(:post).with(@badge_url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"ok":true}'}) }
649
+
650
+ @attributes[:badges] = [Osm::Event::BadgeLink.new(
651
+ badge_key: 'hikes',
652
+ badge_type: :staged,
653
+ requirement_key: '',
654
+ badge_section: :staged,
655
+ label: 'Label for added column',
656
+ data: '1',
657
+ )]
658
+ event = Osm::Event.create(@api, @attributes)
659
+ event.should_not be_nil
660
+ event.id.should == 2
661
+ end
662
+
663
+ it "Existing nights away column" do
664
+ post_data = {
665
+ 'apiid' => @CONFIGURATION[:api][:osm][:id],
666
+ 'token' => @CONFIGURATION[:api][:osm][:token],
667
+ 'userid' => 'user_id',
668
+ 'secret' => 'secret',
669
+ 'section' => :staged,
670
+ 'badgetype' => :staged,
671
+ 'badge' => 'nights',
672
+ 'columnname' => 'custom_01234',
673
+ 'data' => '2',
674
+ 'newcolumnname' => '',
675
+ }
676
+ HTTParty.should_receive(:post).with(@badge_url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"ok":true}'}) }
677
+
678
+ @attributes[:badges] = [Osm::Event::BadgeLink.new(
679
+ badge_key: 'nights',
680
+ badge_type: :staged,
681
+ requirement_key: 'custom_01234',
682
+ badge_section: :staged,
683
+ label: '',
684
+ data: '2',
685
+ )]
686
+ event = Osm::Event.create(@api, @attributes)
687
+ event.should_not be_nil
688
+ event.id.should == 2
689
+ end
690
+
691
+ end
692
+
484
693
  end
485
694
 
486
695
  it "Create (failed)" do
@@ -539,7 +748,7 @@ describe "Event" do
539
748
 
540
749
  event = Osm::Event.new(
541
750
  :section_id => 1,
542
- :name => 'Test event',
751
+ :name => '',
543
752
  :start => DateTime.new(2000, 01, 02, 03, 04, 05),
544
753
  :finish => DateTime.new(2001, 02, 03, 04, 05, 06),
545
754
  :cost => '1.23',
@@ -556,6 +765,7 @@ describe "Event" do
556
765
  :attendance_reminder => 2,
557
766
  :allow_booking => true,
558
767
  )
768
+ event.name = 'Test event'
559
769
  event.notepad = 'notepad'
560
770
  event.public_notepad = 'public notepad'
561
771
  event.update(@api).should be_true
@@ -611,6 +821,78 @@ describe "Event" do
611
821
  event.update(@api).should be_true
612
822
  end
613
823
 
824
+ describe "Badge links" do
825
+
826
+ before :each do
827
+ @event = Osm::Event.new({
828
+ :id => 2,
829
+ :section_id => 1,
830
+ :name => 'Test event',
831
+ :start => DateTime.new(2000, 1, 2, 3, 4, 5),
832
+ :finish => DateTime.new(2001, 2, 3, 4, 5, 6),
833
+ :cost => '1.23',
834
+ :location => 'Somewhere',
835
+ :notes => 'none',
836
+ :badges => [Osm::Event::BadgeLink.new(badge_key: 'test')],
837
+ :columns => [],
838
+ :notepad => '',
839
+ :public_notepad => '',
840
+ :confirm_by_date => Date.new(2000, 1, 1),
841
+ :allow_changes => true,
842
+ :reminders => true,
843
+ :attendance_limit => 3,
844
+ :attendance_limit_includes_leaders => true,
845
+ :attendance_reminder => 1,
846
+ :allow_booking => true,
847
+ })
848
+ end
849
+
850
+ it "Added" do
851
+ url = 'https://www.onlinescoutmanager.co.uk/ext/events/event/index.php?action=badgeAddToEvent&sectionid=1&eventid=2'
852
+ post_data = {
853
+ 'apiid' => @CONFIGURATION[:api][:osm][:id],
854
+ 'token' => @CONFIGURATION[:api][:osm][:token],
855
+ 'userid' => 'user_id',
856
+ 'secret' => 'secret',
857
+ 'badgelinks' => [{
858
+ 'section' => nil,
859
+ 'badgetype' => nil,
860
+ 'badge' => 'test2',
861
+ 'columnname' => nil,
862
+ 'data' => nil,
863
+ 'newcolumnname' => nil,
864
+ }],
865
+ }
866
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"ok":true,"error":"Update Error"}'}) }
867
+
868
+ @event.badges.push(Osm::Event::BadgeLink.new(badge_key: 'test2'))
869
+ @event.update(@api).should be_true
870
+ end
871
+
872
+ it "Removed" do
873
+ url = 'https://www.onlinescoutmanager.co.uk/ext/events/event/index.php?action=badgeDeleteFromEvent&sectionid=1&eventid=2'
874
+ post_data = {
875
+ 'apiid' => @CONFIGURATION[:api][:osm][:id],
876
+ 'token' => @CONFIGURATION[:api][:osm][:token],
877
+ 'userid' => 'user_id',
878
+ 'secret' => 'secret',
879
+ 'badgelinks' => [{
880
+ 'section' => nil,
881
+ 'badgetype' => nil,
882
+ 'badge' => 'test',
883
+ 'columnname' => nil,
884
+ 'data' => nil,
885
+ 'newcolumnname' => nil,
886
+ }],
887
+ }
888
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"ok":true,"error":"Update Error"}'}) }
889
+
890
+ @event.badges = []
891
+ @event.update(@api).should be_true
892
+ end
893
+
894
+ end
895
+
614
896
  end
615
897
 
616
898
  it "Update (failed)" do
@@ -626,6 +908,7 @@ describe "Event" do
626
908
  :notes => 'none',
627
909
  :id => 2
628
910
  )
911
+ event.id = 22
629
912
  event.update(@api).should be_false
630
913
  end
631
914
 
data/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Osm
2
- VERSION = "1.2.10"
2
+ VERSION = "1.2.11"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: osm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.10
4
+ version: 1.2.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Gauld
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-14 00:00:00.000000000 Z
11
+ date: 2014-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport