osm 0.2.2 → 0.3.0

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.
@@ -0,0 +1,149 @@
1
+ module Osm
2
+
3
+ class Badges
4
+
5
+ # Get badge stock levels for a section
6
+ # @param [Osm::Api] api The api to use to make the request
7
+ # @param [Osm::Section, Fixnum, #to_i] section The section (or its ID) to get the badge stock for
8
+ # @!macro options_get
9
+ # @return Hash
10
+ def self.get_stock(api, section, options={})
11
+ Osm::Model.require_ability_to(api, :read, :badge, section, options)
12
+ section = Osm::Section.get(api, section, options) unless section.is_a?(Osm::Section)
13
+ term_id = Osm::Term.get_current_term_for_section(api, section).id
14
+ cache_key = ['badge_stock', section.id]
15
+
16
+ if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key)
17
+ return Osm::Model.cache_read(api, cache_key)
18
+ end
19
+
20
+ data = api.perform_query("challenges.php?action=getInitialBadges&type=core&sectionid=#{section.id}&section=#{section.type}&termid=#{term_id}")
21
+ data = (data['stock'] || {}).select{ |k,v| !k.eql?('sectionid') }.
22
+ inject({}){ |new_hash,(badge, level)| new_hash[badge] = level.to_i; new_hash }
23
+
24
+ Osm::Model.cache_write(api, cache_key, data)
25
+ return data
26
+ end
27
+
28
+ # Update badge stock levels
29
+ # @param [Osm::Api] api The api to use to make the request
30
+ # @param [Osm::Section, Fixnum, #to_i] section The section (or its ID) to update ther badge stock for
31
+ # @param [Sring, #to_s] badge_key The badge to set the stock level for
32
+ # @param [Fixnum, #to_i] stock_level How many of the provided badge there are
33
+ # @return [Boolan] whether the update was successfull or not
34
+ def self.update_stock(api, section, badge_key, stock_level)
35
+ Osm::Model.require_ability_to(api, :write, :badge, section)
36
+ section = Osm::Section.get(api, section) unless section.is_a?(Osm::Section)
37
+
38
+ Osm::Model.cache_delete(api, ['badge_stock', section.id])
39
+
40
+ data = api.perform_query("challenges.php?action=updateStock", {
41
+ 'stock' => stock_level,
42
+ 'table' => badge_key,
43
+ 'sectionid' => section.id,
44
+ 'section' => section.type,
45
+ })
46
+ return data.is_a?(Hash) && (data['sectionid'].to_i == section.id) && (data[badge_key.to_s].to_i == stock_level)
47
+ end
48
+
49
+
50
+ # Get due badges
51
+ # @param [Osm::Api] api The api to use to make the request
52
+ # @param [Osm::Section, Fixnum, #to_i] section The section (or its ID) to get the due badges for
53
+ # @param [Osm::Term, Fixnum, #to_i, nil] term The term (or its ID) to get the due badges for, passing nil causes the current term to be used
54
+ # @!macro options_get
55
+ # @return [Osm::Badges::DueBadges]
56
+ def self.get_due_badges(api, section, term=nil, options={})
57
+ Osm::Model.require_ability_to(api, :read, :badge, section, options)
58
+ section = Osm::Section.get(api, section, options) unless section.is_a?(Osm::Section)
59
+ term_id = (term.nil? ? Osm::Term.get_current_term_for_section(api, section, options) : term).to_i
60
+ cache_key = ['due_badges', section.id, term_id]
61
+
62
+ if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key)
63
+ return Osm::Model.cache_read(api, cache_key)
64
+ end
65
+
66
+ data = api.perform_query("challenges.php?action=outstandingBadges&section=#{section.type}&sectionid=#{section.id}&termid=#{term_id}")
67
+
68
+ data = {} unless data.is_a?(Hash) # OSM/OGM returns an empty array to represent no badges
69
+ pending_raw = data['pending'] || {}
70
+ descriptions_raw = data['description'] || {}
71
+
72
+ attributes = {
73
+ :by_member => {},
74
+ :descriptions => {}
75
+ }
76
+
77
+ pending_raw.each do |key, members|
78
+ members.each do |member|
79
+ name = "#{member['firstname']} #{member['lastname']}"
80
+ description = descriptions_raw[key]['name'] + (descriptions_raw[key]['section'].eql?('staged') ? " (Level #{member['level']})" : '')
81
+ description_key = key + (descriptions_raw[key]['section'].eql?('staged') ? "_#{member['level']}" : '_1')
82
+ attributes[:descriptions][description_key] = description
83
+ attributes[:by_member][name] ||= []
84
+ attributes[:by_member][name].push(description_key)
85
+ end
86
+ end
87
+
88
+ due_badges = Osm::Badges::DueBadges.new(attributes)
89
+ Osm::Model.cache_write(api, cache_key, due_badges)
90
+ return due_badges
91
+ end
92
+
93
+
94
+ class DueBadges < Osm::Model
95
+ # @!attribute [rw] descriptions
96
+ # @return [Hash] descriptions for each of the badges
97
+ # @!attribute [rw] by_member
98
+ # @return [Hash] the due badges grouped by member
99
+
100
+ attribute :descriptions, :default => {}
101
+ attribute :by_member, :default => {}
102
+
103
+ attr_accessible :descriptions, :by_member
104
+
105
+ validates :descriptions, :hash => {:key_type => String, :value_type => String}
106
+
107
+ validates_each :by_member do |record, attr, value|
108
+ desc_keys = record.descriptions.keys
109
+ record.errors.add(attr, 'must be a Hash') unless value.is_a?(Hash)
110
+ value.each do |k, v|
111
+ record.errors.add(attr, 'keys must be String') unless k.is_a?(String)
112
+ record.errors.add(attr, 'values must be Arrays') unless v.is_a?(Array)
113
+ v.each do |vv|
114
+ record.errors.add(attr, 'internal values must be Strings') unless vv.is_a?(String)
115
+ record.errors.add(attr, 'internal values must exist as a key in :descriptions') unless desc_keys.include?(vv)
116
+ end
117
+ end
118
+ end
119
+
120
+
121
+ # @!method initialize
122
+ # Initialize a new Term
123
+ # @param [Hash] attributes The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
124
+
125
+
126
+ # Check if there are no badges due
127
+ # @return [Boolean]
128
+ def empty?
129
+ return by_member.empty?
130
+ end
131
+
132
+ # Calculate the total number of badges needed
133
+ # @return [Hash] the total number of each badge which is due
134
+ def totals()
135
+ totals = {}
136
+ by_member.keys.each do |member_name|
137
+ by_member[member_name].each do |badge|
138
+ totals[badge] ||= 0
139
+ totals[badge] += 1
140
+ end
141
+ end
142
+ return totals
143
+ end
144
+
145
+ end # Class Badges::DueBadges
146
+
147
+ end # Class Badges
148
+
149
+ end # Module
@@ -237,7 +237,7 @@ module Osm
237
237
  # @!attribute [rw] grouping__id
