osm 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,9 @@
1
+ ## Version 1.2.2
2
+
3
+ * Add base\_url method to Api class and instances
4
+ * Add get_photo method to member
5
+ * Add earnt and earnt? methods to Osm::Badge::Data to get the stage which has been earnt (based on the requirements hash)
6
+
1
7
  ## Version 1.2.1
2
8
 
3
9
  * Add section\_id read-only attribute to Osm::Error::NoCurrentTerm
@@ -139,6 +139,21 @@ module Osm
139
139
  return self
140
140
  end
141
141
 
142
+ # Get the base URL for requests to OSM/OGM
143
+ # @param [Symbol] site For OSM or OGM (:osm or :ogm)
144
+ # @return [String] The base URL for requests
145
+ def self.base_url(site=@@site)
146
+ raise ArgumentError, "Invalid site" unless [:osm, :ogm].include?(site)
147
+ BASE_URLS[site]
148
+ end
149
+
150
+ # Get the base URL for requests to OSM.OGM
151
+ # @param site For OSM or OGM (:osm or :ogm), defaults to the default for this api object
152
+ # @return [String] The base URL for requests
153
+ def base_url(site=@site)
154
+ raise ArgumentError, "Invalid site" unless [:osm, :ogm].include?(site)
155
+ self.class.base_url(site)
156
+ end
142
157
 
143
158
  # Make a query to the OSM/OGM API
144
159
  # @param [String] url The script on the remote server to invoke
@@ -217,15 +232,23 @@ module Osm
217
232
 
218
233
  if @@debug
219
234
  puts "Result from :#{site} request to #{url}"
235
+ puts "#{result.response.content_type}"
220
236
  puts result.response.body
221
237
  end
222
238
 
223
239
  return nil if result.response.body.empty?
224
- raise Osm::Error, result.response.body unless looks_like_json?(result.response.body)
225
- decoded = ActiveSupport::JSON.decode(result.response.body)
226
- osm_error = get_osm_error(decoded)
227
- raise Osm::Error, osm_error if osm_error
228
- return decoded
240
+ case result.response.content_type
241
+ when 'application/json', 'text/html'
242
+ raise Osm::Error, result.response.body unless looks_like_json?(result.response.body)
243
+ decoded = ActiveSupport::JSON.decode(result.response.body)
244
+ osm_error = get_osm_error(decoded)
245
+ raise Osm::Error, osm_error if osm_error
246
+ return decoded
247
+ when 'image/jpeg'
248
+ return result.response.body
249
+ else
250
+ raise Osm::Error, "Unhandled content-type: #{result.response.content_type}"
251
+ end
229
252
  end
230
253
 
231
254
  # Check if text looks like it's JSON
@@ -282,6 +282,10 @@ module Osm
282
282
  validates_numericality_of :section_id, :only_integer=>true, :greater_than=>0
283
283
  validates :requirements, :hash => {:key_type => String, :value_type => String}
284
284
 
285
+ STAGES_NIGHTSAWAY = [1, 5, 10, 20, 35, 50, 75, 100, 125, 150, 175, 200]
286
+ STAGES_HIKESAWAY = [1, 5, 10, 20, 35, 50]
287
+
288
+
285
289
  # @!method initialize
286
290
  # Initialize a new Badge
287
291
  # @param [Hash] attributes The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
@@ -300,7 +304,7 @@ module Osm
300
304
  def total_gained
301
305
  count = 0
302
306
  requirements.each do |field, data|
303
- next if data.blank? || data.to_s[0].downcase.eql?('x')
307
+ next unless reguiremet_met?(data)
304
308
  count += 1
305
309
  end
306
310
  return count
@@ -314,7 +318,7 @@ module Osm
314
318
  count = 0
315
319
 
316
320
  required.each do |section, needed|
317
- next if gained[section] >= needed
321
+ next if gained[section] < needed
318
322
  count += 1
319
323
  end
320
324
  return count
@@ -328,22 +332,59 @@ module Osm
328
332
  field = field.split('_')[0]
329
333
  unless field.eql?('y')
330
334
  count[field] ||= 0
331
- next if data.blank? || data.to_s[0].downcase.eql?('x')
335
+ next unless reguiremet_met?(data)
332
336
  count[field] += 1
333
337
  else
334
338
  # A total 'section'
335
- count['a'] = data.to_i
339
+ count['y'] = data.to_i
336
340
  end
