osm 1.2.3 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.2
4
3
  - 1.9.3
5
4
  - 2.0.0
6
5
  branches:
@@ -1,3 +1,9 @@
1
+ ## Version 1.2.4
2
+
3
+ * Remove support for ruby 1.9.2 (listen gem requires >= 1.9.3)
4
+ * Activity's get\_badge\_requirements method now will iterate through activities if there's no permission to use the existing one OSM query trick
5
+ * Fix exception when OSM returns a number (instead of a string) for total/completed columns of a flexi record
6
+
1
7
  ## Version 1.2.3
2
8
 
3
9
  * Fix bug determining if a badge has been earnt (-1 sections required = all required)
data/README.md CHANGED
@@ -15,7 +15,6 @@ Staging [![Build Status](https://secure.travis-ci.org/robertgauld/osm.png?branch
15
15
  ## Ruby Versions
16
16
  This gem supports the following versions of ruby, it may work on other versions but is not tested against them so don't rely on it.
17
17
 
18
- * 1.9.2
19
18
  * 1.9.3
20
19
  * 2.0.0
21
20
 
@@ -98,8 +98,8 @@ module Osm
98
98
  }
99
99
  fields.merge!(
100
100
  'dob' => item['dob'].empty? ? nil : item['dob'],
101
- 'total' => item['total'].empty? ? nil : item['total'],
102
- 'completed' => item['completed'].empty? ? nil : item['completed'],
101
+ 'total' => item['total'].to_s.empty? ? nil : item['total'],
102
+ 'completed' => item['completed'].to_s.empty? ? nil : item['completed'],
103
103
  'age' => item['age'].empty? ? nil : item['age'],
104
104
  )
105
105
 
@@ -253,22 +253,47 @@ module Osm
253
253
 
254
254
 
255
255
  # Get the badge requirements met on a specific meeting
256
+ # Requires either write permission to badges (prefered as it's one OSM query)
257
+ # or read permission to programme.
256
258
  # @param [Osm::Api] api The api to use to make the request
257
259
  # @!macro options_get
258
260
  # @return [Array<Hash>] hashes ready to pass into the update_register method
261
+ # @return [nil] if something went wrong
259
262
  def get_badge_requirements(api, options={})
260
- require_ability_to(api, :read, :programme, section_id, options)
261
263
  section = Osm::Section.get(api, section_id)
262
264
  cache_key = ['badge_requirements', section.id, id]
263
-
264
265
  if !options[:no_cache] && cache_exist?(api, cache_key)
265
266
  return cache_read(api, cache_key)
266
267
  end
268
+ badges = nil
269
+
270
+ if user_has_permission?(api, :write, :badge, section_id, options)
271
+ # We can shortcut and do it in one query
272
+ badges = api.perform_query("users.php?action=getActivityRequirements&date=#{date.strftime(Osm::OSM_DATE_FORMAT)}&sectionid=#{section.id}&section=#{section.type}")
267
273
 
268
- data = api.perform_query("users.php?action=getActivityRequirements&date=#{date.strftime(Osm::OSM_DATE_FORMAT)}&sectionid=#{section.id}&section=#{section.type}")
274
+ else
275
+ # We'll have to iterate through the activities
276
+ require_ability_to(api, :read, :programme, section_id, options)
277
+ badges = []
278
+ activities.each do |activity|
279
+ activity = Osm::Activity.get(api, activity.activity_id, nil, options)
280
+ activity.badges.each do |badge|
281
+ badges.push ({
282
+ 'name' => badge.label,
283
+ 'badgeName' => badge.badge,
284
+ 'sectionid' => section_id.to_s,
285
+ 'eveningid' => id.to_s,
286
+ 'section' => badge.section_type,
287
+ 'badgetype' => badge.type,
288
+ 'badge' => badge.badge,
289
+ 'columnname' => badge.requirement,
290
+ })
291
+ end
292
+ end
293
+ end
269
294
 
270
- cache_write(api, cache_key, data)
271
- return data
295
+ cache_write(api, cache_key, badges) unless badges.nil?
296
+ return badges
272
297
  end
273
298
 
274
299
  # Compare Meeting based on section_id, date, start_time then id
@@ -131,28 +131,61 @@ module Osm
131
131
  api.get_user_permissions(options).keys.include?(section.to_i)
132
132
  end
133
133
 
134
- # Raise an exception if the user does not have the relevant permission
134
+ # Check if the user has the relevant permission
135
135
  # @param [Osm::Api] api The api to use to make the request
136
136
  # @param [Symbol] to What action is required to be done (e.g. :read or :write)
137
137
  # @param [Symbol] on What the OSM permission is required on (e.g. :member or :programme)
138
138
  # @param [Osm::Section, Fixnum, #to_i] section The Section (or its ID) the permission is required on
139
139
  # @!macro options_get
140
- # @raise [Osm::Forbidden] If the Api user does not have the required permission
141
- def self.require_permission(api, to, on, section, options={})
142
- section_id = section.to_i
140
+ def self.has_permission?(api, to, on, section, options={})
141
+ user_has_permission(api, to, on, section, options) && api_has_permission(api, to, on, section, options)
142
+ end
143
143
 
144
- # Check user's permissions in OSM
144
+ # Check if the user has the relevant permission within OSM
145
+ # @param [Osm::Api] api The api to use to make the request
146
+ # @param [Symbol] to What action is required to be done (e.g. :read or :write)
147
+ # @param [Symbol] on What the OSM permission is required on (e.g. :member or :programme)
148
+ # @param [Osm::Section, Fixnum, #to_i] section The Section (or its ID) the permission is required on
149
+ # @!macro options_get
150
+ def self.user_has_permission?(api, to, on, section, options={})
151
+ section_id = section.to_i
145
152
  permissions = api.get_user_permissions(options)
146
153
  permissions = permissions[section_id] || {}
147
154
  permissions = permissions[on] || []
148
155
  unless permissions.include?(to)
149
- raise Osm::Forbidden, "Your OSM user does not have permission to #{to} on #{on} for #{Osm::Section.get(api, section_id, options).try(:name)}"
156
+ return false
150
157
  end
158
+ return true
159
+ end
151
160
 
152
- # Check what the user gave our API
161
+ # Check if the user has granted the relevant permission to the API
162
+ # @param [Osm::Api] api The api to use to make the request
163
+ # @param [Symbol] to What action is required to be done (e.g. :read or :write)
164
+ # @param [Symbol] on What the OSM permission is required on (e.g. :member or :programme)
165
+ # @param [Osm::Section, Fixnum, #to_i] section The Section (or its ID) the permission is required on
166
+ # @!macro options_get
167
+ def self.api_has_permission?(api, to, on, section, options={})
168
+ section_id = section.to_i
153
169
  permissions = Osm::ApiAccess.get_ours(api, section_id, options).permissions
154
170
  permissions = permissions[on] || []
155
171
  unless permissions.include?(to)
172
+ return false
173
+ end
174
+ return true
175
+ end
176
+
177
+ # Raise an exception if the user does not have the relevant permission
178
+ # @param [Osm::Api] api The api to use to make the request
179
+ # @param [Symbol] to What action is required to be done (e.g. :read or :write)
180
+ # @param [Symbol] on What the OSM permission is required on (e.g. :member or :programme)
181
+ # @param [Osm::Section, Fixnum, #to_i] section The Section (or its ID) the permission is required on
182
+ # @!macro options_get
183
+ # @raise [Osm::Forbidden] If the Api user does not have the required permission
184
+ def self.require_permission(api, to, on, section, options={})
185
+ unless user_has_permission?(api, to, on, section, options)
186
+ raise Osm::Forbidden, "Your OSM user does not have permission to #{to} on #{on} for #{Osm::Section.get(api, section_id, options).try(:name)}"
187
+ end
188
+ unless api_has_permission?(api, to, on, section, options)
156
189
  raise Osm::Forbidden, "You have not granted the #{to} permissions on #{on} to the #{api.api_name} API for #{Osm::Section.get(api, section_id, options).try(:name)}"
157
190
  end
158
191
  end
@@ -229,7 +262,8 @@ module Osm
229
262
  # Make selected class methods instance methods too
230
263
  %w{
231
264
  cache_read cache_write cache_exist? cache_delete require_access_to_section
232
- can_access_section? require_permission require_subscription require_ability_to
265
+ can_access_section? has_permission? user_has_permission? api_has_permission?
266
+ require_permission require_subscription require_ability_to
233
267
  }.each do |method_name|
234
268
  define_method method_name do |*options|
235
269
  self.class.send(method_name, *options)
@@ -531,4 +531,37 @@ describe "Flexi Record" do
531
531
 
532
532
  end
533
533
 
534
+ describe "API Strangeness" do
535
+
536
+ it "Calculated columns containing numbers not strings" do
537
+ data = {
538
+ 'identifier' => 'scoutid',
539
+ 'label' => "name",
540
+ 'items' => [{
541
+ "scoutid" => "1",
542
+ "firstname" => "First",
543
+ "lastname" => "Last",
544
+ "dob" => "",
545
+ "patrolid" => "2",
546
+ "total" => 3,
547
+ "completed" => 4,
548
+ "f_1" => "A",
549
+ "f_2" => "B",
550
+ "age" => "",
551
+ "patrol" => "Green"
552
+ }]
553
+ }
554
+ 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, :content_type => 'application/json')
555
+ Osm::Section.stub(:get) { Osm::Section.new(:id => 1, :type => :cubs) }
556
+
557
+ flexi_record = Osm::FlexiRecord.new(:section_id => 1, :id => 2, :name => 'A Flexi Record')
558
+ records = flexi_record.get_data(@api, 3)
559
+ record = records[0]
560
+ record.fields['total'].should == 3
561
+ record.fields['completed'].should == 4
562
+ end
563
+
564
+ end
565
+
534
566
  end
