osm 0.0.26 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/CHANGELOG.md +57 -0
  2. data/README.md +13 -7
  3. data/lib/osm.rb +47 -22
  4. data/lib/osm/activity.rb +52 -57
  5. data/lib/osm/api.rb +115 -1031
  6. data/lib/osm/api_access.rb +73 -36
  7. data/lib/osm/due_badges.rb +27 -12
  8. data/lib/osm/evening.rb +118 -55
  9. data/lib/osm/event.rb +275 -17
  10. data/lib/osm/flexi_record.rb +131 -0
  11. data/lib/osm/grouping.rb +37 -15
  12. data/lib/osm/member.rb +100 -41
  13. data/lib/osm/model.rb +95 -0
  14. data/lib/osm/register.rb +177 -0
  15. data/lib/osm/section.rb +163 -71
  16. data/lib/osm/term.rb +135 -21
  17. data/spec/osm/activity_spec.rb +7 -4
  18. data/spec/osm/api_access_spec.rb +44 -36
  19. data/spec/osm/api_spec.rb +32 -1147
  20. data/spec/osm/due_badges_spec.rb +8 -1
  21. data/spec/osm/evening_spec.rb +119 -54
  22. data/spec/osm/event_spec.rb +363 -13
  23. data/spec/osm/flexi_record_spec.rb +128 -0
  24. data/spec/osm/grouping_spec.rb +9 -5
  25. data/spec/osm/member_spec.rb +111 -36
  26. data/spec/osm/model_spec.rb +140 -0
  27. data/spec/osm/osm_spec.rb +7 -31
  28. data/spec/osm/register_spec.rb +103 -0
  29. data/spec/osm/section_spec.rb +208 -92
  30. data/spec/osm/term_spec.rb +164 -28
  31. data/spec/spec_helper.rb +22 -0
  32. data/version.rb +1 -1
  33. metadata +22 -29
  34. data/lib/osm/event_attendance.rb +0 -55
  35. data/lib/osm/flexi_record_data.rb +0 -51
  36. data/lib/osm/flexi_record_field.rb +0 -42
  37. data/lib/osm/register_data.rb +0 -64
  38. data/lib/osm/register_field.rb +0 -42
  39. data/lib/osm/role.rb +0 -133
  40. data/spec/osm/api_strangeness_spec.rb +0 -83
  41. data/spec/osm/event_attendance_spec.rb +0 -34
  42. data/spec/osm/flexi_record_data_spec.rb +0 -40
  43. data/spec/osm/flexi_record_field_spec.rb +0 -23
  44. data/spec/osm/register_data_spec.rb +0 -35
  45. data/spec/osm/register_field_spec.rb +0 -24
  46. data/spec/osm/role_spec.rb +0 -118