337
341
  end
338
342
  return count
339
343
  end
340
344
 
341
- # Check if this badge is due
345
+ # Check if this badge is due (according data retrieved from OSM)
342
346
  # @return [Boolean] whether the badge is due to the member
343
347
  def due?
344
348
  completed > awarded
345
349
  end
346
350
 
351
+
352
+ # Check if this badge has been earnt
353
+ # @return [Boolean] whether the badge is due to the member
354
+ def earnt?
355
+ if badge.type == :staged
356
+ return (earnt > awarded)
357
+ end
358
+ return false if (completed.eql?(1) && awarded.eql?(1))
359
+ return true if (completed.eql?(1) && awarded.eql?(0))
360
+ return (total_gained >= badge.total_needed) && (sections_gained >= badge.sections_needed)
361
+ end
362
+
363
+ # Get what stage which has most recently been earnt
364
+ # (using #earnt? will tell you if it's still due (not yet awarded))
365
+ # @return [Fixnum] the stage which has most recently been due
366
+ def earnt
367
+ unless badge.type == :staged
368
+ return earnt? ? 1 : 0
369
+ end
370
+ if ['nightsaway', 'hikes'].include?(badge.osm_key)
371
+ total_done = requirements['y_01']
372
+ stages = STAGES_NIGHTSAWAY if badge.osm_key.eql?('nightsaway')
373
+ stages = STAGES_HIKESAWAY if badge.osm_key.eql?('hikes')
374
+ stages.reverse_each do |stage|
375
+ return stage if total_done >= stage
376
+ end
377
+ else
378
+ (awarded..5).reverse_each do |stage|
379
+ group = 'abcde'[stage - 1]
380
+ if gained_in_sections[group] >= badge.needed_from_section[group]
381
+ return stage
382
+ end
383
+ end
384
+ end
385
+ return 0
386
+ end
387
+
347
388
  # Check if this badge has been started
348
389
  # @return [Boolean] whether the badge has been started by the member (always false if the badge has been completed)
349
390
  def started?
@@ -352,9 +393,9 @@ module Osm
352
393
  requirements.each do |key, value|
353
394
  case key.split('_')[0]
354
395
  when 'a'
355
- return true unless value.blank? || value.to_s[0].downcase.eql?('x')
396
+ return true if reguiremet_met?(value)
356
397
  when 'y'
357
- return true if (requirements['y_01'].to_i > 0)
398
+ return true if (value.to_i > 0)
358
399
  end
359
400
  end
360
401
  return false
@@ -368,8 +409,8 @@ module Osm
368
409
  else
369
410
  # Staged badge
370
411
  if ['nightsaway', 'hikes'].include?(badge.osm_key) # Special staged badges
371
- stages = [1, 5, 10, 20, 35, 50, 75, 100, 125, 150, 175, 200] if badge.osm_key.eql?('nightsaway')
372
- stages = [1, 5, 10, 20, 35, 50] if badge.osm_key.eql?('hikes')
412
+ stages = STAGES_NIGHTSAWAY if badge.osm_key.eql?('nightsaway')
413
+ stages = STAGES_HIKESAWAY if badge.osm_key.eql?('hikes')
373
414
  done = requirements['y_01'].to_i
374
415
  return 0 if done < stages[0] # Not started the first stage
375
416
  return 0 if done >= stages[stages.size - 1] # No more stages can be started
@@ -493,6 +534,12 @@ module Osm
493
534
  Osm.inspect_instance(self, options={:replace_with => {'badge' => :osm_key}})
494
535
  end
495
536
 
537
+ private
538
+ def reguiremet_met?(data)
539
+ return false if data == 0
540
+ !(data.blank? || data.to_s[0].downcase.eql?('x'))
541
+ end
542
+
496
543
  end # Class Data
497
544
 
498
545
  end # Class Badge
@@ -80,6 +80,8 @@ module Osm
80
80
  # @return [String] the member's current age (yy/mm)
81
81
  # @!attribute [rw] joined_years
82
82
  # @return [Fixnum] how many years the member has been in Scouting
83
+ # @!attribute [rw] has_photo
84
+ # @return [Boolean] whether the scout has a photo in OSM
83
85
 
84
86
  attribute :id, :type => Integer
85
87
  attribute :section_id, :type => Integer
@@ -120,12 +122,14 @@ module Osm
120
122
  attribute :joined, :type => Date