567
+
@@ -156,11 +156,11 @@ describe "Meeting" do
156
156
  badge_link.label.should == 'Cubs Artist Activity - A: Poster'
157
157
  end
158
158
 
159
- it "Fetch badge requirements for a meeting" do
159
+ it "Fetch badge requirements for a meeting (from API)" do
160
160
  badges_body = [{'a'=>'a'},{'a'=>'A'}]
161
161
  FakeWeb.register_uri(:post, 'https://www.onlinescoutmanager.co.uk/users.php?action=getActivityRequirements&date=2000-01-02&sectionid=3&section=cubs', :body => badges_body.to_json, :content_type => 'application/json')
162
162
  roles_body = [
163
- {"sectionConfig"=>"{\"subscription_level\":1,\"subscription_expires\":\"2013-01-05\",\"sectionType\":\"cubs\",\"columnNames\":{\"column_names\":\"names\"},\"numscouts\":10,\"hasUsedBadgeRecords\":true,\"hasProgramme\":true,\"extraRecords\":[],\"wizard\":\"false\",\"fields\":{\"fields\":true},\"intouch\":{\"intouch_fields\":true},\"mobFields\":{\"mobile_fields\":true}}", "groupname"=>"3rd Somewhere", "groupid"=>"3", "groupNormalised"=>"1", "sectionid"=>"3", "sectionname"=>"Section 1", "section"=>"beavers", "isDefault"=>"1", "permissions"=>{"badge"=>10, "member"=>20, "user"=>100, "register"=>100, "contact"=>100, "programme"=>100, "originator"=>1, "events"=>100, "finance"=>100, "flexi"=>100}},
163
+ {"sectionConfig"=>"{\"subscription_level\":1,\"subscription_expires\":\"2013-01-05\",\"sectionType\":\"cubs\",\"columnNames\":{\"column_names\":\"names\"},\"numscouts\":10,\"hasUsedBadgeRecords\":true,\"hasProgramme\":true,\"extraRecords\":[],\"wizard\":\"false\",\"fields\":{\"fields\":true},\"intouch\":{\"intouch_fields\":true},\"mobFields\":{\"mobile_fields\":true}}", "groupname"=>"3rd Somewhere", "groupid"=>"3", "groupNormalised"=>"1", "sectionid"=>"3", "sectionname"=>"Section 1", "section"=>"beavers", "isDefault"=>"1", "permissions"=>{"badge"=>100, "member"=>20, "user"=>100, "register"=>100, "contact"=>100, "programme"=>100, "originator"=>1, "events"=>100, "finance"=>100, "flexi"=>100}},
164
164
  ]
