osm 0.0.21 → 0.0.22

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,10 +1,21 @@
1
+ ## Version 0.0.22
2
+
3
+ * Adjustments so DueBadge is similar enough to badge\_stock data to be useful:
4
+ * by\_member Hash -> Keys are member's name (String), values are the badge key (String)
5
+ * descriptions Hash -> keys are the badge key (String), values are the badge name (String)
6
+ * totals Hash -> keys are the badge key (String), values are the number required (Fixnum)
7
+ * Badge keys are the same as are used in getting badge stock levels
8
+ * Add ability to get badge stock levels
9
+ * Add ability to Create a term
10
+ * Add ability to Update a term
11
+
1
12
  ## Version 0.0.21
2
13
 
3
14
  * Fix getting section\_id and grouping\_id for api.get\_members
4
15
 
5
16
  ## Version 0.0.20
6
17
 
7
- * Start deprecation of api\_data option in api methods (raise a warning and adjust documentation)
18
+ * Deprecation of api\_data option in api methods (raise a warning if used and adjust documentation)
8
19
  * Hide sesitive information when printing data sent to OSM in debug mode
9
20
  * Add archived attribute to Event
10
21
  * Add :include\_archived option to api.get\_events method
data/README.md CHANGED
@@ -72,6 +72,7 @@ however it should be noted that when the OSM API adds a feature it can be diffic
72
72
  * API Access
73
73
  * API Access for our app
74
74
  * Due Badges
75
+ * Evening
75
76
  * Events
76
77
  * Flexi Record Data
77
78
  * Flexi Record Structure
@@ -100,25 +101,30 @@ however it should be noted that when the OSM API adds a feature it can be diffic
100
101
 
101
102
  ## Parts of the OSM API currently NOT supported:
102
103
 
104
+ * Badges:
105
+ * Which requirements each member has met:
106
+ * Retreive
107
+ * Update
108
+ * Retrieve details for each badge (stock, short column names etc.)
103
109
  * Retreival of leader access
104
- * Terms:
105
- * Create
106
- * Update
107
110
  * Register - Update attendance
108
- * Flexi Record - Update data
111
+ * Flexi Records:
112
+ * Update data
113
+ * Delete
114
+ * Update
115
+ * Add Column
109
116
  * Events:
110
117
  * Attendance (everything)
111
118
  * Create
112
119
  * Update
120
+ * Delete
113
121
  * Member:
114
122
  * Update
115
123
  * Add
116
- * Badges:
117
- * Which requirements each member has met:
118
- * Retreive
119
- * Update
120
- * Retrieve details for each badge (stock, short column names etc.)
124
+ * Delete
121
125
  * Update Activity
126
+ * Add activity to programme
127
+ * Delete Evening
122
128
  * Gift aid (everything)
123
129
  * Finances (Everything)
124
130
  * SMS:
data/lib/osm/api.rb CHANGED
@@ -163,7 +163,7 @@ module Osm
163
163
  end
164
164
 
165
165
  # Get the notepad for a specified section
166
- # @param [Osm:Section, Fixnum] section the section (or its ID) to get the notepad for
166
+ # @param [Osm::Section, Fixnum] section the section (or its ID) to get the notepad for
167
167
  # @!macro options_get
168
168
  # @!macro options_api_data
169
169
  # @return nil if an error occured or the user does not have access to that section
@@ -291,8 +291,8 @@ module Osm
291
291
  end
292
292
 
293
293
  # Get the programme for a given term
294
- # @param [Osm:Section, Fixnum] section the section (or its ID) to get the programme for
295
- # @param [Osm:term, Fixnum] term the term (or its ID) to get the programme for, passing nil causes the current term to be used
294
+ # @param [Osm::Section, Fixnum] section the section (or its ID) to get the programme for
295
+ # @param [Osm::term, Fixnum, nil] term the term (or its ID) to get the programme for, passing nil causes the current term to be used
296
296
  # @!macro options_get
297
297
  # @!macro options_api_data
298
298
  # @return [Array<Osm::Evening>]
@@ -356,8 +356,8 @@ module Osm
356
356
  end
357
357
 
358
358
  # Get members