121
123
  attribute :age, :type => String
122
124
  attribute :joined_years, :type => Integer
125
+ attribute :has_photo, :type => Boolean, :default => false
123
126
 
124
127
  attr_accessible :id, :section_id, :type, :first_name, :last_name, :email1, :email2, :email3, :email4,
125
128
  :phone1, :phone2, :phone3, :phone4, :address, :address2, :date_of_birth, :started,
126
129
  :joining_in_years, :parents, :notes, :medical, :religion, :school, :ethnicity, :subs,
127
130
  :custom1, :custom2, :custom3, :custom4, :custom5, :custom6, :custom7, :custom8, :custom9,
128
- :grouping_id, :grouping_leader, :joined, :age, :joined_years
131
+ :grouping_id, :grouping_leader, :joined, :age, :joined_years,
132
+ :has_photo
129
133
 
130
134
  validates_numericality_of :id, :only_integer=>true, :greater_than=>0, :unless => Proc.new { |r| r.id.nil? }
131
135
  validates_numericality_of :section_id, :only_integer=>true, :greater_than=>0
@@ -139,6 +143,7 @@ module Osm
139
143
  validates_presence_of :started
140
144
  validates_presence_of :joined
141
145
  validates_format_of :age, :with => /\A[0-9]{2}\/(0[0-9]|1[012])\Z/, :message => 'age is not in the correct format (yy/mm)', :allow_blank => true
146
+ validates_inclusion_of :has_photo, :in => [true, false]
142
147
 
143
148
 
144
149
  # Get members for a section
@@ -159,12 +164,17 @@ module Osm
159
164
  end
160
165
 
161
166
  data = api.perform_query("users.php?action=getUserDetails&sectionid=#{section.id}&termid=#{term_id}")
167
+ summary_data = api.perform_query("ext/members/contact/?action=getListOfMembers&sort=patrolid&sectionid=#{section.id}&termid=#{term_id}&section=#{section.type}") || {}
168
+
169
+ summary_data = summary_data['items'] || []
170
+ summary_data = Hash[summary_data.map { |i| [i['scoutid'].to_i, i] }]
162
171
 
163
172
  result = Array.new
164
173
  data['items'].each do |item|
174
+ id = Osm::to_i_or_nil(item['scoutid'])
165
175
  result.push Osm::Member.new(
166
176
  :section_id => section.id,
167
- :id => Osm::to_i_or_nil(item['scoutid']),
177
+ :id => id,
168
178
  :type => item['type'],
169
179
  :first_name => item['firstname'],
170
180
  :last_name => item['lastname'],
@@ -202,6 +212,7 @@ module Osm
202
212
  :joined => Osm::parse_date(item['joined']),
203
213
  :age => item['age'].gsub(' ', ''),
204
214
  :joined_years => item['yrs'].to_i,
215
+ :has_photo => summary_data[id]['pic']
205
216
  )
206
217
  end
207
218
 
@@ -386,11 +397,36 @@ module Osm
386
397
  return @myscout_link_key
387
398
  end
388
399
 
400
+ # Get the member's photo
401
+ # @param [Osm::Api] api The api to use to make the request
402
+ # @param [Boolean] black_and_white Whether you want the photo in blank and white
403
+ # @!macro options_get
404
+ # @raise [Osm:Error] if the member has no photo or doesn't exist in OSM
405
+ # @return the photo of the member
406
+ def get_photo(api, black_and_white=false, options={})
407
+ raise Osm::ObjectIsInvalid, 'member is invalid' unless valid?
408
+ require_ability_to(api, :read, :member, section_id)
409
+ raise Osm::Error, 'the member does not already exist in OSM' if id.nil?
410
+ raise Osm::Error, "the member doesn't have a photo in OSM" unless has_photo
411
+
412
+ cache_key = ['member_photo', self.id, black_and_white]
413
+
414
+ if !options[:no_cache] && cache_exist?(api, cache_key)
415
+ return cache_read(api, cache_key)
416
+ end
417
+
418
+ url = "ext/members/contact/images/member.php?sectionid=#{section_id}&scoutid=#{self.id}&bw=#{black_and_white}"
419
+ image = api.perform_query(url)
420
+
421
+ cache_write(api, cache_key, image) unless image.nil?
422
+ return image
423
+ end
424
+
389
425
  # Get the My.SCOUT link for this member
390
426
  # @param [Osm::Api] api The api to use to make the request