238
238
  # @return [Fixnum] OSM id for the grouping the member is in
239
239
  # @!attribute [rw] fields
240
- # @return [Hash] Keys are the field's id, values are the field values
240
+ # @return [DirtyHashy] Keys are the field's id, values are the field values
241
241
 
242
242
  attribute :flexi_record, :type => Object
243
243
  attribute :member_id, :type => Integer
@@ -251,9 +251,18 @@ module Osm
251
251
  validates_numericality_of :grouping_id, :only_integer=>true, :greater_than_or_equal_to=>-2
252
252
  validates :fields, :hash => {:key_type => String}
253
253
 
254
+
254
255
  # @!method initialize
255
256
  # Initialize a new FlexiRecord::Data
256
257
  # @param [Hash] attributes The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
258
+ # Override initialize to set @orig_attributes
259
+ old_initialize = instance_method(:initialize)
260
+ define_method :initialize do |*args|
261
+ ret_val = old_initialize.bind(self).call(*args)
262
+ self.fields = DirtyHashy.new(self.fields)
263
+ self.fields.clean_up!
264
+ return ret_val
265
+ end
257
266
 
258
267
 
259
268
  # Update data in OSM
@@ -267,20 +276,21 @@ module Osm
267
276
  term_id = Osm::Term.get_current_term_for_section(api, flexi_record.section_id).id