165
165
  FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getUserRoles", :body => roles_body.to_json, :content_type => 'application/json')
166
166
 
@@ -168,6 +168,67 @@ describe "Meeting" do
168
168
  meeting.get_badge_requirements(@api).should == badges_body
169
169
  end
170
170
 
171
+ it "Fetch badge requirements for a meeting (iterating through activities)" do
172
+ roles_body = [
173
+ {"sectionConfig"=>"{\"subscription_level\":1,\"subscription_expires\":\"2013-01-05\",\"sectionType\":\"cubs\",\"columnNames\":{\"column_names\":\"names\"},\"numscouts\":10,\"hasUsedBadgeRecords\":true,\"hasProgramme\":true,\"extraRecords\":[],\"wizard\":\"false\",\"fields\":{\"fields\":true},\"intouch\":{\"intouch_fields\":true},\"mobFields\":{\"mobile_fields\":true}}", "groupname"=>"3rd Somewhere", "groupid"=>"3", "groupNormalised"=>"1", "sectionid"=>"3", "sectionname"=>"Section 1", "section"=>"beavers", "isDefault"=>"1", "permissions"=>{"badge"=>10, "member"=>20, "user"=>100, "register"=>100, "contact"=>100, "programme"=>10, "originator"=>1, "events"=>100, "finance"=>100, "flexi"=>100}},
174
+ ]
175
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/api.php?action=getUserRoles", :body => roles_body.to_json, :content_type => 'application/json')
176
+
177
+ activity_body = {
178
+ 'details' => {
179
+ 'activityid' => '4',
180
+ 'version' => '0',
181
+ 'groupid' => '2',
182
+ 'userid' => '3',
183
+ 'title' => 'Activity Name',
184
+ 'description' => 'Description',
185
+ 'resources' => 'Resources',
186
+ 'instructions' => 'Instructions',
187
+ 'runningtime' => '15',
188
+ 'location' => 'indoors',
189
+ 'shared' => '0',
190
+ 'rating' => '4',
191
+ 'facebook' => ''
192
+ },
193
+ 'editable' => true,
194
+ 'deletable' => false,
195
+ 'used' => 3,
196
+ 'versions' => [
197
+ {
198
+ 'value' => '0',
199
+ 'userid' => '1',
200
+ 'firstname' => 'Alice',
201
+ 'label' => 'Current version - Alice',
202
+ 'selected' => 'selected'
203
+ }
204
+ ],
205
+ 'sections' => ['beavers', 'cubs'],
206
+ 'tags' => ['Tag 1', 'Tag2'],
207
+ 'files' => [
208
+ {
209
+ 'fileid' => '6',
210
+ 'activityid' => '4',
211
+ 'filename' => 'File Name',
212
+ 'name' => 'Name',
213
+ }
214
+ ],
215
+ 'badges' => [
216
+ {
217
+ 'activityid' => '4',
218
+ 'section' => 'section',
219
+ 'badgetype' => 'type',
220
+ 'badge' => 'badge',
221
+ 'columnname' => 'col_name',
222
+ 'label' => 'This is a label',
223
+ }
224
+ ]
225
+ }
226
+ FakeWeb.register_uri(:post, "https://www.onlinescoutmanager.co.uk/programme.php?action=getActivity&id=4", :body => activity_body.to_json, :content_type => 'application/json')
227
+
228
+ meeting = Osm::Meeting.new(:id => 2, :date => Date.new(2000, 1, 2), :section_id => 3, :activities=>[Osm::Meeting::Activity.new(:activity_id => 4)])
229
+ meeting.get_badge_requirements(@api).should == [{"name"=>"This is a label", "badgeName"=>"badge", "sectionid"=>"3", "eveningid"=>"2", "section"=>:section, "badgetype"=>:type, "badge"=>"badge", "columnname"=>"col_name"}]
230
+ end
231
+
171
232
  it "Create a meeting (succeded)" do
172
233
  url = 'https://www.onlinescoutmanager.co.uk/programme.php?action=addActivityToProgramme'
173
234
  post_data = {
@@ -303,4 +364,4 @@ describe "Meeting" do
303
364
  end
304
365
 
305
366
  end
306
- end
367
+ end
data/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Osm
2
- VERSION = "1.2.3"
2
+ VERSION = "1.2.4"
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: 1.2.3
4
+ version: 1.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-28 00:00:00.000000000 Z
12
+ date: 2014-01-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport