osm 0.1.17 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -36,12 +36,14 @@ module Osm
36
36
  cache_key = ['terms', api.user_id]
37
37
 
38
38
  if !options[:no_cache] && cache_exist?(api, cache_key)
39
- return cache_read(api, cache_key)
39
+ ids = cache_read(api, cache_key)
40
+ return get_from_ids(api, ids, 'term', options, :get_all)
40
41
  end
41
42
 
42
43
  data = api.perform_query('api.php?action=getTerms')
43
44
 
44
- result = Array.new
45
+ terms = Array.new
46
+ ids = Array.new
45
47
  data.each_key do |key|
46
48
  data[key].each do |term_data|
47
49
  term = Osm::Term.new(
@@ -51,37 +53,39 @@ module Osm
51
53
  :start => Osm::parse_date(term_data['startdate']),
52
54
  :finish => Osm::parse_date(term_data['enddate']),
53
55
  )
54
- result.push term
56
+ terms.push term
57
+ ids.push term.id
55
58
  cache_write(api, ['term', term.id], term)
56
59
  end
57
60
  end
58
61
 
59
- cache_write(api, cache_key, result)
60
- return result
62
+ cache_write(api, cache_key, ids)
63
+ return terms
61
64
  end
62
65
 
63
66
  # Get the terms that the OSM user can access for a given section
64
67
  # @param [Osm::Api] api The api to use to make the request
65
- # @param [Fixnum] section the section (or its ID) of the section to get terms for
68
+ # @param [Fixnum] section The section (or its ID) of the section to get terms for
66
69
  # @!macro options_get
67
70
  # @return [Array<Osm::Term>, nil] An array of terms or nil if the user can not access that section
68
71
  def self.get_for_section(api, section, options={})
72
+ require_access_to_section(api, section, options)
69
73
  section_id = section.to_i
70
- return nil unless get_user_permissions(api).keys.include?(section_id)
71
74
  return get_all(api, options).select{ |term| term.section_id == section_id }
72
75
  end
73
76
 
74
77
  # Get a term
75
- # @param [Osm::Api] The api to use to make the request
76
- # @param [Fixnum] term_id the id of the required term
78
+ # @param [Osm::Api] api The api to use to make the request
79
+ # @param [Fixnum] term_id The id of the required term
77
80
  # @!macro options_get
78
81
  # @return nil if an error occured or the user does not have access to that term
79
- # @return [Osm::Section]
82
+ # @return [Osm::Term]
80
83
  def self.get(api, term_id, options={})
81
84
  cache_key = ['term', term_id]
82
85
 
83
86
  if !options[:no_cache] && cache_exist?(api, cache_key)
84
- return cache_read(api, cache_key)
87
+ term = cache_read(api, cache_key)
88
+ return term if can_access_section?(api, term.section_id, options)
85
89
  end
86
90
 
87
91
  terms = get_all(api, options)
@@ -89,7 +93,7 @@ module Osm
89
93
 
90
94
  terms.each do |term|
91
95
  if term.id == term_id
92
- return (get_user_permissions(api).keys.include?(term.section_id) ? term : nil)
96
+ return (can_access_section?(api, term.section_id, options) ? term : nil)
93
97
  end
94
98
  end
95
99
  return nil
@@ -97,9 +101,10 @@ module Osm
97
101
 
98
102
  # Get the current term for a given section
99
103
  # @param [Osm::Api] api The api to use to make the request
100
- # @param [Osm::Section, Fixnum] section The section (or its ID) to get terms for
101
- # @!macro options_get
104
+ # @param [Osm::Section, Fixnum, #to_i] section The section (or its ID) to get terms for
105
+ # @!macro options_get
102
106
  # @return [Osm::Term, nil] The current term or nil if the user can not access that section
107
+ # @raise [Osm::Error] If the term doesn't have a Term which is current
103
108
  def self.get_current_term_for_section(api, section, options={})
104
109
  section_id = section.to_i
105
110
  terms = get_for_section(api, section_id, options)
@@ -109,7 +114,7 @@ module Osm
109
114
  return term if term.current?
110
115
  end
111
116
 
112
- raise Error, 'There is no current term for the section.'
117
+ raise Osm::Error, 'There is no current term for the section.'
113
118
  end
114
119
 
115
120
  # Create a term in OSM
@@ -125,6 +130,7 @@ module Osm
125
130
  raise ArgumentError, ":name can't be nil" if options[:name].nil?
126
131
  raise ArgumentError, ":start can't be nil" if options[:start].nil?