359
- # @param [Osm:Section, Fixnum] section the section (or its ID) to get the members for
360
- # @param [Osm:Term, Fixnum] term the term (or its ID) to get the members for, passing nil causes the current term to be used
359
+ # @param [Osm::Section, Fixnum] section the section (or its ID) to get the members for
360
+ # @param [Osm::Term, Fixnum, nil] term the term (or its ID) to get the members for, passing nil causes the current term to be used
361
361
  # @!macro options_get
362
362
  # @!macro options_api_data
363
363
  # @return [Array<Osm::Member>]
@@ -384,7 +384,7 @@ module Osm
384
384
  end
385
385
 
386
386
  # Get API access details for a given section
387
- # @param [Osm:Section, Fixnum] section the section (or its ID) to get the details for
387
+ # @param [Osm::Section, Fixnum] section the section (or its ID) to get the details for
388
388
  # @!macro options_get
389
389
  # @!macro options_api_data
390
390
  # @return [Array<Osm::ApiAccess>]
@@ -414,7 +414,7 @@ module Osm
414
414
  end
415
415
 
416
416
  # Get our API access details for a given section
417
- # @param [Osm:Section, Fixnum] section the section (or its ID) to get the details for
417
+ # @param [Osm::Section, Fixnum] section the section (or its ID) to get the details for
418
418
  # @!macro options_get
419
419
  # @!macro options_api_data
420
420
  # @return [Osm::ApiAccess]
@@ -437,7 +437,7 @@ module Osm
437
437
  end
438
438
 
439
439
  # Get events
440
- # @param [Osm:Section, Fixnum] section the section (or its ID) to get the events for
440
+ # @param [Osm::Section, Fixnum] section the section (or its ID) to get the events for
441
441
  # @!macro options_get
442
442
  # @option options [Boolean] :include_archived (optional) if true then archived activities will also be returned
443
443
  # @!macro options_api_data
@@ -471,7 +471,8 @@ module Osm
471
471
  end
472
472
 
473
473
  # Get due badges
474
- # @param [Osm:Section, Fixnum] section the section (or its ID) to get the due badges for
474
+ # @param [Osm::Section, Fixnum] section the section (or its ID) to get the due badges for
475
+ # @param [Osm::Term, Fixnum, nil] term the term (or its ID) to get the due badges for, passing nil causes the current term to be used
475
476
  # @!macro options_get
476
477
  # @!macro options_api_data
477
478
  # @return [Osm::DueBadges]
@@ -496,8 +497,8 @@ module Osm
496
497
  end
497
498
 
498
499
  # Get register structure
499
- # @param [Osm:Section, Fixnum] section the section (or its ID) to get the structure for
500
- # @param [Osm:Term, Fixnum] section the term (or its ID) to get the structure for, passing nil causes the current term to be used
500
+ # @param [Osm::Section, Fixnum] section the section (or its ID) to get the structure for
501
+ # @param [Osm::Term, Fixnum, nil] section the term (or its ID) to get the structure for, passing nil causes the current term to be used
501
502
  # @!macro options_get
502
503
  # @!macro options_api_data
503
504
  # @return [Array<Osm::RegisterField>] representing the fields of the register
@@ -526,8 +527,8 @@ module Osm
526
527
  end
527
528
 
528
529
  # Get register data
529
- # @param [Osm:Section, Fixnum] section the section (or its ID) to get the register for
530
- # @param [Osm:Term, Fixnum] section the term (or its ID) to get the register for, passing nil causes the current term to be used
530
+ # @param [Osm::Section, Fixnum] section the section (or its ID) to get the register for
531
+ # @param [Osm::Term, Fixnum, nil] section the term (or its ID) to get the register for, passing nil causes the current term to be used
531
532
  # @!macro options_get
532
533
  # @!macro options_api_data
533
534
  # @return [Array<RegisterData>] representing the attendance of each member
@@ -554,7 +555,7 @@ module Osm
554
555
  end
555
556
 
556
557
  # Get flexirecord structure
557
- # @param [Osm:Section, Fixnum] section the section (or its ID) to get the structure for
558
+ # @param [Osm::Section, Fixnum] section the section (or its ID) to get the structure for
558
559
  # @param [Fixnum] the id of the Flexi Record
559
560
  # @!macro options_get
560
561
  # @!macro options_api_data
@@ -585,7 +586,7 @@ module Osm
585
586
  # Get flexi record data
586
587
  # @param [Osm:Section, Fixnum] section the section (or its ID) to get the register for
587
588
  # @param [Fixnum] the id of the Flexi Record
588
- # @param [Osm:Term, Fixnum] section the term (or its ID) to get the register for, passing nil causes the current term to be used
589
+ # @param [Osm:Term, Fixnum, nil] section the term (or its ID) to get the register for, passing nil causes the current term to be used
589
590
  # @!macro options_get
590
591
  # @!macro options_api_data
591
592
  # @return [Array<FlexiRecordData>]
@@ -611,8 +612,32 @@ module Osm
611
612
  return to_return
612
613
  end
613
614
 
615
+ # Get badge stock levels
616
+ # @param [Osm::Section, Fixnum] section the section (or its ID) to get the stock levels for
617
+ # @param [Osm::Term, Fixnum, nil] section the term (or its ID) to get the stock levels for, passing nil causes the current term to be used
618
+ # @!macro options_get
619
+ # @return Hash
620
+ def get_badge_stock_levels(section, term=nil, options={})
621
+ section_id = id_for_section(section)
622
+ section_type = type_for_section(section)
623
+ term_id = id_for_term(term, section)
624
+ cache_key = "badge_stock-#{section_id}-#{term_id}"
625
+
626
+ if !options[:no_cache] && cache_exist?(cache_key) && self.user_can_access?(:badge, section_id)
627
+ return cache_read(cache_key)
628
+ end
629
+
630
+ data = perform_query("challenges.php?action=getInitialBadges&type=core&sectionid=#{section_id}&section=#{section_type}&termid=#{term_id}")
631
+ data = (data['stock'] || {}).select{ |k,v| !k.eql?('sectionid') }
632
+
633
+ self.user_can_access :badge, section_id
634
+ cache_write(cache_key, data, :expires_in => @@default_cache_ttl)
635
+ return data
636
+ end
637
+
638
+
614
639
  # Create an evening in OSM
615
- # @param [Fixnum] section_id the id of the section to add the term to
640
+ # @param [Osm::Section, Fixnum] section or section_id to add the evening to
616
641
  # @param [Date] meeting_date the date of the meeting
617
642
  # @!macro options_api_data
618
643
  # @return [Boolean] if the operation suceeded or not
@@ -635,6 +660,39 @@ module Osm
635
660
  return data.is_a?(Hash) && (data['result'] == 0)
636
661
  end
637
662
 
663
+ # Create a term in OSM
664
+ # @param [Hash] options - the configuration of the new term
665
+ # @option options [Osm::Section, Fixnum] :section (required) section or section_id to add the term to
666
+ # @option options [String] :name (required) the name for the term
667
+ # @option options [Date] :start (required) the date for the start of term
668
+ # @option options [Date] :finish (required) the date for the finish of term
669
+ # @return [Boolean] if the operation suceeded or not
670
+ def create_term(options={})
671
+ raise ArgumentError, ":section can't be nil" if options[:section].nil?
672
+ raise ArgumentError, ":name can't be nil" if options[:name].nil?
673
+ raise ArgumentError, ":start can't be nil" if options[:start].nil?
674
+ raise ArgumentError, ":finish can't be nil" if options[:finish].nil?
675
+
676
+ section_id = id_for_section(options[:section])
677
+ api_data = {
678
+ 'term' => options[:name],
679
+ 'start' => options[:start].strftime(Osm::OSM_DATE_FORMAT),
680
+ 'end' => options[:finish].strftime(Osm::OSM_DATE_FORMAT),
681
+ 'termid' => '0'
682
+ }
683
+
684
+ data = perform_query("users.php?action=addTerm&sectionid=#{section_id}", api_data)
685
+
686
+ # The cached terms for the section will be out of date - remove them
687
+ get_terms.each do |term|
688
+ cache_delete("term-#{term.id}") if term.section_id == section_id
689
+ end
690
+ cache_delete("terms-#{@userid}")
691
+
692
+ return data.is_a?(Hash) && data['terms'].is_a?(Hash)
693
+ end
694
+
695
+
638
696
  # Update an evening in OSM
