osm 0.0.26 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +57 -0
- data/README.md +13 -7
- data/lib/osm.rb +47 -22
- data/lib/osm/activity.rb +52 -57
- data/lib/osm/api.rb +115 -1031
- data/lib/osm/api_access.rb +73 -36
- data/lib/osm/due_badges.rb +27 -12
- data/lib/osm/evening.rb +118 -55
- data/lib/osm/event.rb +275 -17
- data/lib/osm/flexi_record.rb +131 -0
- data/lib/osm/grouping.rb +37 -15
- data/lib/osm/member.rb +100 -41
- data/lib/osm/model.rb +95 -0
- data/lib/osm/register.rb +177 -0
- data/lib/osm/section.rb +163 -71
- data/lib/osm/term.rb +135 -21
- data/spec/osm/activity_spec.rb +7 -4
- data/spec/osm/api_access_spec.rb +44 -36
- data/spec/osm/api_spec.rb +32 -1147
- data/spec/osm/due_badges_spec.rb +8 -1
- data/spec/osm/evening_spec.rb +119 -54
- data/spec/osm/event_spec.rb +363 -13
- data/spec/osm/flexi_record_spec.rb +128 -0
- data/spec/osm/grouping_spec.rb +9 -5
- data/spec/osm/member_spec.rb +111 -36
- data/spec/osm/model_spec.rb +140 -0
- data/spec/osm/osm_spec.rb +7 -31
- data/spec/osm/register_spec.rb +103 -0
- data/spec/osm/section_spec.rb +208 -92
- data/spec/osm/term_spec.rb +164 -28
- data/spec/spec_helper.rb +22 -0
- data/version.rb +1 -1
- metadata +22 -29
- data/lib/osm/event_attendance.rb +0 -55
- data/lib/osm/flexi_record_data.rb +0 -51
- data/lib/osm/flexi_record_field.rb +0 -42
- data/lib/osm/register_data.rb +0 -64
- data/lib/osm/register_field.rb +0 -42
- data/lib/osm/role.rb +0 -133
- data/spec/osm/api_strangeness_spec.rb +0 -83
- data/spec/osm/event_attendance_spec.rb +0 -34
- data/spec/osm/flexi_record_data_spec.rb +0 -40
- data/spec/osm/flexi_record_field_spec.rb +0 -23
- data/spec/osm/register_data_spec.rb +0 -35
- data/spec/osm/register_field_spec.rb +0 -24
- data/spec/osm/role_spec.rb +0 -118
data/lib/osm/register.rb
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
module Osm
|
2
|
+
|
3
|
+
class Register
|
4
|
+
|
5
|
+
# Get register structure
|
6
|
+
# @param [Osm::Api] api The api to use to make the request
|
7
|
+
# @param [Osm::Section, Fixnum] section the section (or its ID) to get the structure for
|
8
|
+
# @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
|
9
|
+
# @!macro options_get
|
10
|
+
# @return [Array<Osm::Register::Field>] representing the fields of the register
|
11
|
+
def self.get_structure(api, section, term=nil, options={})
|
12
|
+
section_id = section.to_i
|
13
|
+
term_id = term.nil? ? Osm::Term.get_current_term_for_section(api, section).id : term.to_i
|
14
|
+
cache_key = ['register_structure', section_id, term_id]
|
15
|
+
|
16
|
+
if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key) && Osm::Model.get_user_permissions(api, section_id)[:register].include?(:read)
|
17
|
+
return Osm::Model.cache_read(api, cache_key)
|
18
|
+
end
|
19
|
+
|
20
|
+
data = api.perform_query("users.php?action=registerStructure§ionid=#{section_id}&termid=#{term_id}")
|
21
|
+
|
22
|
+
structure = []
|
23
|
+
data.each do |item|
|
24
|
+
item['rows'].each do |row|
|
25
|
+
structure.push Field.new(
|
26
|
+
:id => row['field'],
|
27
|
+
:name => row['name'],
|
28
|
+
:tooltip => row['tooltip'],
|
29
|
+
)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
Osm::Model.cache_write(api, cache_key, structure)
|
33
|
+
|
34
|
+
return structure
|
35
|
+
end
|
36
|
+
|
37
|
+
# Get register attendance
|
38
|
+
# @param [Osm::Api] api The api to use to make the request
|
39
|
+
# @param [Osm::Section, Fixnum] section the section (or its ID) to get the register for
|
40
|
+
# @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
|
41
|
+
# @!macro options_get
|
42
|
+
# @return [Array<Register::Attendance>] representing the attendance of each member
|
43
|
+
def self.get_attendance(api, section, term=nil, options={})
|
44
|
+
section_id = section.to_i
|
45
|
+
term_id = term.nil? ? Osm::Term.get_current_term_for_section(api, section).id : term.to_i
|
46
|
+
cache_key = ['register_attendance', section_id, term_id]
|
47
|
+
|
48
|
+
if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key) && Osm::Model.get_user_permissions(api, section_id)[:register].include?(:read)
|
49
|
+
return Osm::Model.cache_read(api, cache_key)
|
50
|
+
end
|
51
|
+
|
52
|
+
data = api.perform_query("users.php?action=register§ionid=#{section_id}&termid=#{term_id}")
|
53
|
+
|
54
|
+
data = data['items']
|
55
|
+
to_return = []
|
56
|
+
data.each do |item|
|
57
|
+
to_return.push Osm::Register::Attendance.new(
|
58
|
+
:member_id => Osm::to_i_or_nil(item['scoutid']),
|
59
|
+
:grouping_id => Osm::to_i_or_nil(item ['patrolid']),
|
60
|
+
:section_id => Osm::to_i_or_nil(item['sectionid']),
|
61
|
+
:first_name => item['firstname'],
|
62
|
+
:last_name => item['lastname'],
|
63
|
+
:total => item['total'].to_i,
|
64
|
+
:attendance => item.select { |key, value| key.to_s.match(Osm::OSM_DATE_REGEX) }.
|
65
|
+
inject({}){ |new_hash,(date, attendance)| new_hash[Date.strptime(date, Osm::OSM_DATE_FORMAT)] = attendance; new_hash },
|
66
|
+
)
|
67
|
+
end
|
68
|
+
Osm::Model.cache_write(api, cache_key, to_return)
|
69
|
+
return to_return
|
70
|
+
end
|
71
|
+
|
72
|
+
# Update attendance for an evening in OSM
|
73
|
+
# @param [Hash] data
|
74
|
+
# @option data [Osm::Api] :api The api to use to make the request
|
75
|
+
# @option data [Osm::Section] :section the section to update the register for
|
76
|
+
# @option data [Osm::Term, Fixnum, nil] :term the term (or its ID) to get the register for, passing nil causes the current term to be used
|
77
|
+
# @option data [Osm::Evening, DateTime, Date] :evening the evening to update the register on
|
78
|
+
# @option data [String] :attendance what to mark the attendance as, one of "Yes", "No" or "Absent"
|
79
|
+
# @option data [Fixnum, Array<Fixnum>, Osm::Member, Array<Osm::Member>] :members the members (or their ids) to update
|
80
|
+
# @option data [Array<Hash>] :completed_badge_requirements (optional) the badge requirements to mark as completed, selected from the Hash returned by the get_badge_requirements_for_evening method
|
81
|
+
# @return [Boolean] wether the update succedded
|
82
|
+
def self.update_attendance(data={})
|
83
|
+
raise ArgumentIsInvalid, ':attendance is invalid' unless ['Yes', 'No', 'Absent'].include?(data[:attendance])
|
84
|
+
raise ArgumentIsInvalid, ':section is missing' if data[:section].nil?
|
85
|
+
raise ArgumentIsInvalid, ':evening is missing' if data[:evening].nil?
|
86
|
+
raise ArgumentIsInvalid, ':members is missing' if data[:members].nil?
|
87
|
+
|
88
|
+
api = data[:api]
|
89
|
+
term_id = data[:term].nil? ? Osm::Term.get_current_term_for_section(api, section).id : data[:term].to_i
|
90
|
+
|
91
|
+
data[:members] = [*data[:members]].map{ |member| (member.is_a?(Fixnum) ? member : member.id).to_s } # Make sure it's an Array of Strings
|
92
|
+
|
93
|
+
response = api.perform_query("users.php?action=registerUpdate§ionid=#{data[:section].id}&termid=#{term_id}", {
|
94
|
+
'scouts' => data[:members].inspect,
|
95
|
+
'selectedDate' => data[:evening].strftime(Osm::OSM_DATE_FORMAT),
|
96
|
+
'present' => data[:attendance],
|
97
|
+
'section' => data[:section].type,
|
98
|
+
'sectionid' => data[:section].id,
|
99
|
+
'completedBadges' => (data[:completed_badge_requirements] || []).to_json
|
100
|
+
})
|
101
|
+
|
102
|
+
# The cached attendance will be out of date - remove them
|
103
|
+
Osm::Model.cache_delete(api, ['register_attendance', data[:section].id, term_id])
|
104
|
+
|
105
|
+
return response.is_a?(Array)
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
class Field < Osm::Model
|
110
|
+
# @!attribute [rw] id
|
111
|
+
# @return [String] OSM identifier for the field
|
112
|
+
# @!attribute [rw] name
|
113
|
+
# @return [String] Human readable name for the field
|
114
|
+
# @!attribute [rw] tooltip
|
115
|
+
# @return [String] Tooltip for the field
|
116
|
+
|
117
|
+
attribute :id, :type => String
|
118
|
+
attribute :name, :type => String
|
119
|
+
attribute :tooltip, :type => String, :default => ''
|
120
|
+
|
121
|
+
attr_accessible :id, :name, :tooltip
|
122
|
+
|
123
|
+
validates_presence_of :id
|
124
|
+
validates_presence_of :name
|
125
|
+
validates_presence_of :tooltip, :allow_blank => true
|
126
|
+
|
127
|
+
# @!method initialize
|
128
|
+
# Initialize a new RegisterField
|
129
|
+
# @param [Hash] attributes the hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
|
130
|
+
|
131
|
+
end # Class Register::Field
|
132
|
+
|
133
|
+
|
134
|
+
class Attendance < Osm::Model
|
135
|
+
# @!attribute [rw] member_id
|
136
|
+
# @return [Fixnum] The OSM ID for the member
|
137
|
+
# @!attribute [rw] grouping_id
|
138
|
+
# @return [Fixnum] The OSM ID for the member's grouping
|
139
|
+
# @!attribute [rw] section_id
|
140
|
+
# @return [Fixnum] The OSM ID for the member's section
|
141
|
+
# @!attribute [rw] first_name
|
142
|
+
# @return [String] The member's first name
|
143
|
+
# @!attribute [rw] last_name
|
144
|
+
# @return [String] The member's last name
|
145
|
+
# @!attribute [rw] total
|
146
|
+
# @return [FixNum] Total
|
147
|
+
# @!attribute [rw] attendance
|
148
|
+
# @return [Hash] The data for each field - keys are the date, values one of 'Yes' (present), 'No' (known absence) or nil (absent)
|
149
|
+
|
150
|
+
attribute :member_id, :type => Integer
|
151
|
+
attribute :grouping_id, :type => Integer
|
152
|
+
attribute :section_id, :type => Integer
|
153
|
+
attribute :first_name, :type => String
|
154
|
+
attribute :last_name, :type => String
|
155
|
+
attribute :total, :type => Integer
|
156
|
+
attribute :attendance, :default => {}
|
157
|
+
|
158
|
+
attr_accessible :member_id, :first_name, :last_name, :section_id, :grouping_id, :total, :attendance
|
159
|
+
|
160
|
+
validates_numericality_of :member_id, :only_integer=>true, :greater_than=>0
|
161
|
+
validates_numericality_of :grouping_id, :only_integer=>true, :greater_than_or_equal_to=>-2
|
162
|
+
validates_numericality_of :section_id, :only_integer=>true, :greater_than=>0
|
163
|
+
validates_numericality_of :total, :only_integer=>true, :greater_than_or_equal_to=>0
|
164
|
+
validates_presence_of :first_name
|
165
|
+
validates_presence_of :last_name
|
166
|
+
|
167
|
+
validates :attendance, :hash => {:key_type => Date, :value_in => ['Yes', 'No', nil]}
|
168
|
+
|
169
|
+
# @!method initialize
|
170
|
+
# Initialize a new registerData
|
171
|
+
# @param [Hash] attributes the hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
|
172
|
+
|
173
|
+
end # Class Register::Data
|
174
|
+
|
175
|
+
end # Class Register
|
176
|
+
|
177
|
+
end # Module
|
data/lib/osm/section.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
module Osm
|
2
2
|
|
3
|
-
class Section
|
3
|
+
class Section < Osm::Model
|
4
4
|
class FlexiRecord; end # Ensure the constant exists for the validators
|
5
5
|
|
6
|
-
include ::ActiveAttr::MassAssignmentSecurity
|
7
|
-
include ::ActiveAttr::Model
|
8
|
-
|
9
6
|
# @!attribute [rw] id
|
10
7
|
# @return [Fixnum] the id for the section
|
11
8
|
# @!attribute [rw] name
|
12
9
|
# @return [String] the section name
|
10
|
+
# @!attribute [rw] group_id
|
11
|
+
# @return [Fixnum] the id for the group
|
12
|
+
# @!attribute [rw] group_name
|
13
|
+
# @return [String] the group name
|
13
14
|
# @!attribute [rw] subscription_level
|
14
15
|
# @return [Symbol] what subscription the section has to OSM (:bronze, :silver or :gold)
|
15
16
|
# @!attribute [rw] subscription_expires
|
@@ -28,11 +29,11 @@ module Osm
|
|
28
29
|
# @return [Hash] which columns are shown in the OSM mobile app
|
29
30
|
# @!attribute [rw] flexi_records
|
30
31
|
# @return [Array<FlexiRecord>] list of the extra records the section has
|
31
|
-
# @!attribute [rw] role
|
32
|
-
# @return [Osm::Role] the role linking the user to this section
|
33
32
|
|
34
33
|
attribute :id, :type => Integer
|
35
34
|
attribute :name, :type => String
|
35
|
+
attribute :group_id, :type => Integer
|
36
|
+
attribute :group_name, :type => String
|
36
37
|
attribute :subscription_level, :default => :unknown
|
37
38
|
attribute :subscription_expires, :type => Date
|
38
39
|
attribute :type, :default => :unknown
|
@@ -42,12 +43,15 @@ module Osm
|
|
42
43
|
attribute :intouch_fields, :default => {}
|
43
44
|
attribute :mobile_fields, :default => {}
|
44
45
|
attribute :flexi_records, :default => []
|
45
|
-
attribute :role
|
46
46
|
|
47
|
-
attr_accessible :id, :name, :
|
47
|
+
attr_accessible :id, :name, :group_id, :group_name, :subscription_level, :subscription_expires, :type,
|
48
|
+
:num_scouts, :column_names, :fields, :intouch_fields, :mobile_fields, :flexi_records
|
48
49
|
|
49
50
|
validates_numericality_of :id, :only_integer=>true, :greater_than=>0, :allow_nil => true
|
51
|
+
validates_numericality_of :group_id, :only_integer=>true, :greater_than=>0, :allow_nil => true
|
50
52
|
validates_numericality_of :num_scouts, :only_integer=>true, :greater_than_or_equal_to=>0
|
53
|
+
validates_presence_of :name
|
54
|
+
validates_presence_of :group_name
|
51
55
|
validates_presence_of :subscription_level
|
52
56
|
validates_presence_of :subscription_expires
|
53
57
|
validates_presence_of :type
|
@@ -56,14 +60,13 @@ module Osm
|
|
56
60
|
validates_presence_of :intouch_fields, :unless => Proc.new { |a| a.intouch_fields == {} }
|
57
61
|
validates_presence_of :mobile_fields, :unless => Proc.new { |a| a.mobile_fields == {} }
|
58
62
|
validates_presence_of :flexi_records, :unless => Proc.new { |a| a.flexi_records == [] }
|
59
|
-
validates_presence_of :role
|
60
63
|
|
61
64
|
validates_inclusion_of :subscription_level, :in => [:bronze, :silver, :gold, :unknown], :message => 'is not a valid level'
|
62
65
|
|
63
66
|
validates :column_names, :hash => {:key_type => Symbol, :value_type => String}
|
64
|
-
validates :fields, :hash => {:key_type => Symbol, :
|
65
|
-
validates :intouch_fields, :hash => {:key_type => Symbol, :
|
66
|
-
validates :mobile_fields, :hash => {:key_type => Symbol, :
|
67
|
+
validates :fields, :hash => {:key_type => Symbol, :value_in => [true, false]}
|
68
|
+
validates :intouch_fields, :hash => {:key_type => Symbol, :value_in => [true, false]}
|
69
|
+
validates :mobile_fields, :hash => {:key_type => Symbol, :value_in => [true, false]}
|
67
70
|
|
68
71
|
|
69
72
|
# @!method initialize
|
@@ -71,40 +74,137 @@ module Osm
|
|
71
74
|
# @param [Hash] attributes the hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
|
72
75
|
|
73
76
|
|
74
|
-
#
|
75
|
-
# @param [
|
76
|
-
#
|
77
|
-
# @
|
78
|
-
|
79
|
-
|
80
|
-
subscription_levels =
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
:id => Osm::to_i_or_nil(id),
|
85
|
-
:name => name,
|
86
|
-
:subscription_level => (subscription_levels[subscription_level] unless subscription_level < 0) || :unknown,
|
87
|
-
:subscription_expires => data['subscription_expires'] ? Date.parse(data['subscription_expires'], 'yyyy-mm-dd') : nil,
|
88
|
-
:type => !data['sectionType'].nil? ? data['sectionType'].to_sym : :unknown,
|
89
|
-
:num_scouts => data['numscouts'],
|
90
|
-
:column_names => data['columnNames'].is_a?(Hash) ? Osm::symbolize_hash(data['columnNames']) : {},
|
91
|
-
:fields => data['fields'].is_a?(Hash) ? Osm::symbolize_hash(data['fields']) : {},
|
92
|
-
:intouch_fields => data['intouch'].is_a?(Hash) ? Osm::symbolize_hash(data['intouch']) : {},
|
93
|
-
:mobile_fields => data['mobFields'].is_a?(Hash) ? Osm::symbolize_hash(data['mobFields']) : {},
|
94
|
-
:role => role,
|
95
|
-
:flexi_records => [],
|
77
|
+
# Get the user's sections
|
78
|
+
# @param [Osm::Api] The api to use to make the request
|
79
|
+
# @!macro options_get
|
80
|
+
# @return [Array<Osm::Section>]
|
81
|
+
def self.get_all(api, options={})
|
82
|
+
cache_key = ['sections', api.user_id]
|
83
|
+
subscription_levels = {
|
84
|
+
1 => :bronze,
|
85
|
+
2 => :silver,
|
86
|
+
3 => :gold,
|
96
87
|
}
|
97
88
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
89
|
+
if !options[:no_cache] && cache_exist?(api, cache_key)
|
90
|
+
return cache_read(api, cache_key)
|
91
|
+
end
|
92
|
+
|
93
|
+
data = api.perform_query('api.php?action=getUserRoles')
|
94
|
+
|
95
|
+
result = Array.new
|
96
|
+
permissions = Hash.new
|
97
|
+
data.each do |role_data|
|
98
|
+
section_data = ActiveSupport::JSON.decode(role_data['sectionConfig'])
|
99
|
+
|
100
|
+
# Make sense of flexi records
|
101
|
+
fr_data = []
|
102
|
+
flexi_records = []
|
103
|
+
fr_data = section_data['extraRecords'] if section_data['extraRecords'].is_a?(Array)
|
104
|
+
fr_data = section_data['extraRecords'].values if section_data['extraRecords'].is_a?(Hash)
|
105
|
+
fr_data.each do |record_data|
|
106
|
+
# Expect item to be: {:name=>String, :extraid=>Fixnum}
|
107
|
+
# Sometimes get item as: [String, {"name"=>String, "extraid"=>Fixnum}]
|
108
|
+
record_data = record_data[1] if record_data.is_a?(Array)
|
109
|
+
flexi_records.push FlexiRecord.new(
|
110
|
+
:id => Osm::to_i_or_nil(record_data['extraid']),
|
111
|
+
:name => record_data['name'],
|
112
|
+
)
|
113
|
+
end
|
114
|
+
|
115
|
+
section = new(
|
116
|
+
:id => Osm::to_i_or_nil(role_data['sectionid']),
|
117
|
+
:name => role_data['sectionname'],
|
118
|
+
:subscription_level => (subscription_levels[section_data['subscription_level']] || :unknown),
|
119
|
+
:subscription_expires => Osm::parse_date(section_data['subscription_expires']),
|
120
|
+
:type => !section_data['sectionType'].nil? ? section_data['sectionType'].to_sym : :unknown,
|
121
|
+
:num_scouts => section_data['numscouts'],
|
122
|
+
:column_names => section_data['columnNames'].is_a?(Hash) ? Osm::symbolize_hash(section_data['columnNames']) : {},
|
123
|
+
:fields => section_data['fields'].is_a?(Hash) ? Osm::symbolize_hash(section_data['fields']) : {},
|
124
|
+
:intouch_fields => section_data['intouch'].is_a?(Hash) ? Osm::symbolize_hash(section_data['intouch']) : {},
|
125
|
+
:mobile_fields => section_data['mobFields'].is_a?(Hash) ? Osm::symbolize_hash(section_data['mobFields']) : {},
|
126
|
+
:flexi_records => flexi_records.sort,
|
127
|
+
:group_id => role_data['groupid'],
|
128
|
+
:group_name => role_data['groupname'],
|
129
|
+
)
|
130
|
+
|
131
|
+
result.push section
|
132
|
+
cache_write(api, ['section', section.id], section)
|
133
|
+
permissions.merge!(section.id => make_permissions_hash(role_data['permissions']))
|
134
|
+
end
|
135
|
+
|
136
|
+
set_user_permissions(api, get_user_permissions(api).merge(permissions))
|
137
|
+
cache_write(api, cache_key, result)
|
138
|
+
return result
|
139
|
+
end
|
140
|
+
|
141
|
+
|
142
|
+
# Get a section
|
143
|
+
# @param [Osm::Api] The api to use to make the request
|
144
|
+
# @param [Fixnum] section_id the section id of the required section
|
145
|
+
# @!macro options_get
|
146
|
+
# @return nil if an error occured or the user does not have access to that section
|
147
|
+
# @return [Osm::Section]
|
148
|
+
def self.get(api, section_id, options={})
|
149
|
+
cache_key = ['section', section_id]
|
150
|
+
|
151
|
+
if !options[:no_cache] && cache_exist?(api, cache_key) && get_user_permissions(api).keys.include?(section_id)
|
152
|
+
return cache_read(api, cache_key)
|
104
153
|
end
|
105
|
-
attributes[:flexi_records].sort
|
106
154
|
|
107
|
-
|
155
|
+
sections = get_all(api, options)
|
156
|
+
return nil unless sections.is_a? Array
|
157
|
+
|
158
|
+
sections.each do |section|
|
159
|
+
return section if section.id == section_id
|
160
|
+
end
|
161
|
+
return nil
|
162
|
+
end
|
163
|
+
|
164
|
+
|
165
|
+
# Get API user's permissions
|
166
|
+
# @param [Osm::Api] The api to use to make the request
|
167
|
+
# @!macro options_get
|
168
|
+
# @return nil if an error occured or the user does not have access to that section
|
169
|
+
# @return [Hash] {section_id => permissions_hash}
|
170
|
+
def self.fetch_user_permissions(api, options={})
|
171
|
+
cache_key = ['permissions', api.user_id]
|
172
|
+
|
173
|
+
if !options[:no_cache] && cache_exist?(api, cache_key)
|
174
|
+
return cache_read(api, cache_key)
|
175
|
+
end
|
176
|
+
|
177
|
+
data = api.perform_query('api.php?action=getUserRoles')
|
178
|
+
|
179
|
+
all_permissions = Hash.new
|
180
|
+
data.each do |item|
|
181
|
+
all_permissions.merge!(Osm::to_i_or_nil(item['sectionid']) => make_permissions_hash(item['permissions']))
|
182
|
+
end
|
183
|
+
cache_write(api, cache_key, all_permissions)
|
184
|
+
return all_permissions
|
185
|
+
end
|
186
|
+
|
187
|
+
# Get the section's notepads
|
188
|
+
# @param [Osm::Api] The api to use to make the request
|
189
|
+
# @!macro options_get
|
190
|
+
# @return [String] the section's notepad
|
191
|
+
def get_notepad(api, options={})
|
192
|
+
cache_key = ['notepad', id]
|
193
|
+
|
194
|
+
if !options[:no_cache] && self.class.cache_exist?(api, cache_key) && get_user_permissions(api).keys.include?(section_id)
|
195
|
+
return self.class.cache_read(api, cache_key)
|
196
|
+
end
|
197
|
+
|
198
|
+
notepads = api.perform_query('api.php?action=getNotepads')
|
199
|
+
return '' unless notepads.is_a?(Hash)
|
200
|
+
|
201
|
+
notepad = ''
|
202
|
+
notepads.each do |key, value|
|
203
|
+
self.class.cache_write(api, ['notepad', key.to_i], value)
|
204
|
+
notepad = value if key.to_i == id
|
205
|
+
end
|
206
|
+
|
207
|
+
return notepad
|
108
208
|
end
|
109
209
|
|
110
210
|
# Check if this section is one of the youth sections
|
@@ -140,7 +240,14 @@ module Osm
|
|
140
240
|
|
141
241
|
def <=>(another)
|
142
242
|
begin
|
143
|
-
|
243
|
+
compare_group_name = group_name <=> another.group_name
|
244
|
+
return compare_group_name unless compare_group_name == 0
|
245
|
+
|
246
|
+
return 0 if section.type == another.type
|
247
|
+
[:beavers, :cubs, :scouts, :explorers, :waiting, :adults].each do |type|
|
248
|
+
return -1 if section.type == type
|
249
|
+
return 1 if another.type == type
|
250
|
+
end
|
144
251
|
rescue NoMethodError
|
145
252
|
return 1
|
146
253
|
end
|
@@ -153,23 +260,22 @@ module Osm
|
|
153
260
|
return false
|
154
261
|
end
|
155
262
|
end
|
156
|
-
|
157
|
-
def inspect
|
158
|
-
attribute_descriptions = attributes.merge('role' => (role.nil? ? nil : role.inspect_without_section(self)))
|
159
|
-
return_inspect(attribute_descriptions)
|
160
|
-
end
|
161
|
-
|
162
|
-
def inspect_without_role(exclude_role)
|
163
|
-
attribute_descriptions = (role == exclude_role) ? attributes.merge('role' => 'SET') : attributes
|
164
|
-
return_inspect(attribute_descriptions)
|
165
|
-
end
|
166
263
|
|
167
264
|
|
168
265
|
private
|
169
|
-
def
|
170
|
-
|
171
|
-
|
172
|
-
|
266
|
+
def self.make_permissions_hash(permissions)
|
267
|
+
return {} unless permissions.is_a?(Hash)
|
268
|
+
|
269
|
+
permissions_map = {
|
270
|
+
10 => [:read],
|
271
|
+
20 => [:read, :write],
|
272
|
+
100 => [:read, :write, :administer],
|
273
|
+
}
|
274
|
+
|
275
|
+
return permissions.inject({}) do |new_hash, (key, value)|
|
276
|
+
new_hash[key.to_sym] = (permissions_map[value] || [])
|
277
|
+
new_hash
|
278
|
+
end
|
173
279
|
end
|
174
280
|
|
175
281
|
|
@@ -195,20 +301,6 @@ module Osm
|
|
195
301
|
# Initialize a new FlexiRecord
|
196
302
|
# @param [Hash] attributes the hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
|
197
303
|
|
198
|
-
|
199
|
-
# Initialize a new FlexiRecord from api data
|
200
|
-
# @param [Hash] data the hash of data provided by the API
|
201
|
-
def self.from_api(data)
|
202
|
-
# Expect item to be: {:name=>String, :extraid=>Fixnum}
|
203
|
-
# Sometimes get item as: [String, {"name"=>String, "extraid"=>Fixnum}]
|
204
|
-
data = data[1] if data.is_a?(Array)
|
205
|
-
|
206
|
-
new({
|
207
|
-
:id => Osm::to_i_or_nil(data['extraid']),
|
208
|
-
:name => data['name'],
|
209
|
-
})
|
210
|
-
end
|
211
|
-
|
212
304
|
def <=>(another)
|
213
305
|
begin
|
214
306
|
return self.name <=> another.name
|