391
427
  # @param [Symbol] link_to The page in My.SCOUT to link to (:payments, :events, :programme, :badges, :notice or :details)
392
428
  # @param [#to_i] item_id Allows you to link to a specfic item (only for :events)
393
- # @return [String] the link for this member's My.SCOUT
429
+ # @return [String] the URL for this member's My.SCOUT
394
430
  # @raise [Osm::ObjectIsInvalid] If the Member is invalid
395
431
  # @raise [Osm::ArgumentIsInvalid] If link_to is not an allowed Symbol
396
432
  # @raise [Osm::Error] if the member does not already exist in OSM or the member's My.SCOUT key could not be retrieved from OSM
@@ -400,7 +436,7 @@ module Osm
400
436
  raise Osm::Error, 'the member does not already exist in OSM' if id.nil?
401
437
  raise Osm::ArgumentIsInvalid, 'link_to is invalid' unless [:payments, :events, :programme, :badges, :notice, :details].include?(link_to)
402
438
 
403
- link = "https://www.onlinescoutmanager.co.uk/parents/#{link_to}.php?sc=#{self.id}&se=#{section_id}&c=#{myscout_link_key(api)}"
439
+ link = "#{api.base_url}/parents/#{link_to}.php?sc=#{self.id}&se=#{section_id}&c=#{myscout_link_key(api)}"
404
440
  link += "&e=#{item_id.to_i}" if item_id && link_to.eql?(:events)
405
441
  return link
406
442
  end
@@ -102,7 +102,7 @@ describe "Activity" do
102
102
  }
103
103
  ]
104
104
  }
105
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/programme.php?action=getActivity&id=1", :body => body.to_json)
105
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/programme.php?action=getActivity&id=1", :body => body.to_json, :content_type => 'application/json')
106
106
 
107
107
 
108
108
  activity = Osm::Activity.get(@api, 1)
@@ -43,7 +43,7 @@ describe "API Access" do
43
43
  }
44
44
  ]
45
45
  }
46
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=getAPIAccess&sectionid=1", :body => body.to_json)
46
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=getAPIAccess&sectionid=1", :body => body.to_json, :content_type => 'application/json')
47
47
  end
48
48
 
49
49
  describe "Get All" do
@@ -79,7 +79,7 @@ describe "API" do
79
79
  {"sectionid"=>"1", "permissions"=>{"badge"=>20}},
80
80
  {"sectionid"=>"2", "permissions"=>{"badge"=>10}}
81
81
  ]
82
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getUserRoles", :body => body.to_json)
82
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getUserRoles", :body => body.to_json, :content_type => 'application/json')
83
83
 
84
84
  permissions = {1 => {:badge => [:read, :write]}, 2 => {:badge => [:read]}}
85
85
  OsmTest::Cache.should_not_receive('read')
@@ -122,6 +122,20 @@ describe "API" do
122
122
  end
123
123
 
124
124
 
125
+ describe "Get base URL" do
126
+ it "For the class" do
127
+ Osm::Api.base_url(:osm).should == 'https://www.onlinescoutmanager.co.uk'
128
+ Osm::Api.base_url(:ogm).should == 'http://www.onlineguidemanager.co.uk'
129
+ end
130
+
131
+ it "For an instance" do
132
+ @api.base_url.should == 'https://www.onlinescoutmanager.co.uk'
133
+ @api.base_url(:osm).should == 'https://www.onlinescoutmanager.co.uk'
134
+ @api.base_url(:ogm).should == 'http://www.onlineguidemanager.co.uk'
135
+ end
136
+ end
137
+
138
+
125
139
  describe "OSM and Internet error conditions:" do
126
140
  it "Raises a connection error if the HTTP status code was not 'OK'" do
127
141
  HTTParty.stub(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'500'}) }
@@ -146,4 +160,4 @@ describe "API" do
146
160
  end
147
161
  end
148
162
 
149
- end
163
+ end
@@ -95,9 +95,19 @@ describe "Badge" do
95
95
 
96
96
  it "Get total requirements gained for a member" do