639
697
  # @param [Osm::Evening] evening the evening to update
640
698
  # @!macro options_api_data
@@ -652,6 +710,22 @@ module Osm
652
710
  return response.is_a?(Hash) && (response['result'] == 0)
653
711
  end
654
712
 
713
+ # Update a term in OSM
714
+ # @param [Osm::Term] term the term to update
715
+ # @return [Boolean] if the operation suceeded or not
716
+ def update_term(term)
717
+ raise ArgumentIsInvalid, 'term is invalid' unless term.valid?
718
+
719
+ data = perform_query("users.php?action=addTerm&sectionid=#{term.section_id}", term.to_api)
720
+
721
+ # The cached terms for the section will be out of date - remove them
722
+ cache_delete("term-#{term.id}")
723
+ cache_delete("terms-#{@userid}")
724
+
725
+ return data.is_a?(Hash) && data['terms'].is_a?(Hash)
726
+ end
727
+
728
+
655
729
 
656
730
  protected
657
731
  # Set access permission for the current user on a resource stored in the cache
@@ -660,7 +734,7 @@ module Osm
660
734
  # @param [Hash] api_data the data hash used in accessing the api
661
735
  # @param [Boolean] permission wether the user can access the resource
662
736
  # @return [Boolean] the permission which was set
663
- def user_can_access(resource_type, resource_id, api_data, permission=true)
737
+ def user_can_access(resource_type, resource_id, api_data={}, permission=true)
664
738
  user = (api_data['userid'] || @userid).to_i
665
739
  resource_id = resource_id.to_i
666
740
  resource_type = resource_type.to_sym
@@ -677,7 +751,7 @@ module Osm
677
751
  # @param [Hash] api_data the data hash used in accessing the api
678
752
  # @return nil if the combination of user and resource has not been set
679
753
  # @return [Boolean] if the user can access the resource
680
- def user_can_access?(resource_type, resource_id, api_data)
754
+ def user_can_access?(resource_type, resource_id, api_data={})
681
755
  user = (api_data['userid'] || @userid).to_i
682
756
  resource_id = resource_id.to_i
683
757
  resource_type = resource_type.to_sym
@@ -785,11 +859,11 @@ module Osm
785
859
  id_for(Osm::Section, section, 'section')
786
860
  end
787
861
 
788
- def type_for_section(section, api_data)
862
+ def type_for_section(section, api_data={})
789
863
  (section.is_a?(Osm::Section) ? section : get_section(section, api_data)).type.to_s
790
864
  end
791
865
 
792
- def id_for_term(term, section, api_data)
866
+ def id_for_term(term, section, api_data={})
793
867
  return term.nil? ? Osm::find_current_term_id(self, id_for_section(section), api_data) : id_for(Osm::Term, term, 'term')
794
868
  end
795
869
 
@@ -14,28 +14,17 @@ module Osm
14
14
 
15
15
  attr_accessible :descriptions, :by_member
16
16
 
17
- validates_each :descriptions do |record, attr, value|
18
- record.errors.add(attr, 'must be a Hash') unless value.is_a?(Hash)
19
- value.each do |k, v|
20
- record.errors.add(attr, 'keys must be Symbols') unless k.is_a?(Symbol)
21
- record.errors.add(attr, 'values must be Hashes') unless v.is_a?(Hash)
22
- [:name, :section, :type, :badge].each do |key|
23
- record.errors.add(attr, "values must include the key #{key}") unless v.keys.include?(key)
24
- end
25
- end
26
- end
17
+ validates :descriptions, :hash => {:key_type => String, :value_type => String}
27
18
 
28
19
  validates_each :by_member do |record, attr, value|
20
+ desc_keys = record.descriptions.keys
29
21
  record.errors.add(attr, 'must be a Hash') unless value.is_a?(Hash)
30
22
  value.each do |k, v|
31
23
  record.errors.add(attr, 'keys must be String') unless k.is_a?(String)
32
24
  record.errors.add(attr, 'values must be Arrays') unless v.is_a?(Array)