@@ -0,0 +1,128 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'date'
4
+
5
+
6
+ describe "Flexi Record" do
7
+
8
+ it "Create FlexiRecord::Field" do
9
+ field = Osm::FlexiRecord::Field.new(
10
+ :id => "f_1",
11
+ :name => "Field Name",
12
+ :editable => true
13
+ )
14
+
15
+ field.id.should == 'f_1'
16
+ field.name.should == 'Field Name'
17
+ field.editable.should be_true
18
+ field.valid?.should be_true
19
+ end
20
+
21
+ it "Create FlexiRecord::Data" do
22
+ rd = Osm::FlexiRecord::Data.new(
23
+ :member_id => 1,
24
+ :grouping_id => 2,
25
+ :fields => {
26
+ 'firstname' => 'First',
27
+ 'lastname' => 'Last',
28
+ 'dob' => Date.new(1899, 11, 30),
29
+ 'total' => 3,
30
+ 'completed' => nil,
31
+ 'age' => nil,
32
+ 'f_1' => 'a',
33
+ 'f_2' => 'b',
34
+ }
35
+ )
36
+
37
+ rd.member_id.should == 1
38
+ rd.grouping_id.should == 2
39
+ rd.fields.should == {
40
+ 'firstname' => 'First',
41
+ 'lastname' => 'Last',
42
+ 'dob' => Date.new(1899, 11, 30),
43
+ 'total' => 3,
44
+ 'completed' => nil,
45
+ 'age' => nil,
46
+ 'f_1' => 'a',
47
+ 'f_2' => 'b',
48
+ }
49
+ rd.valid?.should be_true
50
+ end
51
+
52
+
53
+ describe "Using the API" do
54
+
55
+ it "Fetch Fields" do
56
+ data = {
57
+ "extraid" => "2",
58
+ "sectionid" => "1",
59
+ "name" => "A Flexi Record",
60
+ "config" => "[{\"id\":\"f_1\",\"name\":\"Field 1\",\"width\":\"150\"},{\"id\":\"f_2\",\"name\":\"Field 2\",\"width\":\"150\"}]",
61
+ "total" => "none",
62
+ "extrafields" => "[]",
63
+ "structure" => [
64
+ {
65
+ "rows" => [
66
+ {"name" => "First name","field" => "firstname","width" => "150px"},
67
+ {"name" => "Last name","field" => "lastname","width" => "150px"},
68
+ ],
69
+ "noscroll" => true
70
+ },
71
+ {"rows" => [
72
+ {"name" => "Field 1","field" => "f_1","width" => "150px","editable" => true},
73
+ {"name" => "Filed 2","field" => "f_2","width" => "150px","editable" => true},
74
+ ]}
75
+ ]
76
+ }
77
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/extras.php?action=getExtra&sectionid=1&extraid=2", :body => data.to_json)
78
+
79
+ fields = Osm::FlexiRecord.get_fields(@api, 1, 2)
80
+ fields.is_a?(Array).should be_true
81
+ fields[0].valid?.should be_true
82
+ fields[0].id.should == 'firstname'
83
+ fields[1].id.should == 'lastname'
84
+ fields[2].id.should == 'f_1'
85
+ fields[3].id.should == 'f_2'
86
+ end
87
+
88
+ it "Fetch Data" do
89
+ data = {
90
+ 'identifier' => 'scoutid',
91
+ 'label' => "name",
92
+ 'items' => [{
93
+ "scoutid" => "1",
94
+ "firstname" => "First",
95
+ "lastname" => "Last",
96
+ "dob" => "",
97
+ "patrolid" => "2",
98
+ "total" => "",
99
+ "completed" => "",
100
+ "f_1" => "A",
101
+ "f_2" => "B",
102
+ "age" => "",
103
+ "patrol" => "Green"
104
+ }]
105
+ }
106
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/extras.php?action=getExtraRecords&sectionid=1&extraid=2&termid=3&section=cubs", :body => data.to_json)
107
+
108
+ records = Osm::FlexiRecord.get_data(@api, Osm::Section.new(:id => 1, :type => :cubs), 2, 3)
109
+ records.is_a?(Array).should be_true
110
+ record = records[0]
111
+ record.member_id.should == 1
112
+ record.grouping_id.should == 2
113
+ record.fields.should == {
114
+ 'firstname' => 'First',
115
+ 'lastname' => 'Last',
116
+ 'dob' => nil,
117
+ 'total' => nil,
118
+ 'completed' => nil,
119
+ 'age' => nil,
120
+ 'f_1' => 'A',
121
+ 'f_2' => 'B',
122
+ }
123
+ record.valid?.should be_true
124
+ end
125
+
126
+ end
127
+
128
+ end
@@ -4,18 +4,22 @@ require 'spec_helper'
4
4
  describe "Grouping" do
5
5
 
6
6
  it "Create" do
7
- data = {
7
+ body = {'patrols' => [{
8
8
  'patrolid' => 1,
9
9
  'name' => 'Patrol Name',
10
10
  'active' => 1,
11
- 'points' => '2',
12
- }
13
- patrol = Osm::Grouping.from_api(data)
11
+ 'points' => '3',
12
+ }]}
13
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=getPatrols&sectionid=2", :body => body.to_json)
14
14
 
15
+ patrols = Osm::Grouping.get_for_section(@api, 2)
16
+ patrols.size.should == 1
17
+ patrol = patrols[0]
15
18
  patrol.id.should == 1
19
+ patrol.section_id.should == 2
16
20
  patrol.name.should == 'Patrol Name'
17
21
  patrol.active.should == true
18
- patrol.points.should == 2
22
+ patrol.points.should == 3
19
23
  patrol.valid?.should be_true
20
24
  end
21
25
 
@@ -3,43 +3,49 @@ require 'spec_helper'
3
3
 
4
4
  describe "Member" do
5
5
 