97
97
  data = Osm::Badge::Data.new(
98
- :requirements => {'a_1' => 'x', 'a_2' => 'a', 'b_1' => 'yes', 'b_2' => '2000-01-02'}
98
+ :requirements => {
99
+ 'a_1' => 'a',
100
+ 'a_2' => 'yes',
101
+ 'a_3' => '2000-01-02',
102
+ 'a_4' => 1,
103
+ 'b_1' => 'x',
104
+ 'b_2' => 'xYES',
105
+ 'b_3' => '',
106
+ 'b_4' => nil,
107
+ 'b_5' => 0,
108
+ }
99
109
  )
100
- data.total_gained.should == 3
110
+ data.total_gained.should == 4
101
111
  end
102
112
 
103
113
  it "Get total requirements met in each section for a member" do
@@ -108,37 +118,140 @@ describe "Badge" do
108
118
  Osm::Badge::Requirement.new(:field => 'a_2'),
109
119
  Osm::Badge::Requirement.new(:field => 'b_1'),
110
120
  Osm::Badge::Requirement.new(:field => 'b_2'),
121
+ Osm::Badge::Requirement.new(:field => 'y_1'),
122
+ Osm::Badge::Requirement.new(:field => 'y_2'),
111
123
  ]
112
124
  )
113
125
  data = Osm::Badge::Data.new(
114
126
  :badge => badge,
115
- :requirements => {'a_1' => 'x', 'a_2' => '', 'b_1' => 'yes', 'b_2' => '2000-01-02'}
127
+ :requirements => {
128
+ 'a_1' => 'x',
129
+ 'a_2' => '',
130
+ 'b_1' => 'yes',
131
+ 'b_2' => '2000-01-02',
132
+ 'y_1' => 1,
133
+ }
116
134
  )
117
- data.gained_in_sections.should == {'a' => 0, 'b' => 2}
135
+ data.gained_in_sections.should == {'a' => 0, 'b' => 2, 'y' => 1}
118
136
  end
119
137
 
120
138
  it "Get number of sections met for a member" do
121
139
  badge = Osm::Badge.new(
122
- :needed_from_section => {'a' => 1, 'b' => 2},
140
+ :needed_from_section => {'a' => 1, 'b' => 2, 'c' => 1},
123
141
  :requirements => [
124
142
  Osm::Badge::Requirement.new(:field => 'a_1'),
125
143
  Osm::Badge::Requirement.new(:field => 'a_2'),
126
144
  Osm::Badge::Requirement.new(:field => 'b_1'),
127
145
  Osm::Badge::Requirement.new(:field => 'b_2'),
146
+ Osm::Badge::Requirement.new(:field => 'c_1'),
128
147
  ]
129
148
  )
130
149
  data = Osm::Badge::Data.new(
131
150
  :badge => badge,
132
- :requirements => {'a_1' => 'x', 'a_2' => '', 'b_1' => 'yes', 'b_2' => '2000-01-02'}
151
+ :requirements => {'a_1' => 'x', 'a_2' => '', 'b_1' => 'yes', 'b_2' => '2000-01-02', 'c_1' => 'yes'}
133
152
  )