268
277
 
269
278
  updated = true
270
- flexi_record.get_columns(api).each do |column|
271
- if column.editable
279
+ editable_fields = flexi_record.get_columns(api).select{ |c| c.editable }.map{ |i| i.id }
280
+ fields.changes.each do |field, (was,now)|
281
+ if editable_fields.include?(field)
272
282
  data = api.perform_query("extras.php?action=updateScout", {
273
283
  'termid' => term_id,
274
284
  'scoutid' => self.member_id,
275
- 'column' => column.id,
276
- 'value' => fields[column.id],
285
+ 'column' => field,
286
+ 'value' => now,
277
287
  'sectionid' => flexi_record.section_id,
278
288
  'extraid' => flexi_record.id,
279
289
  })
280
290
  if (data.is_a?(Hash) && data['items'].is_a?(Array))
281
291
  data['items'].each do |item|
282
292
  if item['scoutid'] == member_id.to_s # Find this member from the list of all members
283
- updated = false unless item[column.id] == self.fields[column.id]
293
+ updated = false unless item[field] == now
284
294
  end
285
295
  end
286
296
  else
@@ -290,7 +300,7 @@ module Osm
290
300
  end
291
301
 
292
302
  if updated
293
- reset_changed_attributes
303
+ fields.clean_up!
294
304
  # The cached datas for the flexi record will be out of date - remove them
295
305
  cache_delete(api, ['flexi_record_data', flexi_record.id])
296
306
  end
@@ -291,29 +291,6 @@ module Osm
291
291
  end
292
292
 
293
293
 
294
- # Get badge stock levels
295
- # @param [Osm::Api] api The api to use to make the request
296
- # @param [Osm::Term, Fixnum, #to_i, nil] term The term (or its ID) to get the stock levels for, passing nil causes the current term to be used
297
- # @!macro options_get
298
- # @return Hash
299
- def get_badge_stock(api, term=nil, options={})
300
- require_ability_to(api, :read, :badge, self, options)
301
- term_id = term.nil? ? Osm::Term.get_current_term_for_section(api, self).id : term.to_i
302
- cache_key = ['badge_stock', id, term_id]
303
-
304
- if !options[:no_cache] && cache_exist?(api, cache_key)
305
- return cache_read(api, cache_key)
306
- end
307
-
308
- data = api.perform_query("challenges.php?action=getInitialBadges&type=core&sectionid=#{id}&section=#{type}&termid=#{term_id}")
309
- data = (data['stock'] || {}).select{ |k,v| !k.eql?('sectionid') }.
310
- inject({}){ |new_hash,(badge, level)| new_hash[badge] = level.to_i; new_hash }
311
-
312
- cache_write(api, cache_key, data)
313
- return data
314
- end
315
-
316
-
317
294
  # Check if this section is one of the youth sections
318
295
  # @return [Boolean]
319
296
  def youth_section?
@@ -22,11 +22,12 @@ Gem::Specification.new do |s|
22
22
  s.add_runtime_dependency 'httparty', '~> 0.9' # Used to make web requests to the API
23
23
  s.add_runtime_dependency 'active_attr', '~> 0.6'
24
24
  s.add_runtime_dependency 'activemodel', '~> 3.2'
25
+ s.add_runtime_dependency 'dirty_hashy', '~> 0.2.1' # Used to trach changed data in Badge::Data and FlexiRecord::Data
25
26
 
26
27
  s.add_development_dependency 'rake', '~> 10.0'
27
28
  s.add_development_dependency 'rspec', '~> 2.11'
28
29
  s.add_development_dependency 'fakeweb', '~> 1.3'
29
30
  s.add_development_dependency 'guard-rspec', '~> 2.4'
30
- s.add_development_dependency 'rb-inotify', '~> 0.8.8'
31
+ s.add_development_dependency 'rb-inotify', '~> 0.9'
31
32
 