6
- it "Create from API data" do
7
- data = {
8
- 'scoutid' => 1,
9
- 'sectionidO' => 2,
10
- 'type' => '',
11
- 'firstname' => 'First',
12
- 'lastname' => 'Last',
13
- 'email1' => 'email1@example.com',
14
- 'email2' => 'email2@example.com',
15
- 'email3' => 'email3@example.com',
16
- 'email4' => 'email4@example.com',
17
- 'phone1' => '11111 111111',
18
- 'phone2' => '222222',
19
- 'phone3' => '+33 3333 333333',
20
- 'phone4' => '4444 444 444',
21
- 'address' => '1 Some Road',
22
- 'address2' => '',
23
- 'dob' => '2000-01-02',
24
- 'started' => '2006-01-02',
25
- 'joining_in_yrs' => '2',
26
- 'parents' => 'John and Jane Doe',
27
- 'notes' => 'None',
28
- 'medical' => 'Nothing',
29
- 'religion' => 'Unknown',
30
- 'school'=> 'Some School',
31
- 'ethnicity' => 'Yes',
32
- 'subs' => 'Upto end of 2007',
33
- 'patrolid' => '3',
34
- 'patrolleader' => 0,
35
- 'joined' => '2006-01-07',
36
- 'age' => '06/07',
37
- 'yrs' => 1,
38
- 'patrol' => 'Blue',
39
- 'patrolidO' => '4',
40
- 'patrolleaderO' => 0,
6
+ it "Create" do
7
+ attributes = {
8
+ :id => 1,
9
+ :section_id => 2,
10
+ :type => '',
11
+ :first_name => 'First',
12
+ :last_name => 'Last',
13
+ :email1 => 'email1@example.com',
14
+ :email2 => 'email2@example.com',
15
+ :email3 => 'email3@example.com',
16
+ :email4 => 'email4@example.com',
17
+ :phone1 => '11111 111111',
18
+ :phone2 => '222222',
19
+ :phone3 => '+33 3333 333333',
20
+ :phone4 => '4444 444 444',
21
+ :address => '1 Some Road',
22
+ :address2 => '',
23
+ :date_of_birth => '2000-01-02',
24
+ :started => '2006-01-02',
25
+ :joining_in_years => '2',
26
+ :parents => 'John and Jane Doe',
27
+ :notes => 'None',
28
+ :medical => 'Nothing',
29
+ :religion => 'Unknown',
30
+ :school=> 'Some School',
31
+ :ethnicity => 'Yes',
32
+ :subs => 'Upto end of 2007',
33
+ :custom1 => 'Custom Field 1',
34
+ :custom2 => 'Custom Field 2',
35
+ :custom3 => 'Custom Field 3',
36
+ :custom4 => 'Custom Field 4',
37
+ :custom5 => 'Custom Field 5',
38
+ :custom6 => 'Custom Field 6',
39
+ :custom7 => 'Custom Field 7',
40
+ :custom8 => 'Custom Field 8',
41
+ :custom9 => 'Custom Field 9',
42
+ :grouping_id => '3',
43
+ :grouping_leader => 0,
44
+ :joined => '2006-01-07',
45
+ :age => '06/07',
46
+ :joined_years => 1,
41
47
  }
42
- member = Osm::Member.from_api(data, 2)
48
+ member = Osm::Member.new(attributes)
43
49
 
44
50
  member.id.should == 1
45
51
  member.section_id.should == 2
@@ -66,6 +72,15 @@ describe "Member" do
66
72
  member.school.should == 'Some School'
67
73
  member.ethnicity.should == 'Yes'
68
74
  member.subs.should == 'Upto end of 2007'
75
+ member.custom1.should == 'Custom Field 1'
76
+ member.custom2.should == 'Custom Field 2'
77
+ member.custom3.should == 'Custom Field 3'
78
+ member.custom4.should == 'Custom Field 4'
79
+ member.custom5.should == 'Custom Field 5'
80
+ member.custom6.should == 'Custom Field 6'
81
+ member.custom7.should == 'Custom Field 7'
82
+ member.custom8.should == 'Custom Field 8'
83
+ member.custom9.should == 'Custom Field 9'
69
84
  member.grouping_id.should == 3
70
85
  member.grouping_leader.should == 0
71
86
  member.joined.should == Date.new(2006, 1, 7)
@@ -97,4 +112,64 @@ describe "Member" do
97
112
  member.age_months.should == 7
98
113
  end
99
114
 
115
+
116
+ describe "Using the API" do
117
+
118
+ it "Create from API data" do
119
+ body = {
120
+ 'identifier' => 'scoutid',
121
+ 'items' => [{
122
+ 'scoutid' => 1,
123
+ 'sectionidO' => 2,
124
+ 'type' => '',
125
+ 'firstname' => 'First',
126
+ 'lastname' => 'Last',
127
+ 'email1' => 'email1@example.com',
128
+ 'email2' => 'email2@example.com',
129
+ 'email3' => 'email3@example.com',
130
+ 'email4' => 'email4@example.com',
131
+ 'phone1' => '11111 111111',
132
+ 'phone2' => '222222',
133
+ 'phone3' => '+33 3333 333333',
134
+ 'phone4' => '4444 444 444',
135
+ 'address' => '1 Some Road',
136
+ 'address2' => '',
137
+ 'dob' => '2000-01-02',
138
+ 'started' => '2006-01-02',
139
+ 'joining_in_yrs' => '2',
140
+ 'parents' => 'John and Jane Doe',
141
+ 'notes' => 'None',
142
+ 'medical' => 'Nothing',
143
+ 'religion' => 'Unknown',
144
+ 'school'=> 'Some School',
145
+ 'ethnicity' => 'Yes',
146
+ 'subs' => 'Upto end of 2007',
147
+ 'custom1' => 'Custom Field 1',
148
+ 'custom2' => 'Custom Field 2',
149
+ 'custom3' => 'Custom Field 3',
150
+ 'custom4' => 'Custom Field 4',
151
+ 'custom5' => 'Custom Field 5',
152
+ 'custom6' => 'Custom Field 6',
153
+ 'custom7' => 'Custom Field 7',
154
+ 'custom8' => 'Custom Field 8',
155
+ 'custom9' => 'Custom Field 9',
156
+ 'patrolid' => '3',
157
+ 'patrolleader' => 0,
158
+ 'joined' => '2006-01-07',
159
+ 'age' => '06/07',
160
+ 'yrs' => 1,
161
+ 'patrol' => 'Blue',
162
+ 'patrolidO' => '4',
163
+ 'patrolleaderO' => 0,
164
+ }]
165
+ }
166
+
167
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/users.php?action=getUserDetails&sectionid=1&termid=2", :body => body.to_json)
168
+ members = Osm::Member.get_for_section(@api, 1, 2)
169
+ members.size.should == 1
170
+ members[0].id.should == 1
171
+ end
172
+
173
+ end
174
+
100
175
  end
@@ -0,0 +1,140 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+
5
+ describe "Model" do
6
+
7
+ class ModelTester < Osm::Model
8
+ def self.test_get_config
9
+ {
10
+ :cache => @@cache,
11
+ :prepend_to_key => @@cache_prepend,
12
+ :ttl => @@cache_ttl,
13
+ }
14
+ end
15
+
16
+ def self.cache(method, *options)
17
+ self.send("cache_#{method}", *options)
18
+ end
19
+ end
20
+
21
+
22
+ it "Create" do
23
+ model = Osm::Model.new
24
+ model.should_not be_nil
25
+ end
26
+
27
+
28
+ it "Configure" do
29
+ options = {
30
+ :cache => OsmTest::Cache,
31
+ :ttl => 100,
32
+ :prepend_to_key => 'Hi',
33
+ }
34
+
35
+ Osm::Model.configure(options)
36
+ config = ModelTester.test_get_config
37
+ config.should == options
38
+ end
39
+
40
+ it "Configure (allows empty Hash)" do
41
+ Osm::Model.configure({})
42
+ config = ModelTester.test_get_config
43
+ config[:cache].should be_nil
44
+ config[:ttl].should == 1800
45
+ config[:prepend_to_key].should == 'OSMAPI'
46
+ end
47
+
48
+ it "Configure (bad arguments)" do
49
+ expect{ Osm::Model.configure(@CONFIGURATION[:cache].merge(:prepend_to_key => :invalid)) }.to raise_error(ArgumentError, ':prepend_to_key must be a String')
50
+
51
+ expect{ Osm::Model.configure(@CONFIGURATION[:cache].merge(:ttl => :invalid)) }.to raise_error(ArgumentError, ':ttl must be a FixNum greater than 0')
52
+ expect{ Osm::Model.configure(@CONFIGURATION[:cache].merge(:ttl => 0)) }.to raise_error(ArgumentError, ':ttl must be a FixNum greater than 0')
53
+
54
+ expect{ Osm::Model.configure(@CONFIGURATION[:cache].merge(:cache => String)) }.to raise_error(ArgumentError, ':cache must have a exist? method')
55
+ end
56
+
57
+
58
+ describe "Caching" do
59
+
60
+ it "Checks for existance" do
61
+ OsmTest::Cache.should_receive('exist?').with('OSMAPI-osm-key') { true }
62
+ ModelTester.cache('exist?', @api, 'key').should be_true
63
+ end
64
+
65
+ it "Writes" do
66
+ OsmTest::Cache.should_receive('write').with('OSMAPI-osm-key', 'data', {:expires_in=>1800}) { true }
67
+ ModelTester.cache('write', @api, 'key', 'data').should be_true
68
+ end
69
+
70
+ it "Reads" do
71
+ OsmTest::Cache.should_receive('read').with('OSMAPI-osm-key') { 'data' }
72
+ ModelTester.cache('read', @api, 'key').should == 'data'
73
+ end
74
+
75
+ it "Deletes" do
76
+ OsmTest::Cache.should_receive('delete').with('OSMAPI-osm-key') { true }
77
+ ModelTester.cache('delete', @api, 'key').should be_true
78
+ end
79
+
80
+ it "Behaves when cache is nil (no caching)" do
81
+ Osm::Model.configure({:cache => nil})
82
+ ModelTester.cache('exist?', @api, 'key').should be_false
83
+ ModelTester.cache('write', @api, 'key', 'data').should be_false
84
+ ModelTester.cache('read', @api, 'key').should be_nil
85
+ ModelTester.cache('delete', @api, 'key').should be_true
86
+ end
87
+
88
+ it "Builds a key from an array" do
89
+ ModelTester.cache('key', @api, ['a', 'b']).should == 'OSMAPI-osm-a-b'
90
+ end
91
+
92
+ end
93
+
94
+
95
+ describe "Get User Permissions" do
96
+
97
+ it "From cache" do
98
+ permissions = {1 => {:a => [:read, :write]}, 2 => {:a => [:read]}}
99
+ OsmTest::Cache.should_receive('exist?').with('OSMAPI-osm-permissions-user_id') { true }
100
+ OsmTest::Cache.should_receive('read').with('OSMAPI-osm-permissions-user_id') { permissions }
101
+ ModelTester.get_user_permissions(@api).should == permissions
102
+ end
103
+
104
+ it "From API" do
105
+ permissions = {1 => {:a => [:read, :write]}, 2 => {:a => [:read]}}
106
+ OsmTest::Cache.should_receive('exist?').with('OSMAPI-osm-permissions-user_id') { false }
107
+ Osm::Section.should_receive('fetch_user_permissions').with(@api) { permissions }
108
+ ModelTester.get_user_permissions(@api).should == permissions
109
+ end
110
+
111
+ it "Single section" do
112
+ permissions = {1 => {:a => [:read, :write]}, 2 => {:a => [:read]}}
113
+ OsmTest::Cache.should_receive('exist?').with('OSMAPI-osm-permissions-user_id').twice { true }
114
+ OsmTest::Cache.should_receive('read').with('OSMAPI-osm-permissions-user_id').twice { permissions }
115
+ ModelTester.get_user_permissions(@api, 1).should == permissions[1]
116
+ ModelTester.get_user_permissions(@api, 2).should == permissions[2]
117
+ end
118
+
119
+ end
120
+
121
+
122
+ describe "Set User Permissions" do
123
+
124
+ it "All Sections" do
125
+ permissions = {1 => {:a => [:read, :write]}, 2 => {:a => [:read]}}
126
+ OsmTest::Cache.should_receive('write').with('OSMAPI-osm-permissions-user_id', permissions, {:expires_in=>1800}) { true }
127
+ ModelTester.set_user_permissions(@api, permissions)
128
+ end
129
+
130
+ it "Single section" do
131
+ permissions = {1 => {:a => [:read, :write]}, 2 => {:a => [:read]}}
132
+ OsmTest::Cache.should_receive('exist?').with('OSMAPI-osm-permissions-user_id') { true }
133
+ OsmTest::Cache.should_receive('read').with('OSMAPI-osm-permissions-user_id') { permissions }
134
+ OsmTest::Cache.should_receive('write').with('OSMAPI-osm-permissions-user_id', permissions.merge(3 => {:a => [:read]}), {:expires_in=>1800}) { true }
135
+ ModelTester.set_user_permissions(@api, 3, {:a => [:read]})
136
+ end
137
+
138
+ end
139
+
140
+ end