33
25
  v.each do |vv|
34
- record.errors.add(attr, 'internal values must be Hashes') unless vv.is_a?(Hash)
35
- record.errors.add(attr, 'internal values must include the key :badge') unless vv.keys.include?(:badge)
36
- record.errors.add(attr, 'internal values :badge value must be a Symbol') unless vv[:badge].is_a?(Symbol)
37
- record.errors.add(attr, 'internal values must include the key :extra_information') unless vv.keys.include?(:extra_information)
38
- record.errors.add(attr, 'internal values :extra_information value must be a String') unless vv[:extra_information].is_a?(String)
26
+ record.errors.add(attr, 'internal values must be Strings') unless vv.is_a?(String)
27
+ record.errors.add(attr, 'internal values must exist as a key in :descriptions') unless desc_keys.include?(vv)
39
28
  end
40
29
  end
41
30
  end
@@ -49,39 +38,27 @@ module Osm
49
38
  # Initialize a new DueBadges from api data
50
39
  # @param [Hash] data the hash of data provided by the API
51
40
  def self.from_api(data)
52
- data = {} unless data.is_a?(Hash)
53
- attributes = {}
54
-
55
- attributes[:pending] = data['pending'].is_a?(Hash) ? Osm::symbolize_hash(data['pending']) : {}
56
- attributes[:descriptions] = data['description'].is_a?(Hash) ? Osm::symbolize_hash(data['description']) : {}
57
-
58
- attributes[:pending].each_key do |key|
59
- attributes[:pending][key].each_with_index do |item, index|
60
- attributes[:pending][key][index] = item = Osm::symbolize_hash(item)
61
- item[:sid] = item[:sid].to_i
62
- item[:completed] = item[:completed].to_i
63
- end
64
- end
65
- attributes[:descriptions].each_key do |key|
66
- attributes[:descriptions][key] = Osm::symbolize_hash(attributes[:descriptions][key])
67
- attributes[:descriptions][key][:section] = attributes[:descriptions][key][:section].to_sym
68
- attributes[:descriptions][key][:type] = attributes[:descriptions][key][:type].to_sym
69
- end
70
-
71
-
72
- attributes[:by_member] = {}
73
- attributes[:pending].each_key do |key|
74
- attributes[:pending][key].each do |item|
75
- name = "#{item[:firstname]} #{item[:lastname]}"
41
+ data = {} unless data.is_a?(Hash) # OSM returns an empty array to represent no badges
42
+ pending_raw = data['pending'] || {}
43
+ descriptions_raw = data['description'] || {}
44
+
45
+ attributes = {
46
+ :by_member => {},
47
+ :descriptions => {}
48
+ }
49
+
50
+ pending_raw.each do |key, members|
51
+ members.each do |member|
52
+ name = "#{member['firstname']} #{member['lastname']}"
53
+ description = descriptions_raw[key]['name'] + (descriptions_raw[key]['section'].eql?('staged') ? " (Level #{member['level']})" : '')
54
+ description_key = key + (descriptions_raw[key]['section'].eql?('staged') ? "_#{member['level']}" : '_1')
55
+
56
+ attributes[:descriptions][description_key] = description
76
57
  attributes[:by_member][name] ||= []
77
- badge = {
78
- :badge => key,
79
- :extra_information => item[:extra]
80
- }
81
- attributes[:by_member][name].push badge
58
+ attributes[:by_member][name].push(description_key)
82
59
  end
83
60
  end
84
-
61
+
85
62
  new(attributes)
86
63
  end
87
64
 
@@ -93,15 +70,12 @@ module Osm
93
70
 
94
71
  # Calculate the total number of badges needed
95
72
  # @return [Hash] the total number of each badge which is due
96
- def totals(attributes={})
73
+ def totals()
97
74
  totals = {}
98
75
  by_member.keys.each do |member_name|
99
- by_member[member_name].each do |badge_record|
100
- badge_symbol = badge_record[:badge]
101
- badge_extra = badge_record[:extra_information]
102
- totals[badge_record[:badge]] ||= {}
103
- totals[badge_symbol][badge_extra] ||= 0
104
- totals[badge_symbol][badge_extra] += 1
76
+ by_member[member_name].each do |badge|
77
+ totals[badge] ||= 0
78
+ totals[badge] += 1
105
79
  end