134
- data.sections_gained.should == 1
153
+ data.sections_gained.should == 2
154
+ end
155
+
156
+ describe "Works out if the badge has been earnt" do
157
+ it "Staged" do
158
+ badge = Osm::StagedBadge.new(:osm_key => 'not_hikes_or_nights')
159
+ data = Osm::Badge::Data.new(:awarded => 2, :badge => badge)
160
+
161
+ data.stub(:earnt) { 1 }
162
+ data.earnt?.should be_false
163
+
164
+ data.stub(:earnt) { 2 }
165
+ data.earnt?.should be_false
166
+
167
+ data.stub(:earnt) { 3 }
168
+ data.earnt?.should be_true
169
+ end
170
+
171
+ it "Non staged" do
172
+ badge = Osm::ActivityBadge.new()
173
+ data = Osm::Badge::Data.new(:completed => 1, :awarded => 1, :badge => badge)
174
+ data.earnt?.should be_false
175
+
176
+ badge = Osm::ActivityBadge.new()
177
+ data = Osm::Badge::Data.new(:completed => 1, :awarded => 0, :badge => badge)
178
+ data.earnt?.should be_true
179
+
180
+
181
+ badge = Osm::ActivityBadge.new(:total_needed => 0, :sections_needed => 2, :needed_from_section => {'a' => 2, 'b' => 1})
182
+ data = Osm::Badge::Data.new(:requirements => {'a_01'=>'y', 'a_02'=>'y', 'b_01' => 'y'}, :completed => 0, :awarded => 0, :badge => badge)
183
+ data.earnt?.should be_true
184
+
185
+ badge = Osm::ActivityBadge.new(:total_needed => 0, :sections_needed => 2, :needed_from_section => {'a' => 2, 'b' => 1})
186
+ data = Osm::Badge::Data.new(:requirements => {'a_01'=>'y', 'a_02'=>'y', 'b_01' => 'x'}, :completed => 0, :awarded => 0, :badge => badge)
187
+ data.earnt?.should be_false
188
+
189
+ badge = Osm::ActivityBadge.new(:total_needed => 3, :sections_needed => 0, :needed_from_section => {'a' => 2, 'b' => 1})
190
+ data = Osm::Badge::Data.new(:requirements => {'a_01'=>'y', 'a_02'=>'y', 'b_01' => 'y'}, :completed => 0, :awarded => 0, :badge => badge)
191
+ data.earnt?.should be_true
192
+
193
+ badge = Osm::ActivityBadge.new(:total_needed => 3, :sections_needed => 0, :needed_from_section => {'a' => 2, 'b' => 1})
194
+ data = Osm::Badge::Data.new(:requirements => {'a_01'=>'y', 'a_02'=>'x', 'b_01' => 'y'}, :completed => 0, :awarded => 0, :badge => badge)
195
+ data.earnt?.should be_false
196
+
197
+ badge = Osm::ActivityBadge.new(:total_needed => 3, :sections_needed => 2, :needed_from_section => {'a' => 2, 'b' => 1})
198
+ data = Osm::Badge::Data.new(:requirements => {'a_01'=>'y', 'a_02'=>'y', 'b_01' => 'y'}, :completed => 0, :awarded => 0, :badge => badge)
199
+ data.earnt?.should be_true
200
+
201
+ badge = Osm::ActivityBadge.new(:total_needed => 1, :sections_needed => 1, :needed_from_section => {'a' => 2, 'b' => 1})
202
+ data = Osm::Badge::Data.new(:requirements => {'a_01'=>'y', 'a_02'=>'y', 'b_01' => 'y'}, :completed => 0, :awarded => 0, :badge => badge)
203
+ data.earnt?.should be_true
204
+ end
135
205
  end
136
206
 
137
- it "Works out if the badge is due" do
138
- Osm::Badge::Data.new(:completed => 0, :awarded => 0).due?.should be_false
139
- Osm::Badge::Data.new(:completed => 1, :awarded => 0).due?.should be_true
140
- Osm::Badge::Data.new(:completed => 2, :awarded => 2).due?.should be_false
141
- Osm::Badge::Data.new(:completed => 2, :awarded => 1).due?.should be_true
207
+ describe "Works out what level of a badge has been earnt" do
208
+ it "Staged" do
209
+ badge = Osm::StagedBadge.new(:osm_key => 'not_hikes_or_nights', :needed_from_section => {'a'=>1,'b'=>1,'c'=>1,'d'=>2,'e'=>2})
210
+
211
+ data = Osm::Badge::Data.new(:requirements=>{'a_01'=>'','b_01'=>'','c_01'=>'','d_01'=>'','d_02'=>'','e_01'=>'','e_02'=>''}, :badge=>badge)
212
+ data.earnt.should == 0
213
+
214
+ data = Osm::Badge::Data.new(:requirements=>{'a_01'=>'y','b_01'=>'','c_01'=>'','d_01'=>'','d_02'=>'','e_01'=>'','e_02'=>''}, :badge=>badge)
215
+ data.earnt.should == 1
216
+
217
+ data = Osm::Badge::Data.new(:requirements=>{'a_01'=>'y','b_01'=>'y','c_01'=>'','d_01'=>'y','d_02'=>'','e_01'=>'','e_02'=>''}, :badge=>badge)
218
+ data.earnt.should == 2
219
+
220
+ data = Osm::Badge::Data.new(:requirements=>{'a_01'=>'y','b_01'=>'y','c_01'=>'','d_01'=>'y','d_02'=>'y','e_01'=>'','e_02'=>''}, :badge=>badge)
221
+ data.earnt.should == 4
222
+ end
223
+
224
+ it "Nights away" do
225
+ badge = Osm::StagedBadge.new(:osm_key => 'nightsaway')
226
+
227
+ Osm::Badge::Data.new(:requirements => {'y_01'=>3}, :badge => badge).earnt.should == 1
228
+ Osm::Badge::Data.new(:requirements => {'y_01'=>5}, :badge => badge).earnt.should == 5
229
+ Osm::Badge::Data.new(:requirements => {'y_01'=>6}, :badge => badge).earnt.should == 5
230
+ Osm::Badge::Data.new(:requirements => {'y_01'=>199}, :badge => badge).earnt.should == 175
231
+ Osm::Badge::Data.new(:requirements => {'y_01'=>200}, :badge => badge).earnt.should == 200
232
+ Osm::Badge::Data.new(:requirements => {'y_01'=>999}, :badge => badge).earnt.should == 200
233
+ end
234
+
235
+ it "Hikes away" do
236
+ badge = Osm::StagedBadge.new(:osm_key => 'hikes')
237
+
238
+ Osm::Badge::Data.new(:requirements => {'y_01'=>3}, :badge => badge).earnt.should == 1
239
+ Osm::Badge::Data.new(:requirements => {'y_01'=>5}, :badge => badge).earnt.should == 5
240
+ Osm::Badge::Data.new(:requirements => {'y_01'=>6}, :badge => badge).earnt.should == 5
241
+ Osm::Badge::Data.new(:requirements => {'y_01'=>49}, :badge => badge).earnt.should == 35
242
+ Osm::Badge::Data.new(:requirements => {'y_01'=>50}, :badge => badge).earnt.should == 50
243
+ Osm::Badge::Data.new(:requirements => {'y_01'=>999}, :badge => badge).earnt.should == 50
244
+ end
245
+
246
+ it "Non staged" do
247
+ data = Osm::Badge::Data.new(:badge => Osm::ActivityBadge.new)
248
+
249
+ data.stub(:earnt?) { true }
250
+ data.earnt.should == 1
251
+
252
+ data.stub(:earnt?) { false }
253
+ data.earnt.should == 0
254
+ end
142
255
  end