32
33
  end
@@ -0,0 +1,452 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe "Badge" do
5
+
6
+ it "Create" do
7
+ badge = Osm::Badge.new(
8
+ :name => 'name',
9
+ :requirement_notes => 'notes',
10
+ :osm_key => 'key',
11
+ :sections_needed => 1,
12
+ :total_needed => 2,
13
+ :needed_from_section => {'a' => 1},
14
+ :requirements => [],
15
+ )
16
+
17
+ badge.name.should == 'name'
18
+ badge.requirement_notes.should == 'notes'
19
+ badge.osm_key.should == 'key'
20
+ badge.sections_needed.should == 1
21
+ badge.total_needed.should == 2
22
+ badge.needed_from_section.should == {'a' => 1}
23
+ badge.requirements.should == []
24
+ badge.valid?.should be_true
25
+ end
26
+
27
+ it "Create Requirement" do
28
+ requirement = Osm::Badge::Requirement.new(
29
+ :name => 'name',
30
+ :description => 'description',
31
+ :field => 'field',
32
+ :editable => true,
33
+ :badge => Osm::Badge.new(:osm_key => 'key'),
34
+ )
35
+
36
+ requirement.name.should == 'name'
37
+ requirement.description.should == 'description'
38
+ requirement.field.should == 'field'
39
+ requirement.editable.should == true
40
+ requirement.badge.osm_key.should == 'key'
41
+ requirement.valid?.should be_true
42
+ end
43
+
44
+ it "Create Data" do
45
+ data = Osm::Badge::Data.new(
46
+ :member_id => 1,
47
+ :completed => true,
48
+ :awarded_date => Date.new(2000, 1, 2),
49
+ :requirements => {},
50
+ :section_id => 2,
51
+ :badge => Osm::Badge.new(:osm_key => 'key'),
52
+ )
53
+
54
+ data.member_id.should == 1
55
+ data.completed.should == true
56
+ data.awarded_date.should == Date.new(2000, 1, 2)
57
+ data.requirements.should == {}
58
+ data.section_id.should == 2
59
+ data.badge.osm_key.should == 'key'
60
+ data.valid?.should be_true
61
+ end
62
+
63
+
64
+ it "Compare badges by name then osm_key" do
65
+ b1 = Osm::Badge.new(:name => 'A', :osm_key => 'a')
66
+ b2 = Osm::Badge.new(:name => 'B', :osm_key => 'a')
67
+ b3 = Osm::Badge.new(:name => 'B', :osm_key => 'b')
68
+ badges = [b3, b1, b2]
69
+ badges.sort.should == [b1, b2, b3]
70
+ end
71
+
72
+ it "Compare badge requirements by badge then field" do
73
+ b1 = Osm::Badge::Requirement.new(:badge => Osm::Badge.new(:name => 'A'), :field => 'a')
74
+ b2 = Osm::Badge::Requirement.new(:badge => Osm::Badge.new(:name => 'B'), :field => 'a')
75
+ b3 = Osm::Badge::Requirement.new(:badge => Osm::Badge.new(:name => 'B'), :field => 'b')
76
+ badges = [b3, b1, b2]
77
+ badges.sort.should == [b1, b2, b3]
78
+ end
79
+
80
+ it "Compare badge data by badge, section_id then member_id" do
81
+ b1 = Osm::Badge::Data.new(:badge => Osm::Badge.new(:name => 'A'), :section_id => 1, :member_id => 1)
82
+ b2 = Osm::Badge::Data.new(:badge => Osm::Badge.new(:name => 'B'), :section_id => 1, :member_id => 1)
83
+ b3 = Osm::Badge::Data.new(:badge => Osm::Badge.new(:name => 'B'), :section_id => 2, :member_id => 1)
84
+ b4 = Osm::Badge::Data.new(:badge => Osm::Badge.new(:name => 'B'), :section_id => 2, :member_id => 2)
85
+ badges = [b3, b4, b1, b2]
86
+ badges.sort.should == [b1, b2, b3, b4]
87
+ end
88
+
89
+
90
+ it "Get total requirements gained for a member" do
91
+ data = Osm::Badge::Data.new(
92
+ :requirements => {'a_1' => 'x', 'a_2' => 'a', 'b_1' => 'yes', 'b_2' => '2000-01-02'}
93
+ )
94
+ data.total_gained.should == 3
95
+ end
96
+
97
+ it "Get total requirements met in each section for a member" do
98
+ badge = Osm::Badge.new(
99
+ :needed_from_section => {'a' => 1, 'b' => 2},
100
+ :requirements => [
101
+ Osm::Badge::Requirement.new(:field => 'a_1'),
102
+ Osm::Badge::Requirement.new(:field => 'a_2'),
103
+ Osm::Badge::Requirement.new(:field => 'b_1'),
104
+ Osm::Badge::Requirement.new(:field => 'b_2'),
105
+ ]
106
+ )
107
+ data = Osm::Badge::Data.new(
108
+ :badge => badge,
109
+ :requirements => {'a_1' => 'x', 'a_2' => '', 'b_1' => 'yes', 'b_2' => '2000-01-02'}
110
+ )
111
+ data.gained_in_sections.should == {'a' => 0, 'b' => 2}
112
+ end
113
+
114
+ it "Get number of sections met for a member" do
115
+ badge = Osm::Badge.new(
116
+ :needed_from_section => {'a' => 1, 'b' => 2},
117
+ :requirements => [
118
+ Osm::Badge::Requirement.new(:field => 'a_1'),
119
+ Osm::Badge::Requirement.new(:field => 'a_2'),
120
+ Osm::Badge::Requirement.new(:field => 'b_1'),
121
+ Osm::Badge::Requirement.new(:field => 'b_2'),
122
+ ]
123
+ )
124
+ data = Osm::Badge::Data.new(
125
+ :badge => badge,
126
+ :requirements => {'a_1' => 'x', 'a_2' => '', 'b_1' => 'yes', 'b_2' => '2000-01-02'}
127
+ )
128
+ data.sections_gained.should == 1
129
+ end
130
+
131
+
132
+ describe "Using the OSM API" do
133
+
134
+ describe "Get Badges" do
135
+
136
+ before :each do
137
+ @data = {
138
+ "badgeOrder" => "badge",
139
+ "structure" => {
140
+ "badge" => [
141
+ {
142
+ "rows" => [
143
+ {"name" => "First name","field" => "firstname","width" => "120px"},
144
+ {"name" => "Last name","field" => "lastname","width" => "120px"},
145
+ {"name" => "Done","field" => "completed","width" => "70px","formatter" => "doneFormatter"},
146
+ {"name" => "Awarded","field" => "awardeddate","width" => "100px","formatter" => "dueFormatter"}
147
+ ],
148
+ "numactivities" => "23",
149
+ "noscroll" => true
150
+ },{
151
+ "rows" => [
152
+ {"name" => "r_name","field" => "r_field","width" => "80px","formatter" => "cellFormatter","tooltip" => "r_description","editable" => "true"}
153
+ ]
154
+ }
155
+ ],
156
+ },
157
+ "details" => {
158
+ "badge" => {
159
+ "shortname" => "badge",
160
+ "name" => "b_name",
161
+ "description" => "b_req_notes",
162
+ "picture" => "badge.png",
163
+ "config" => "{\"sectionsneeded\":\"1\",\"totalneeded\":\"2\",\"sections\":{\"a\":\"1\"}}",
164
+ "order" => "1",
165
+ "groupname" => nil,
166
+ "status" => "3",
167
+ "userid" => "0",
168
+ "table" => "table"
169
+ },
170
+ },
171
+ "stock" => {"sectionid" => "1","badge" => "3"}
172
+ }
173
+ @data = @data.to_json
174
+ end
175
+
176
+ it "Core" do
177
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=core&sectionid=1&section=beavers&termid=2", :body => @data)
178
+ Osm::Term.stub(:get_current_term_for_section){ Osm::Term.new(:id => 2) }
179
+
180
+ badges = Osm::CoreBadge.get_badges_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers))
181
+ badges.size.should == 1
182
+ badge = badges[0]
183
+ badge.name.should == 'b_name'
184
+ badge.requirement_notes.should == 'b_req_notes'
185
+ badge.osm_key.should == 'badge'
186
+ badge.sections_needed.should == 1
187
+ badge.total_needed.should == 2
188
+ badge.needed_from_section.should == {'a' => 1}
189
+ badge.requirements.size.should == 1
190
+ requirement = badge.requirements[0]
191
+ requirement.name.should == 'r_name'
192
+ requirement.description.should == 'r_description'
193
+ requirement.field.should == 'r_field'
194
+ requirement.editable.should == true
195
+ requirement.badge.osm_key.should == 'badge'
196
+ end
197
+
198
+ it "Challenge" do
199
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=challenge&sectionid=1&section=beavers&termid=2", :body => @data)
200
+ Osm::Term.stub(:get_current_term_for_section){ Osm::Term.new(:id => 2) }
201
+
202
+ badges = Osm::ChallengeBadge.get_badges_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers))
203
+ badges.size.should == 1
204
+ badge = badges[0]
205
+ badge.name.should == 'b_name'
206
+ badge.requirement_notes.should == 'b_req_notes'
207
+ badge.osm_key.should == 'badge'
208
+ badge.sections_needed.should == 1
209
+ badge.total_needed.should == 2
210
+ badge.needed_from_section.should == {'a' => 1}
211
+ badge.requirements.size.should == 1
212
+ requirement = badge.requirements[0]
213
+ requirement.name.should == 'r_name'
214
+ requirement.description.should == 'r_description'
215
+ requirement.field.should == 'r_field'
216
+ requirement.editable.should == true
217
+ requirement.badge.osm_key.should == 'badge'
218
+ end
219
+
220
+ it "Staged" do
221
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=staged&sectionid=1&section=beavers&termid=2", :body => @data)
222
+ Osm::Term.stub(:get_current_term_for_section){ Osm::Term.new(:id => 2) }
223
+
224
+ badges = Osm::StagedBadge.get_badges_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers))
225
+ badges.size.should == 1
226
+ badge = badges[0]
227
+ badge.name.should == 'b_name'
228
+ badge.requirement_notes.should == 'b_req_notes'
229
+ badge.osm_key.should == 'badge'
230
+ badge.sections_needed.should == 1
231
+ badge.total_needed.should == 2
232
+ badge.needed_from_section.should == {'a' => 1}
233
+ badge.requirements.size.should == 1
234
+ requirement = badge.requirements[0]
235
+ requirement.name.should == 'r_name'
236
+ requirement.description.should == 'r_description'
237
+ requirement.field.should == 'r_field'
238
+ requirement.editable.should == true
239
+ requirement.badge.osm_key.should == 'badge'
240
+ end
241
+
242
+ it "Activity" do
243
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=activity&sectionid=1&section=beavers&termid=2", :body => @data)
244
+ Osm::Term.stub(:get_current_term_for_section){ Osm::Term.new(:id => 2) }
245
+
246
+ badges = Osm::ActivityBadge.get_badges_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers))
247
+ badges.size.should == 1
248
+ badge = badges[0]
249
+ badge.name.should == 'b_name'
250
+ badge.requirement_notes.should == 'b_req_notes'
251
+ badge.osm_key.should == 'badge'
252
+ badge.sections_needed.should == 1
253
+ badge.total_needed.should == 2
254
+ badge.needed_from_section.should == {'a' => 1}
255
+ badge.requirements.size.should == 1
256
+ requirement = badge.requirements[0]
257
+ requirement.name.should == 'r_name'
258
+ requirement.description.should == 'r_description'
259
+ requirement.field.should == 'r_field'
260
+ requirement.editable.should == true
261
+ requirement.badge.osm_key.should == 'badge'
262
+ end
263
+
264
+ end
265
+
266
+
267
+ describe "Get badge data for a section" do
268
+
269
+ before :each do
270
+ @data = {
271
+ 'identifier' => 'scoutid',
272
+ 'items' => [{
273
+ 'scoutid' => 3,
274
+ 'firstname' => 'fn',
275
+ 'lastname' => 'ln',
276
+ 'sid' => '',
277
+ 'completed' => '1',
278
+ 'awarded' => '',
279
+ 'awardeddate' => '2000-01-02',
280
+ 'patrolid' => 4,
281
+ 'a_1' => 'd',
282
+ }]
283
+ }
284
+ @data = @data.to_json
285
+ end
286
+
287
+ it "Core badge" do
288
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?termid=2&type=core&section=beavers&c=badge&sectionid=1", :body => @data)
289
+ datas = Osm::CoreBadge.get_badge_data_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers), Osm::Badge.new(:osm_key => 'badge'), 2)
290
+ datas.size.should == 1
291
+ data = datas[0]
292
+ data.member_id.should == 3
293
+ data.completed.should == true
294
+ data.awarded_date.should == Date.new(2000, 1, 2)
295
+ data.requirements.should == {'a_1' => 'd'}
296
+ data.section_id.should == 1
297
+ data.badge.osm_key.should == 'badge'
298
+ end
299
+
300
+ it "Challenge badge" do
301
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?termid=2&type=challenge&section=beavers&c=badge&sectionid=1", :body => @data)
302
+ datas = Osm::ChallengeBadge.get_badge_data_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers), Osm::Badge.new(:osm_key => 'badge'), 2)
303
+ datas.size.should == 1
304
+ data = datas[0]
305
+ data.member_id.should == 3
306
+ data.completed.should == true
307
+ data.awarded_date.should == Date.new(2000, 1, 2)
308
+ data.requirements.should == {'a_1' => 'd'}
309
+ data.section_id.should == 1
310
+ data.badge.osm_key.should == 'badge'
311
+ end
312
+
313
+ it "Staged badge" do
314
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?termid=2&type=staged&section=beavers&c=badge&sectionid=1", :body => @data)
315
+ datas = Osm::StagedBadge.get_badge_data_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers), Osm::Badge.new(:osm_key => 'badge'), 2)
316
+ datas.size.should == 1
317
+ data = datas[0]
318
+ data.member_id.should == 3
319
+ data.completed.should == true
320
+ data.awarded_date.should == Date.new(2000, 1, 2)
321
+ data.requirements.should == {'a_1' => 'd'}
322
+ data.section_id.should == 1
323
+ data.badge.osm_key.should == 'badge'
324
+ end
325
+
326
+ it "Activity badge" do
327
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?termid=2&type=activity&section=beavers&c=badge&sectionid=1", :body => @data)
328
+ datas = Osm::ActivityBadge.get_badge_data_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers), Osm::Badge.new(:osm_key => 'badge'), 2)
329
+ datas.size.should == 1
330
+ data = datas[0]
331
+ data.member_id.should == 3
332
+ data.completed.should == true
333
+ data.awarded_date.should == Date.new(2000, 1, 2)
334
+ data.requirements.should == {'a_1' => 'd'}
335
+ data.section_id.should == 1
336
+ data.badge.osm_key.should == 'badge'
337
+ end
338
+
339
+ end
340
+
341
+ describe "Update badge data for a section/member" do
342
+
343
+ before :each do
344
+ @post_data = {
345
+ 'apiid' => @CONFIGURATION[:api][:osm][:id],
346
+ 'token' => @CONFIGURATION[:api][:osm][:token],
347
+ 'userid' => 'user_id',
348
+ 'secret' => 'secret',
349
+ 'action' => 'updatesingle',
350
+ 'id' => 1,
351
+ 'col' => 'a',
352
+ 'value' => '2',
353
+ 'chal' => 'badge',
354
+ 'sectionid' => 2,
355
+ }
356
+
357
+ @body_data = {'sid' => '1', 'a' => '2', 'b' => '2'}
358
+ end
359
+
360
+ it "Core badge" do
361
+ data = Osm::Badge::Data.new(
362
+ :member_id => 1,
363
+ :section_id => 2,
364
+ :requirements => {'a' => '1', 'b' => '2'},
365
+ :badge => Osm::CoreBadge.new(
366
+ :osm_key => 'badge',
367
+ :requirements => [
368
+ Osm::Badge::Requirement.new(:field => 'a', :editable => true),
369
+ Osm::Badge::Requirement.new(:field => 'b', :editable => true),
370
+ ]),
371
+ :completed => false,
372
+ )
373
+
374
+ url = "https://www.onlinescoutmanager.co.uk/challenges.php?type=core&section=beavers"
375
+ HTTParty.should_receive(:post).with(url, {:body => @post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>@body_data.to_json}) }
376
+ Osm::Section.stub(:get) { Osm::Section.new(:id => 2, :type => :beavers) }
377
+
378
+ data.requirements['a'] = '2'
379
+ data.update(@api).should be_true
380
+ end
381
+
382
+ it "Challenge badge" do
383
+ data = Osm::Badge::Data.new(
384
+ :member_id => 1,
385
+ :section_id => 2,
386
+ :requirements => {'a' => '1', 'b' => '2'},
387
+ :badge => Osm::ChallengeBadge.new(
388
+ :osm_key => 'badge',
389
+ :requirements => [
390
+ Osm::Badge::Requirement.new(:field => 'a', :editable => true),
391
+ Osm::Badge::Requirement.new(:field => 'b', :editable => true),
392
+ ]),
393
+ :completed => false,
394
+ )
395
+
396
+ url = "https://www.onlinescoutmanager.co.uk/challenges.php?type=challenge&section=beavers"
397
+ HTTParty.should_receive(:post).with(url, {:body => @post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>@body_data.to_json}) }
398
+ Osm::Section.stub(:get) { Osm::Section.new(:id => 2, :type => :beavers) }
399
+
400
+ data.requirements['a'] = '2'
401
+ data.update(@api).should be_true
402
+ end
403
+
404
+ it "Staged badge" do
405
+ data = Osm::Badge::Data.new(
406
+ :member_id => 1,
407
+ :section_id => 2,
408
+ :requirements => {'a' => '1', 'b' => '2'},
409
+ :badge => Osm::StagedBadge.new(
410
+ :osm_key => 'badge',
411
+ :requirements => [
412
+ Osm::Badge::Requirement.new(:field => 'a', :editable => true),
413
+ Osm::Badge::Requirement.new(:field => 'b', :editable => true),
414
+ ]),
415
+ :completed => false,
416
+ )
417
+
418
+ url = "https://www.onlinescoutmanager.co.uk/challenges.php?type=staged&section=beavers"
419
+ HTTParty.should_receive(:post).with(url, {:body => @post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>@body_data.to_json}) }
420
+ Osm::Section.stub(:get) { Osm::Section.new(:id => 2, :type => :beavers) }
421
+
422
+ data.requirements['a'] = '2'
423
+ data.update(@api).should be_true
424
+ end
425
+
426
+ it "Activity badge" do
427
+ data = Osm::Badge::Data.new(
428
+ :member_id => 1,
429
+ :section_id => 2,
430
+ :requirements => {'a' => '1', 'b' => '2'},
431
+ :badge => Osm::ActivityBadge.new(
432
+ :osm_key => 'badge',
433
+ :requirements => [
434
+ Osm::Badge::Requirement.new(:field => 'a', :editable => true),
435
+ Osm::Badge::Requirement.new(:field => 'b', :editable => true),
436
+ ]),
437
+ :completed => false,
438
+ )
439
+
440
+ url = "https://www.onlinescoutmanager.co.uk/challenges.php?type=activity&section=beavers"
441
+ HTTParty.should_receive(:post).with(url, {:body => @post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>@body_data.to_json}) }
442
+ Osm::Section.stub(:get) { Osm::Section.new(:id => 2, :type => :beavers) }
443
+
444
+ data.requirements['a'] = '2'
445
+ data.update(@api).should be_true
446
+ end
447
+
448
+ end
449
+
450
+ end
451
+
452
+ end