106
80
  end
107
81
  return totals
data/lib/osm/term.rb CHANGED
@@ -47,6 +47,17 @@ module Osm
47
47
  )
48
48
  end
49
49
 
50
+ # Get the term's data for use with the API
51
+ # @return [Hash]
52
+ def to_api
53
+ {
54
+ 'term' => name,
55
+ 'start' => start.strftime(Osm::OSM_DATE_FORMAT),
56
+ 'end' => finish.strftime(Osm::OSM_DATE_FORMAT),
57
+ 'termid' => id
58
+ }
59
+ end
60
+
50
61
  # Determine if the term is completly before the passed date
51
62
  # @param [Date] date
52
63
  # @return [Boolean] if the term is completly before the passed date
data/spec/osm/api_spec.rb CHANGED
@@ -401,6 +401,33 @@ describe "API" do
401
401
  end
402
402
 
403
403
 
404
+ it "Fetch badge stock levels for a section" do
405
+ badges_body = {
406
+ 'stock' => {
407
+ 'sectionid' => 1,
408
+ 'badge_1' => 1,
409
+ 'badge_2' => 2
410
+ }
411
+ }
412
+
413
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=core&sectionid=1&section=test&termid=2", :body => badges_body.to_json)
414
+
415
+ section = Osm::Section.new(:id => 1, :type => :test)
416
+ term = Osm::Term.new(:id => 2)
417
+ stock = Osm::Api.new('1', '2').get_badge_stock_levels(section, term)
418
+ stock.should == {'badge_1' => 1, 'badge_2' => 2}
419
+ end
420
+
421
+ it "Fetch badge stock levels for a section (data from API missing)" do
422
+ badges_body = {}
423
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/challenges.php?action=getInitialBadges&type=core&sectionid=1&section=test&termid=2", :body => badges_body.to_json)
424
+
425
+ section = Osm::Section.new(:id => 1, :type => :test)
426
+ term = Osm::Term.new(:id => 2)
427
+ stock = Osm::Api.new('1', '2').get_badge_stock_levels(section, term)
428
+ stock.should == {}
429
+ end
430
+
404
431
  it "Fetch the register structure for a section" do