127
132
  raise ArgumentError, ":finish can't be nil" if options[:finish].nil?
133
+ require_access_to_section(api, options[:section])
128
134
 
129
135
  api_data = {
130
136
  'term' => options[:name],
@@ -146,10 +152,12 @@ module Osm
146
152
 
147
153
 
148
154
  # Update a term in OSM
149
- # @param [Osm::Api] The api to use to make the request
155
+ # @param [Osm::Api] api The api to use to make the request
150
156
  # @return [Boolean] if the operation suceeded or not
157
+ # @raise [Osm::ObjectIsInvalid] If the Term is invalid
151
158
  def update(api)
152
- raise ObjectIsInvalid, 'term is invalid' unless valid?
159
+ raise Osm::ObjectIsInvalid, 'term is invalid' unless valid?
160
+ require_access_to_section(api, section_id)
153
161
 
154
162
  data = api.perform_query("users.php?action=addTerm&sectionid=#{section_id}", {
155
163
  'term' => name,
@@ -158,18 +166,21 @@ module Osm
158
166
  'termid' => id
159
167
  })
160
168
 
161
- # The cached terms for the section will be out of date - remove them
162
- cache_delete(api, ['term', id])
163
- cache_delete(api, ['terms', api.user_id])
164
-
165
- return data.is_a?(Hash) && data['terms'].is_a?(Hash)
169
+ if data.is_a?(Hash) && data['terms'].is_a?(Hash)
170
+ reset_changed_attributes
171
+ # The cached term will be out of date - remove it
172
+ cache_delete(api, ['term', id])
173
+ return true
174
+ else
175
+ return false
176
+ end
166
177
  end
167
178
 
168
179
 
169
180
 
170
181
  # @!method initialize
171
182
  # Initialize a new Term
172
- # @param [Hash] attributes the hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
183
+ # @param [Hash] attributes The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
173
184
 
174
185
 
175
186
  # Determine if the term is completly before the passed date
@@ -205,7 +216,7 @@ module Osm
205
216
  end
206
217
 
207
218
  # Determine if the provided date is within the term
208
- # @param [Date] date the date to test
219
+ # @param [Date] date The date to test
209
220
  # @return [Boolean] if the term started before the date and finishes after the date
210
221
  def contains_date?(date)
211
222
  return (start <= date) && (finish >= date)
@@ -26,5 +26,7 @@ Gem::Specification.new do |s|
26
26
  s.add_development_dependency 'rake', '~> 10.0'
27
27
  s.add_development_dependency 'rspec', '~> 2.11'
28
28
  s.add_development_dependency 'fakeweb', '~> 1.3'
29
+ s.add_development_dependency 'guard-rspec', '~> 2.4'
30
+ s.add_development_dependency 'rb-inotify', '~> 0.8.8'
29
31
 
30
32
  end
@@ -1,179 +1,196 @@
1
1
  # encoding: utf-8
2
2
  require 'spec_helper'
3
3
 
4
- describe "Using The API" do
5
-
6
- it "Get One" do
7
- body = {
8
- 'details' => {
9
- 'activityid' => '1',
10
- 'version' => '0',
11
- 'groupid' => '2',
12
- 'userid' => '3',
13
- 'title' => 'Activity Name',
14
- 'description' => 'Description',
15
- 'resources' => 'Resources',
16
- 'instructions' => 'Instructions',
17
- 'runningtime' => '15',
18
- 'location' => 'indoors',
19
- 'shared' => '0',
20
- 'rating' => '4',
21
- 'facebook' => ''
22
- },
23
- 'editable' => true,
24
- 'deletable' => false,
25
- 'used' => 3,
26
- 'versions' => [
27
- {
28
- 'value' => '0',
29
- 'userid' => '1',
30
- 'firstname' => 'Alice',
31
- 'label' => 'Current version - Alice',
32
- 'selected' => 'selected'
33
- }
34
- ],
35
- 'sections' => ['beavers', 'cubs'],
36
- 'tags' => ['Tag 1', 'Tag2'],
37
- 'files' => [
38
- {
39
- 'fileid' => '6',
40
- 'activityid' => '1',
41
- 'filename' => 'File Name',
42
- 'name' => 'Name',
43
- }
44
- ],
45
- 'badges' => [
46
- {
4
+ describe "Activity" do
5
+
6
+ it "Get OSM link" do
7
+ activity = Osm::Activity.new(
8
+ :id => 1,
9
+ :running_time => 10,
10
+ :title => 'Title',
11
+ :description => 'Description',
12
+ :resources => 'Resources',
13
+ :instructions => 'Instructions',
14
+ :location => :indoors,
15
+ )
16
+ activity.osm_link.should == 'https://www.onlinescoutmanager.co.uk/?l=p1'
17
+ end
18
+
19
+
20
+ describe "Using The API" do
21
+
22
+ it "Get One" do
23
+ body = {
24
+ 'details' => {
47
25
  'activityid' => '1',
48
- 'section' => 'section',
49
- 'badgetype' => 'type',
50
- 'badge' => 'badge',
51
- 'columnname' => 'col_name',
52
- 'label' => 'This is a label',
53
- }
54
- ]
55
- }
56
- FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/programme.php?action=getActivity&id=1", :body => body.to_json)
57
-
58
-
59
- activity = Osm::Activity.get(@api, 1)
60
-
61
- activity.id.should == 1
62
- activity.version.should == 0
63
- activity.group_id.should == 2
64
- activity.user_id.should == 3
65
- activity.title.should == 'Activity Name'
66
- activity.description.should == 'Description'
67
- activity.resources.should == 'Resources'
68
- activity.instructions.should == 'Instructions'
69
- activity.running_time.should == 15
70
- activity.location.should == :indoors
71
- activity.shared.should == 0
72
- activity.rating.should == 4
73
- activity.editable.should == true
74
- activity.deletable.should == false
75
- activity.used.should == 3
76
- activity.versions[0].version.should == 0
77
- activity.versions[0].created_by.should == 1
78
- activity.versions[0].created_by_name.should == 'Alice'
79
- activity.versions[0].label.should == 'Current version - Alice'
80
- activity.sections.should == [:beavers, :cubs]
81
- activity.tags.should == ['Tag 1', 'Tag2']
82
- activity.files[0].id.should == 6
83
- activity.files[0].activity_id.should == 1
84
- activity.files[0].file_name.should == 'File Name'
85
- activity.files[0].name.should == 'Name'
86
- activity.badges[0].activity_id.should == 1
87
- activity.badges[0].section_type.should == :section
88
- activity.badges[0].type.should == :type
89
- activity.badges[0].badge.should == 'badge'
90
- activity.badges[0].requirement.should == 'col_name'
91
- activity.badges[0].label.should == 'This is a label'
92
-
93
- activity.valid?.should be_true
94
- end
95
-
96
-
97
- it "Add activity to programme (succeded)" do
98
- url = 'https://www.onlinescoutmanager.co.uk/programme.php?action=addActivityToProgramme'
99
- post_data = {
100
- 'apiid' => @CONFIGURATION[:api][:osm][:id],
101
- 'token' => @CONFIGURATION[:api][:osm][:token],
102
- 'userid' => 'user_id',
103
- 'secret' => 'secret',
104
- 'meetingdate' => '2000-01-02',
105
- 'sectionid' => 1,
106
- 'activityid' => 2,
107
- 'notes' => 'Notes',
108
- }
109
-
110
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":0}'}) }
111
- activity = Osm::Activity.new(:id => 2)
112
- activity.add_to_programme(@api, 1, Date.new(2000, 1, 2), 'Notes').should be_true
26
+ 'version' => '0',
27
+ 'groupid' => '2',
28
+ 'userid' => '3',
29
+ 'title' => 'Activity Name',
30
+ 'description' => 'Description',
31
+ 'resources' => 'Resources',
32
+ 'instructions' => 'Instructions',
33
+ 'runningtime' => '15',
34
+ 'location' => 'indoors',
35
+ 'shared' => '0',
36
+ 'rating' => '4',
37
+ 'facebook' => ''
38
+ },
39
+ 'editable' => true,
40
+ 'deletable' => false,
41
+ 'used' => 3,
42
+ 'versions' => [
43
+ {
44
+ 'value' => '0',
45
+ 'userid' => '1',
46
+ 'firstname' => 'Alice',
47
+ 'label' => 'Current version - Alice',
48
+ 'selected' => 'selected'
49
+ }
50
+ ],
51
+ 'sections' => ['beavers', 'cubs'],
52
+ 'tags' => ['Tag 1', 'Tag2'],
53
+ 'files' => [
54
+ {
55
+ 'fileid' => '6',
56
+ 'activityid' => '1',
57
+ 'filename' => 'File Name',
58
+ 'name' => 'Name',
59
+ }
60
+ ],
61
+ 'badges' => [
62
+ {
63
+ 'activityid' => '1',
64
+ 'section' => 'section',
65
+ 'badgetype' => 'type',
66
+ 'badge' => 'badge',
67
+ 'columnname' => 'col_name',
68
+ 'label' => 'This is a label',
69
+ }
70
+ ]
71
+ }
72
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/programme.php?action=getActivity&id=1", :body => body.to_json)
73
+
74
+
75
+ activity = Osm::Activity.get(@api, 1)
76
+
77
+ activity.id.should == 1
78
+ activity.version.should == 0
79
+ activity.group_id.should == 2
80
+ activity.user_id.should == 3
81
+ activity.title.should == 'Activity Name'
82
+ activity.description.should == 'Description'
83
+ activity.resources.should == 'Resources'
84
+ activity.instructions.should == 'Instructions'
85
+ activity.running_time.should == 15
86
+ activity.location.should == :indoors
87
+ activity.shared.should == 0
88
+ activity.rating.should == 4
89
+ activity.editable.should == true
90
+ activity.deletable.should == false
91
+ activity.used.should == 3
92
+ activity.versions[0].version.should == 0
93
+ activity.versions[0].created_by.should == 1
94
+ activity.versions[0].created_by_name.should == 'Alice'
95
+ activity.versions[0].label.should == 'Current version - Alice'
96
+ activity.sections.should == [:beavers, :cubs]
97
+ activity.tags.should == ['Tag 1', 'Tag2']
98
+ activity.files[0].id.should == 6
99
+ activity.files[0].activity_id.should == 1
100
+ activity.files[0].file_name.should == 'File Name'
101
+ activity.files[0].name.should == 'Name'
102
+ activity.badges[0].activity_id.should == 1
103
+ activity.badges[0].section_type.should == :section
104
+ activity.badges[0].type.should == :type
105
+ activity.badges[0].badge.should == 'badge'
106
+ activity.badges[0].requirement.should == 'col_name'
107
+ activity.badges[0].label.should == 'This is a label'
108
+
109
+ activity.valid?.should be_true
110
+ end
111
+
112
+
113
+ it "Add activity to programme (succeded)" do
114
+ url = 'https://www.onlinescoutmanager.co.uk/programme.php?action=addActivityToProgramme'
115
+ post_data = {
116
+ 'apiid' => @CONFIGURATION[:api][:osm][:id],
117
+ 'token' => @CONFIGURATION[:api][:osm][:token],
118
+ 'userid' => 'user_id',
119
+ 'secret' => 'secret',
120
+ 'meetingdate' => '2000-01-02',
121
+ 'sectionid' => 1,
122
+ 'activityid' => 2,
123
+ 'notes' => 'Notes',
124
+ }
125
+
126
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":0}'}) }
127
+ activity = Osm::Activity.new(:id => 2)
128
+ activity.add_to_programme(@api, 1, Date.new(2000, 1, 2), 'Notes').should be_true
129
+ end
130
+
131
+ it "Add activity to programme (failed)" do
132
+ HTTParty.should_receive(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":1}'}) }
133
+ activity = Osm::Activity.new(:id => 2)
134
+ activity.add_to_programme(@api, 1, Date.new(2000, 1, 2), 'Notes').should be_false
135
+ end
136
+
137
+
138
+ it "Update activity in OSM (succeded)" do
139
+ url = 'https://www.onlinescoutmanager.co.uk/programme.php?action=update'
140
+ post_data = {
141
+ 'apiid' => @CONFIGURATION[:api][:osm][:id],
142
+ 'token' => @CONFIGURATION[:api][:osm][:token],
143
+ 'userid' => 'user_id',
144
+ 'secret' => 'secret',
145
+ 'title' => 'title',
146
+ 'description' => 'description',
147
+ 'resources' => 'resources',
148
+ 'instructions' => 'instructions',
149
+ 'id' => 2,
150
+ 'files' => '3,4',
151
+ 'time' => '5',
152
+ 'location' => :indoors,
153
+ 'sections' => '["beavers","cubs"]',
154
+ 'tags' => '["tag1","tag2"]',
155
+ 'links' => '[{"activityid":"2","section":"beavers","badgetype":"t","badge":"b","columnname":"r","label":"l"}]',
156
+ 'shared' => 0,
157
+ 'sectionid' => 1,
158
+ 'secretEdit' => true,
159
+ }
160
+
161
+ HTTParty.should_receive(:post).with(url, {:body => post_data}) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":true}'}) }
162
+ activity = Osm::Activity.new(
163
+ :id => 2,
164
+ :title => 'title',
165
+ :description => 'description',
166
+ :resources => 'resources',
167
+ :instructions => 'instructions',
168
+ :files => [Osm::Activity::File.new(:id=>3, :activity_id=>2, :file_name=>'fn', :name=>'n'), Osm::Activity::File.new(:id=>4, :activity_id=>2, :file_name=>'fn2', :name=>'n2')],
169
+ :running_time => 5,
170
+ :location => :indoors,
171
+ :sections => [:beavers, :cubs],
172
+ :tags => ['tag1', 'tag2'],
173
+ :badges => [Osm::Activity::Badge.new(:activity_id=>2, :section_type=>:beavers, :type=>:t, :badge=>'b', :requirement=>'r', :label=>'l')],
174
+ :shared => 0,
175
+ :section_id => 1,
176
+ )
177
+ activity.update(@api, 1, true).should be_true
178
+ end
179
+
180
+ it "Update activity in OSM (failed)" do
181
+ HTTParty.should_receive(:post) { OsmTest::DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":false}'}) }
182
+ activity = Osm::Activity.new(
183
+ :id => 2,
184
+ :title => 'title',
185
+ :description => 'description',
186
+ :resources => 'resources',
187
+ :instructions => 'instructions',
188
+ :location => :indoors,
189
+ :running_time => 0,
190
+ )
191
+ activity.update(@api, 1, true).should be_false
192
+ end
193
+
113
194
  end
114
195
 
115
- it "Add activity to programme (failed)" do
116
- HTTParty.should_receive(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":1}'}) }
117
- activity = Osm::Activity.new(:id => 2)
118
- activity.add_to_programme(@api, 1, Date.new(2000, 1, 2), 'Notes').should be_false
119
- end
120
-
121
-
122
- it "Update activity in OSM (succeded)" do
123
- url = 'https://www.onlinescoutmanager.co.uk/programme.php?action=update'
124
- post_data = {
125
- 'apiid' => @CONFIGURATION[:api][:osm][:id],
126
- 'token' => @CONFIGURATION[:api][:osm][:token],
127
- 'userid' => 'user_id',
128
- 'secret' => 'secret',
129
- 'title' => 'title',
130
- 'description' => 'description',
131
- 'resources' => 'resources',
132
- 'instructions' => 'instructions',
133
- 'id' => 2,
134
- 'files' => '3,4',
135
- 'time' => '5',
136
- 'location' => :indoors,
137
- 'sections' => '["beavers","cubs"]',
138
- 'tags' => '["tag1","tag2"]',
139
- 'links' => '[{"activityid":"2","section":"beavers","badgetype":"t","badge":"b","columnname":"r","label":"l"}]',
140
- 'shared' => 0,
141
- 'sectionid' => 1,
142
- 'secretEdit' => true,
143
- }
144
-
145
- HTTParty.should_receive(:post).with(url, {:body => post_data}) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":true}'}) }
146
- activity = Osm::Activity.new(
147
- :id => 2,
148
- :title => 'title',
149
- :description => 'description',
150
- :resources => 'resources',
151
- :instructions => 'instructions',
152
- :files => [Osm::Activity::File.new(:id=>3, :activity_id=>2, :file_name=>'fn', :name=>'n'), Osm::Activity::File.new(:id=>4, :activity_id=>2, :file_name=>'fn2', :name=>'n2')],
153
- :running_time => 5,
154
- :location => :indoors,
155
- :sections => [:beavers, :cubs],
156
- :tags => ['tag1', 'tag2'],
157
- :badges => [Osm::Activity::Badge.new(:activity_id=>2, :section_type=>:beavers, :type=>:t, :badge=>'b', :requirement=>'r', :label=>'l')],
158
- :shared => 0,
159
- :section_id => 1,
160
- )
161
- activity.update(@api, 1, true).should be_true
162
- end
163
-
164
- it "Update activity in OSM (failed)" do
165
- HTTParty.should_receive(:post) { DummyHttpResult.new(:response=>{:code=>'200', :body=>'{"result":false}'}) }
166
- activity = Osm::Activity.new(
167
- :id => 2,
168
- :title => 'title',
169
- :description => 'description',
170
- :resources => 'resources',
171
- :instructions => 'instructions',
172
- :location => :indoors,
173
- :running_time => 0,
174
- )
175
- activity.update(@api, 1, true).should be_false
176
- end
177
-
178
-
179
- end
196
+ end