143
256
 
144
257
  it "Works out if the badge has been started" do
@@ -285,7 +398,7 @@ describe "Badge" do
285
398
  end
286
399
 
287
400
  it "Core" do
288
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=core&sectionid=1&section=beavers&termid=2", :body => @data)
401
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=core&sectionid=1&section=beavers&termid=2", :body => @data, :content_type => 'application/json')
289
402
  Osm::Term.stub(:get_current_term_for_section){ Osm::Term.new(:id => 2) }
290
403
 
291
404
  badges = Osm::CoreBadge.get_badges_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers))
@@ -307,7 +420,7 @@ describe "Badge" do
307
420
  end
308
421
 
309
422
  it "Challenge" do
310
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=challenge&sectionid=1&section=beavers&termid=2", :body => @data)
423
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=challenge&sectionid=1&section=beavers&termid=2", :body => @data, :content_type => 'application/json')
311
424
  Osm::Term.stub(:get_current_term_for_section){ Osm::Term.new(:id => 2) }
312
425
 
313
426
  badges = Osm::ChallengeBadge.get_badges_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers))
@@ -329,7 +442,7 @@ describe "Badge" do
329
442
  end
330
443
 
331
444
  it "Staged" do
332
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=staged&sectionid=1&section=beavers&termid=2", :body => @data)
445
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=staged&sectionid=1&section=beavers&termid=2", :body => @data, :content_type => 'application/json')
333
446
  Osm::Term.stub(:get_current_term_for_section){ Osm::Term.new(:id => 2) }
334
447
 
335
448
  badges = Osm::StagedBadge.get_badges_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers))
@@ -351,7 +464,7 @@ describe "Badge" do
351
464
  end
352
465
 
353
466
  it "Activity" do
354
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=activity&sectionid=1&section=beavers&termid=2", :body => @data)
467
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=activity&sectionid=1&section=beavers&termid=2", :body => @data, :content_type => 'application/json')
355
468
  Osm::Term.stub(:get_current_term_for_section){ Osm::Term.new(:id => 2) }
356
469
 
357
470
  badges = Osm::ActivityBadge.get_badges_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers))
@@ -373,7 +486,7 @@ describe "Badge" do
373
486
  end
374
487
 
375
488
  it "For a different section type" do
376
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=activity&sectionid=1&section=cubs&termid=2", :body => @data)
489
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=activity&sectionid=1&section=cubs&termid=2", :body => @data, :content_type => 'application/json')
377
490
  Osm::Term.stub(:get_current_term_for_section){ Osm::Term.new(:id => 2) }
