class Osm::Section
Public Class Methods
Get a section @param [Osm::Api] api The api to use to make the request @param [Fixnum] section_id The section id of the required section @!macro options_get @return nil if an error occured or the user does not have access to that section @return [Osm::Section]
# File lib/osm/section.rb, line 238 def self.get(api, section_id, options={}) cache_key = ['section', section_id] if !options[:no_cache] && cache_exist?(api, cache_key) && can_access_section?(api, section_id) return cache_read(api, cache_key) end sections = get_all(api, options) return nil unless sections.is_a? Array sections.each do |section| return section if section.id == section_id end return nil end
Get the user's sections @param [Osm::Api] api The api to use to make the request @!macro options_get @return [Array<Osm::Section>]
# File lib/osm/section.rb, line 148 def self.get_all(api, options={}) cache_key = ['sections', api.user_id] if !options[:no_cache] && cache_exist?(api, cache_key) ids = cache_read(api, cache_key) return get_from_ids(api, ids, 'section', options, :get_all) end result = Array.new ids = Array.new permissions = Hash.new api.get_user_roles(options).each do |role_data| next if role_data['section'].eql?('discount') # It's not an actual section next if role_data['sectionConfig'].nil? # No config for the section = user hasn't got access section_data = role_data['sectionConfig'].is_a?(String) ? ActiveSupport::JSON.decode(role_data['sectionConfig']) : role_data['sectionConfig'] myscout_data = section_data['portal'] || {} section_data['portalExpires'] ||= {} section_id = Osm::to_i_or_nil(role_data['sectionid']) # Make sense of flexi records fr_data = [] flexi_records = [] fr_data = section_data['extraRecords'] if section_data['extraRecords'].is_a?(Array) fr_data = section_data['extraRecords'].values if section_data['extraRecords'].is_a?(Hash) fr_data.each do |record_data| # Expect item to be: {:name=>String, :extraid=>Fixnum} # Sometimes get item as: [String, {"name"=>String, "extraid"=>Fixnum}] record_data = record_data[1] if record_data.is_a?(Array) flexi_records.push Osm::FlexiRecord.new( :id => Osm::to_i_or_nil(record_data['extraid']), :name => record_data['name'], :section_id => section_id, ) end section = new( :id => section_id, :name => role_data['sectionname'], :subscription_level => Osm::to_i_or_nil(section_data['subscription_level']), :subscription_expires => Osm::parse_date(section_data['subscription_expires']), :type => !section_data['sectionType'].nil? ? section_data['sectionType'].to_sym : (!section_data['section'].nil? ? section_data['section'].to_sym : :unknown), :num_scouts => section_data['numscouts'], :flexi_records => flexi_records.sort, :group_id => role_data['groupid'], :group_name => role_data['groupname'], :gocardless => (section_data['gocardless'] || 'false').downcase.eql?('true'), :myscout_events_expires => Osm::parse_date(section_data['portalExpires']['events']), :myscout_badges_expires => Osm::parse_date(section_data['portalExpires']['badges']), :myscout_programme_expires => Osm::parse_date(section_data['portalExpires']['programme']), :myscout_details_expires => Osm::parse_date(section_data['portalExpires']['details']), :myscout_events => myscout_data['events'] == 1, :myscout_badges => myscout_data['badges'] == 1, :myscout_programme => myscout_data['programme'] == 1, :myscout_payments => myscout_data['payments'] == 1, :myscout_details => myscout_data['details'] == 1, :myscout_emails => (myscout_data['emails'] || {}).inject({}) { |n,(k,v)| n[k.to_sym] = v.eql?('true'); n}, :myscout_email_address_from => myscout_data['emailAddress'] ? myscout_data['emailAddress'] : '', :myscout_email_address_copy => myscout_data['emailAddressCopy'] ? myscout_data['emailAddressCopy'] : '', :myscout_badges_partial => myscout_data['badgesPartial'] == 1, :myscout_programme_summary => myscout_data['programmeSummary'] == 1, :myscout_programme_times => myscout_data['programmeTimes'] == 1, :myscout_programme_show => myscout_data['programmeShow'].to_i, :myscout_event_reminder_count => myscout_data['eventRemindCount'].to_i, :myscout_event_reminder_frequency => myscout_data['eventRemindFrequency'].to_i, :myscout_payment_reminder_count => myscout_data['paymentRemindCount'].to_i, :myscout_payment_reminder_frequency => myscout_data['paymentRemindFrequency'].to_i, :myscout_details_email_changes_to => myscout_data['contactNotificationEmail'], ) result.push section ids.push section.id cache_write(api, ['section', section.id], section) permissions.merge!(section.id => Osm.make_permissions_hash(role_data['permissions'])) end permissions.each do |s_id, perms| api.set_user_permissions(s_id, perms) end cache_write(api, cache_key, ids) return result end
Public Instance Methods
Compare Section
based on group_name type (age order), then name
# File lib/osm/section.rb, line 380 def <=>(another) type_order = [:beavers, :cubs, :scouts, :explorers, :network, :adults, :waiting] result = self.group_name <=> another.try(:group_name) if result == 0 result = type_order.find_index(self.type) <=> type_order.find_index(another.try(:type)) end result = self.name <=> another.try(:name) if result == 0 return result end
Get the section's notepad from OSM @param [Osm::Api] api The api to use to make the request @!macro options_get @return [String] the section's notepad
# File lib/osm/section.rb, line 259 def get_notepad(api, options={}) require_access_to_section(api, self, options) cache_key = ['notepad', id] if !options[:no_cache] && cache_exist?(api, cache_key) && can_access_section?(api, self.id) return cache_read(api, cache_key) end notepads = api.perform_query('api.php?action=getNotepads') return '' unless notepads.is_a?(Hash) notepad = '' notepads.each do |key, value| raw_value = value.fetch('raw', '') cache_write(api, ['notepad', key.to_i], raw_value) notepad = raw_value if key.to_i == id end return notepad end
Set the section's notepad in OSM @param [Osm::Api] api The api to use to make the request @param [String] content The content of the notepad @return [Boolean] whether the notepad was sucessfully updated
# File lib/osm/section.rb, line 284 def set_notepad(api, content) require_access_to_section(api, self) data = api.perform_query("users.php?action=updateNotepad§ionid=#{id}", {'raw' => content}) if data.is_a?(Hash) && data['ok'] # Success cache_write(api, ['notepad', id], content) return true end return false end
Check if the section has a subscription of a given level (or higher) @param level [Fixnum, Symbol] the subscription level required @return [Boolean] Whether the section has a subscription of level (or higher)
# File lib/osm/section.rb, line 341 def subscription_at_least?(level) if level.is_a?(Symbol) # Convert to Fixnum case level when :bronze level = 1 when :silver level = 2 when :gold level = 3 when :gold_plus level = 4 else level = 0 end end return subscription_level >= level end
Get the name for the section's subscription level @return [String, nil] the name of the subscription level (nil if no name exists) @deprecated Please use Osm::SUBSCRIPTION_LEVEL_NAMES[section.subscription_level instead
# File lib/osm/section.rb, line 333 def subscription_level_name warn "[DEPRECATION] `subscription_level_name` is deprecated. Please use `Osm::SUBSCRIPTION_LEVEL_NAMES[section.subscription_level` instead." Osm::SUBSCRIPTION_LEVEL_NAMES[subscription_level] end
Check if this section is one of the youth sections @return [Boolean]
# File lib/osm/section.rb, line 298 def youth_section? [:beavers, :cubs, :scouts, :explorers].include?(type) end