405
432
  data = [
406
433
  {"rows" => [{"name"=>"First name","field"=>"firstname","width"=>"100px"},{"name"=>"Last name","field"=>"lastname","width"=>"100px"},{"name"=>"Total","field"=>"total","width"=>"60px"}],"noscroll"=>true},
@@ -557,7 +584,107 @@ describe "API" do
557
584
  api = Osm::Api.new('user', 'secret')
558
585
  evening = Osm::Evening.new
559
586
  expect{ api.update_evening(evening) }.to raise_error(Osm::ArgumentIsInvalid)
560
- end end
587
+ end
588
+
589
+
590
+ it "Create a term (succeded)" do
591
+ url = 'https://www.onlinescoutmanager.co.uk/users.php?action=addTerm&sectionid=1'
592
+ post_data = {
593
+ 'apiid' => @api_config[:api_id],
594
+ 'token' => @api_config[:api_token],
595
+ 'userid' => 'user',
596
+ 'secret' => 'secret',
597
+ 'term' => 'A Term',
598
+ 'start' => '2010-01-01',
599
+ 'end' => '2010-12-31',
600
+ 'termid' => '0'
601
+ }
602
+
603
+ api = Osm::Api.new('user', 'secret')
604
+ api.stub(:get_terms) { [] }
605
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"terms":{}}'}) }
606
+
607
+ api.create_term({
608
+ :section => 1,
609
+ :name => 'A Term',
610
+ :start => Date.new(2010, 01, 01),
611
+ :finish => Date.new(2010, 12, 31),
612
+ }).should be_true
613
+ end
614
+
615
+ it "Create a term (failed)" do
616
+ url = 'https://www.onlinescoutmanager.co.uk/users.php?action=addTerm&sectionid=1'
617
+ post_data = {
618
+ 'apiid' => @api_config[:api_id],
619
+ 'token' => @api_config[:api_token],
620
+ 'userid' => 'user',
621
+ 'secret' => 'secret',
622
+ 'term' => 'A Term',
623
+ 'start' => '2010-01-01',
624
+ 'end' => '2010-12-31',
625
+ 'termid' => '0'
626
+ }
627
+
628
+ api = Osm::Api.new('user', 'secret')
629
+ api.stub(:get_terms) { [] }
630
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
631
+
632
+ api.create_term({
633
+ :section => 1,
634
+ :name => 'A Term',
635
+ :start => Date.new(2010, 01, 01),
636
+ :finish => Date.new(2010, 12, 31),
637
+ }).should be_false
638
+ end
639
+
640
+
641
+ it "Update a term (succeded)" do
642
+ url = 'https://www.onlinescoutmanager.co.uk/users.php?action=addTerm&sectionid=1'
643
+ post_data = {
644
+ 'apiid' => @api_config[:api_id],
645
+ 'token' => @api_config[:api_token],
646
+ 'userid' => 'user',
647
+ 'secret' => 'secret',
648
+ 'term' => 'A Term',
649
+ 'start' => '2010-01-01',
650
+ 'end' => '2010-12-31',
651
+ 'termid' => 2
652
+ }
653
+ api = Osm::Api.new('user', 'secret')
654
+ api.stub(:get_terms) { [] }
655
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"terms":{}}'}) }
656
+
657
+ term = Osm::Term.new(:id=>2, :section_id=>1, :name=>'A Term', :start=>Date.new(2010, 01, 01), :finish=>Date.new(2010, 12, 31))
658
+ api.update_term(term).should be_true
659
+ end
660
+
661
+ it "Update a term (failed)" do
662
+ url = 'https://www.onlinescoutmanager.co.uk/users.php?action=addTerm&sectionid=1'
663
+ post_data = {
664
+ 'apiid' => @api_config[:api_id],
665
+ 'token' => @api_config[:api_token],
666
+ 'userid' => 'user',
667
+ 'secret' => 'secret',
668
+ 'term' => 'A Term',
669
+ 'start' => '2010-01-01',
670
+ 'end' => '2010-12-31',
671
+ 'termid' => 2
672
+ }
673
+ api = Osm::Api.new('user', 'secret')
674
+ api.stub(:get_terms) { [] }
675
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{}'}) }
676
+
677
+ term = Osm::Term.new(:id=>2, :section_id=>1, :name=>'A Term', :start=>Date.new(2010, 01, 01), :finish=>Date.new(2010, 12, 31))
678
+ api.update_term(term).should be_false
679
+ end
680
+
681
+ it "Update a term (invalid term)" do
682
+ api = Osm::Api.new('user', 'secret')
683
+ term = Osm::Term.new
684
+ expect{ api.update_term(term) }.to raise_error(Osm::ArgumentIsInvalid)
685
+ end
686
+
687
+ end
561
688
 
562
689
 
563
690
  describe "Options Hash" do
@@ -11,21 +11,21 @@ describe "DueBadge" do
11
11
  'scoutid' => '1',
12
12
  'firstname' => 'John',
13
13
  'lastname' => 'Doe',
14
- 'completed' => '',
14
+ 'level' => '',
15
15
  'extra' => '',
16
16
  }
17
17
  ],