378
491
 
379
492
  badges = Osm::ActivityBadge.get_badges_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers), :cubs)
@@ -404,7 +517,7 @@ describe "Badge" do
404
517
  end
405
518
 
406
519
  it "Core badge" do
407
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?termid=2&type=core&section=beavers&c=badge&sectionid=1", :body => @data)
520
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?termid=2&type=core&section=beavers&c=badge&sectionid=1", :body => @data, :content_type => 'application/json')
408
521
  datas = Osm::CoreBadge.new(:osm_key => 'badge').get_data_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers), 2)
409
522
  datas.size.should == 1
410
523
  data = datas[0]
@@ -420,7 +533,7 @@ describe "Badge" do
420
533
  end
421
534
 
422
535
  it "Challenge badge" do
423
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?termid=2&type=challenge&section=beavers&c=badge&sectionid=1", :body => @data)
536
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?termid=2&type=challenge&section=beavers&c=badge&sectionid=1", :body => @data, :content_type => 'application/json')
424
537
  datas = Osm::ChallengeBadge.new(:osm_key => 'badge').get_data_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers), 2)
425
538
  datas.size.should == 1
426
539
  data = datas[0]
@@ -436,7 +549,7 @@ describe "Badge" do
436
549
  end
437
550
 
438
551
  it "Staged badge" do
439
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?termid=2&type=staged&section=beavers&c=badge&sectionid=1", :body => @data)
552
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?termid=2&type=staged&section=beavers&c=badge&sectionid=1", :body => @data, :content_type => 'application/json')
440
553
  datas = Osm::StagedBadge.new(:osm_key => 'badge').get_data_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers), 2)
441
554
  datas.size.should == 1
442
555
  data = datas[0]
@@ -452,7 +565,7 @@ describe "Badge" do
452
565
  end
453
566
 
454
567
  it "Activity badge" do
455
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?termid=2&type=activity&section=beavers&c=badge&sectionid=1", :body => @data)
568
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?termid=2&type=activity&section=beavers&c=badge&sectionid=1", :body => @data, :content_type => 'application/json')
456
569
  datas = Osm::ActivityBadge.new(:osm_key => 'badge').get_data_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers), 2)
457
570
  datas.size.should == 1
458
571
  data = datas[0]
@@ -713,7 +826,7 @@ describe "Badge" do
713
826
  end
714
827
 
715
828
  it "Core badge" do
716
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=summary&section=beavers&sectionid=1&termid=2&type=core", :body => @data)
829
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=summary&section=beavers&sectionid=1&termid=2&type=core", :body => @data, :content_type => 'application/json')
717
830
  summary = Osm::CoreBadge.get_summary_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers), 2)
718
831
  summary.size.should == 1
719
832
  summary[0].should == {
@@ -725,7 +838,7 @@ describe "Badge" do
725
838
  end
726
839
 
727
840
  it "Challenge badge" do
728
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=summary&section=beavers&sectionid=1&termid=2&type=challenge", :body => @data)
841
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=summary&section=beavers&sectionid=1&termid=2&type=challenge", :body => @data, :content_type => 'application/json')
729
842
  summary = Osm::ChallengeBadge.get_summary_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers), 2)
730
843
  summary.size.should == 1
731
844
  summary[0].should == {
@@ -737,7 +850,7 @@ describe "Badge" do
737
850
  end
738
851
 
739
852
  it "Staged badge" do
740
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=summary&section=beavers&sectionid=1&termid=2&type=staged", :body => @data)
853
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=summary&section=beavers&sectionid=1&termid=2&type=staged", :body => @data, :content_type => 'application/json')
741
854
  summary = Osm::StagedBadge.get_summary_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers), 2)
742
855
  summary.size.should == 1
743
856
  summary[0].should == {
@@ -749,7 +862,7 @@ describe "Badge" do
749
862
  end
750
863
 
751
864
  it "Activity badge" do
752
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=summary&section=beavers&sectionid=1&termid=2&type=activity", :body => @data)
865
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=summary&section=beavers&sectionid=1&termid=2&type=activity", :body => @data, :content_type => 'application/json')
753
866
  summary = Osm::ActivityBadge.get_summary_for_section(@api, Osm::Section.new(:id => 1, :type => :beavers), 2)
754
867
  summary.size.should == 1
755
868
  summary[0].should == {