18
- 'cubs_core_participation' => [{
18
+ 'staged_staged_participation' => [{
19
19
  'sid' => '2',
20
20
  'firstname' => 'Jane',
21
21
  'lastname' => 'Doe',
22
- 'completed' => '3',
23
- 'extra' => 'Lvl 3'
22
+ 'level' => '2',
23
+ 'extra' => 'Lvl 2'
24
24
  }, {
25
25
  'sid' => '1',
26
26
  'firstname' => 'John',
27
27
  'lastname' => 'Doe',
28
- 'completed' => '2',
28
+ 'level' => '2',
29
29
  'extra' => 'Lvl 2'
30
30
  }
31
31
  ]
@@ -38,10 +38,10 @@ describe "DueBadge" do
38
38
  'type' => 'activity',
39
39
  'badge' => 'badge_name'
40
40
  },
41
- 'cubs_core_participation' => {
41
+ 'staged_staged_participation' => {
42
42
  'name' => 'Participation',
43
- 'section' => 'cubs',
44
- 'type' => 'core',
43
+ 'section' => 'staged',
44
+ 'type' => 'staged',
45
45
  'badge' => 'participation'
46
46
  }
47
47
  }
@@ -49,9 +49,9 @@ describe "DueBadge" do
49
49
  db = Osm::DueBadges.from_api(data)
50
50
 
51
51
  db.empty?.should == false
52
- db.descriptions.should == {:badge_name=>{:name=>"Badge Name", :section=>:cubs, :type=>:activity, :badge=>"badge_name"}, :cubs_core_participation=>{:name=>"Participation", :section=>:cubs, :type=>:core, :badge=>"participation"}}
53
- db.by_member.should == {"John Doe"=>[{:badge=>:badge_name, :extra_information=>""}, {:badge=>:cubs_core_participation, :extra_information=>"Lvl 2"}], "Jane Doe"=>[{:badge=>:cubs_core_participation, :extra_information=>"Lvl 3"}]}
54
- db.totals.should == {:badge_name=>{""=>1}, :cubs_core_participation=>{"Lvl 3"=>1, "Lvl 2"=>1}}
52
+ db.descriptions.should == {'badge_name_1'=>'Badge Name', 'staged_staged_participation_2'=>'Participation (Level 2)'}
53
+ db.by_member.should == {'John Doe'=>['badge_name_1', 'staged_staged_participation_2'], 'Jane Doe'=>['staged_staged_participation_2']}
54
+ db.totals.should == {'staged_staged_participation_2'=>2, 'badge_name_1'=>1}
55
55
  db.valid?.should be_true
56
56
  end
57
57
 
@@ -33,6 +33,17 @@ describe "Term" do
33
33
  end
34
34
 
35
35
 
36
+ it "Creates the data for saving through the API" do
37
+ term = Osm::Term.new(@attributes)
38
+ term.to_api.should == {
39
+ 'term' => @attributes[:name],
40
+ 'start' => @attributes[:start].strftime(Osm::OSM_DATE_FORMAT),
41
+ 'end' => @attributes[:finish].strftime(Osm::OSM_DATE_FORMAT),
42
+ 'termid' => @attributes[:id]
43
+ }
44
+ end
45
+
46
+
36
47
  it "Compares two matching terms" do
37
48
  term1 = Osm::Term.new(@attributes)
38
49
  term2 = Osm::Term.new(@attributes)
data/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Osm
2
- VERSION = "0.0.21"
2
+ VERSION = "0.0.22"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: osm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.21
4
+ version: 0.0.22
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-24 00:00:00.000000000Z
12
+ date: 2012-09-25 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &84462520 !ruby/object:Gem::Requirement
16
+ requirement: &83982580 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *84462520
24
+ version_requirements: *83982580
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: httparty
27
- requirement: &84461050 !ruby/object:Gem::Requirement
27
+ requirement: &83975470 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0.9'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *84461050
35
+ version_requirements: *83975470
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: active_attr
38
- requirement: &84459560 !ruby/object:Gem::Requirement
38
+ requirement: &83974580 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0.6'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *84459560
46
+ version_requirements: *83974580
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: activemodel
49
- requirement: &84432320 !ruby/object:Gem::Requirement
49
+ requirement: &83973660 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '3.2'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *84432320
57
+ version_requirements: *83973660
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &84430640 !ruby/object:Gem::Requirement
60
+ requirement: &83972980 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0.9'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *84430640
68
+ version_requirements: *83972980
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
- requirement: &84429660 !ruby/object:Gem::Requirement
71
+ requirement: &83972430 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '2.11'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *84429660
79
+ version_requirements: *83972430
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: fakeweb
82
- requirement: &84429240 !ruby/object:Gem::Requirement
82
+ requirement: &83968460 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: '1.3'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *84429240
90
+ version_requirements: *83968460
91
91
  description: Use the Online Scout Manager API (https://www.onlinescoutmanager.co.uk)
92
92
  to retrieve and save data.
93
93
  email: