Class: Osm::Section

Inherits:
Model
  • Object
show all
Defined in:
lib/osm/section.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#<, #<=, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i

Constructor Details

#initializeObject

Initialize a new Section

Parameters:

  • attributes (Hash)

    The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)



# File 'lib/osm/section.rb', line 170

Instance Attribute Details

#column_namesHash

Returns custom names to use for the data columns.

Returns:

  • (Hash)

    custom names to use for the data columns



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#fieldsHash

Returns which columns are shown in OSM.

Returns:

  • (Hash)

    which columns are shown in OSM



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#flexi_recordsArray<FlexiRecord>

Returns list of the extra records the section has.

Returns:

  • (Array<FlexiRecord>)

    list of the extra records the section has



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#gocardlessBoolean

Returns does the section use gocardless.

Returns:

  • (Boolean)

    does the section use gocardless



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#group_idFixnum

Returns the id for the group.

Returns:

  • (Fixnum)

    the id for the group



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#group_nameString

Returns the group name.

Returns:

  • (String)

    the group name



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#idFixnum

Returns the id for the section.

Returns:

  • (Fixnum)

    the id for the section



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#intouch_fieldsHash

Returns which columns are shown in OSM’s in touch reports.

Returns:

  • (Hash)

    which columns are shown in OSM’s in touch reports



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#mobile_fieldsHash

Returns which columns are shown in the OSM mobile app.

Returns:

  • (Hash)

    which columns are shown in the OSM mobile app



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_badgesBoolean

Returns whether the section uses the Badges part of My.SCOUT.

Returns:

  • (Boolean)

    whether the section uses the Badges part of My.SCOUT



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_badges_expiresDate

Returns when the subscription to Badges in My.SCOUT expires.

Returns:

  • (Date)

    when the subscription to Badges in My.SCOUT expires



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_badges_partialBoolean

Returns Wether parents can see partially completed badges.

Returns:

  • (Boolean)

    Wether parents can see partially completed badges



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_detailsBoolean

Returns whether the section uses the Personal Details part of My.SCOUT.

Returns:

  • (Boolean)

    whether the section uses the Personal Details part of My.SCOUT



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_details_email_changes_toString

Returns email address to send changes to personal details made through My.SCOUT to.

Returns:

  • (String)

    email address to send changes to personal details made through My.SCOUT to



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_details_expiresBoolean

Returns whether the section uses the Personal Details part of My.SCOUT.

Returns:

  • (Boolean)

    whether the section uses the Personal Details part of My.SCOUT



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_email_address_copyString

Returns which email address to send copys of My.SCOUT emails to.

Returns:

  • (String)

    which email address to send copys of My.SCOUT emails to



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_email_address_fromString

Returns which email address to send My.SCOUT emails as coming from.

Returns:

  • (String)

    which email address to send My.SCOUT emails as coming from



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_emailsHash of Symbol to Boolean

Returns which email addresses are linked to MyScout for each Member.

Returns:

  • (Hash of Symbol to Boolean)

    which email addresses are linked to MyScout for each Member



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_event_reminder_countFixnum

Returns How many event reminders to send to parents who haven’t responded.

Returns:

  • (Fixnum)

    How many event reminders to send to parents who haven’t responded



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_event_reminder_frequencyFixnum

Returns How many days to leave between event reminder emails.

Returns:

  • (Fixnum)

    How many days to leave between event reminder emails



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_events_expiresDate

Returns when the subscription to Events in My.SCOUT expires.

Returns:

  • (Date)

    when the subscription to Events in My.SCOUT expires



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_payment_reminder_countFixnum

Returns How many payment reminders to send to parents who haven’t paid yet.

Returns:

  • (Fixnum)

    How many payment reminders to send to parents who haven’t paid yet



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_payment_reminder_frequencyFixnum

Returns How many days to leave between payment reminder emails.

Returns:

  • (Fixnum)

    How many days to leave between payment reminder emails



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_paymentsBoolean

Returns whether the section uses the Payments part of My.SCOUT.

Returns:

  • (Boolean)

    whether the section uses the Payments part of My.SCOUT



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_programmeBoolean

Returns whether the section uses the Programme part of My.SCOUT.

Returns:

  • (Boolean)

    whether the section uses the Programme part of My.SCOUT



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_programme_expiresDate

Returns when the subscription to Badges in My.SCOUT expires.

Returns:

  • (Date)

    when the subscription to Badges in My.SCOUT expires



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_programme_showFixnum

Returns How many programme itemms parents can see (the next 5, 10, 15, 20 meetings, -1 (whole term), 0 (remaining this term) or -2 (all future)).

Returns:

  • (Fixnum)

    How many programme itemms parents can see (the next 5, 10, 15, 20 meetings, -1 (whole term), 0 (remaining this term) or -2 (all future))



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_programme_summaryBoolean

Returns Wether parents can see summary of programme items.

Returns:

  • (Boolean)

    Wether parents can see summary of programme items



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#myscout_programme_timesBoolean

Returns Whether parents can see times of programme items.

Returns:

  • (Boolean)

    Whether parents can see times of programme items



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#nameString

Returns the section name.

Returns:

  • (String)

    the section name



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#sms_messages_remainingFixnum

Returns How many SMS messages the section can send before needing to topup.

Returns:

  • (Fixnum)

    How many SMS messages the section can send before needing to topup



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#sms_messages_sentFixnum

Returns How many SMS messages have been sent by the section.

Returns:

  • (Fixnum)

    How many SMS messages have been sent by the section



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#sms_sent_testBoolean

Returns Whether the section has sent their test SMS message.

Returns:

  • (Boolean)

    Whether the section has sent their test SMS message



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#subscription_expiresDate

Returns when the section’s subscription to OSM expires.

Returns:

  • (Date)

    when the section’s subscription to OSM expires



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#subscription_levelFixnum

Returns what subscription the section has to OSM (1-bronze, 2-silver, 3-gold, 4-gold+).

Returns:

  • (Fixnum)

    what subscription the section has to OSM (1-bronze, 2-silver, 3-gold, 4-gold+)



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

#typeSymbol

Returns the section type (:beavers, :cubs, :scouts, :exporers, :network, :adults, :waiting, :unknown).

Returns:

  • (Symbol)

    the section type (:beavers, :cubs, :scouts, :exporers, :network, :adults, :waiting, :unknown)



79
# File 'lib/osm/section.rb', line 79

attribute :id, :type => Integer

Class Method Details

.get(api, section_id, options = {}) ⇒ Object

Get a section

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section_id (Fixnum)

    The section id of the required section

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache

Returns:

  • nil if an error occured or the user does not have access to that section

  • (Osm::Section)


277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/osm/section.rb', line 277

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_all(api, options = {}) ⇒ Array<Osm::Section>

Get the user’s sections

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache

Returns:



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/osm/section.rb', line 179

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

  data = api.perform_query('api.php?action=getUserRoles')

  result = Array.new
  ids = Array.new
  permissions = Hash.new
  data.each do |role_data|
    unless role_data['section'].eql?('discount')  # It's not an actual section
      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'],
        :column_names => section_data['columnNames'].is_a?(Hash) ? Osm::symbolize_hash(section_data['columnNames']) : {},
        :fields => section_data['fields'].is_a?(Hash) ? Osm::symbolize_hash(section_data['fields']) : {},
        :intouch_fields => section_data['intouch'].is_a?(Hash) ? Osm::symbolize_hash(section_data['intouch']) : {},
        :mobile_fields => section_data['mobFields'].is_a?(Hash) ? Osm::symbolize_hash(section_data['mobFields']) : {},
        :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'],
        :sms_sent_test => section_data['hasSentTestSMS'],
        :sms_messages_sent => section_data['sms_sent'],
        :sms_messages_remaining => section_data['sms_remaining'],
      )

      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
  end

  permissions.each do |s_id, perms|
    api.set_user_permissions(s_id, perms)
  end
  cache_write(api, cache_key, ids)
  return result
end

Instance Method Details

#<=>(another) ⇒ Object

Compare Section based on group_name type (age order), then name



377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/osm/section.rb', line 377

def <=>(another)
  result = self.group_name <=> another.try(:group_name)
  result = 0 if self.type == another.try(:type) && result == 0
  [:beavers, :cubs, :scouts, :explorers, :network, :adults, :waiting].each do |type|
    if result == 0
      result = -1 if self.type == type
      result =  1 if another.try(:type) == type
    end
  end
  result = self.name <=> another.try(:name) if result == 0
  return result
end

#adults?Boolean

Check if this is an Adults section

Returns:

  • (Boolean)


362
363
364
365
366
# File 'lib/osm/section.rb', line 362

[:beavers, :cubs, :scouts, :explorers, :network, :adults, :waiting].each do |attribute|
  define_method "#{attribute}?" do
    type == attribute
  end
end

#beavers?Boolean

Check if this is a Beavers section

Returns:

  • (Boolean)


362
363
364
365
366
# File 'lib/osm/section.rb', line 362

[:beavers, :cubs, :scouts, :explorers, :network, :adults, :waiting].each do |attribute|
  define_method "#{attribute}?" do
    type == attribute
  end
end

#cubs?Boolean

Check if this is a Cubs section

Returns:

  • (Boolean)


362
363
364
365
366
# File 'lib/osm/section.rb', line 362

[:beavers, :cubs, :scouts, :explorers, :network, :adults, :waiting].each do |attribute|
  define_method "#{attribute}?" do
    type == attribute
  end
end

#explorers?Boolean

Check if this is an Explorers section

Returns:

  • (Boolean)


362
363
364
365
366
# File 'lib/osm/section.rb', line 362

[:beavers, :cubs, :scouts, :explorers, :network, :adults, :waiting].each do |attribute|
  define_method "#{attribute}?" do
    type == attribute
  end
end

#get_notepad(api, options = {}) ⇒ String

Get the section’s notepad from OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache

Returns:

  • (String)

    the section’s notepad



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/osm/section.rb', line 298

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|
    cache_write(api, ['notepad', key.to_i], value)
    notepad = value if key.to_i == id
  end

  return notepad
end

#network?Boolean

Check if this is a Network section

Returns:

  • (Boolean)


362
363
364
365
366
# File 'lib/osm/section.rb', line 362

[:beavers, :cubs, :scouts, :explorers, :network, :adults, :waiting].each do |attribute|
  define_method "#{attribute}?" do
    type == attribute
  end
end

#scouts?Boolean

Check if this is a Scouts section

Returns:

  • (Boolean)


362
363
364
365
366
# File 'lib/osm/section.rb', line 362

[:beavers, :cubs, :scouts, :explorers, :network, :adults, :waiting].each do |attribute|
  define_method "#{attribute}?" do
    type == attribute
  end
end

#set_notepad(api, content) ⇒ Boolean

Set the section’s notepad in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • content (String)

    The content of the notepad

Returns:

  • (Boolean)

    whether the notepad was sucessfully updated



322
323
324
325
326
327
328
329
330
331
# File 'lib/osm/section.rb', line 322

def set_notepad(api, content)
  require_access_to_section(api, self)
  data = api.perform_query("users.php?action=updateNotepad&sectionid=#{id}", {'value' => content})

  if data.is_a?(Hash) && data['ok'] # Success
    cache_write(api, ['notepad', id], content)
    return true
  end
  return false
end

#subscription_level_nameString?

Deprecated.

Please use Osm::SUBSCRIPTION_LEVEL_NAMES[section.subscription_level instead

Get the name for the section’s subscription level

Returns:

  • (String, nil)

    the name of the subscription level (nil if no name exists)



371
372
373
374
# File 'lib/osm/section.rb', line 371

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

#waiting?Boolean

Check if this is a waiting list

Returns:

  • (Boolean)


362
363
364
365
366
# File 'lib/osm/section.rb', line 362

[:beavers, :cubs, :scouts, :explorers, :network, :adults, :waiting].each do |attribute|
  define_method "#{attribute}?" do
    type == attribute
  end
end

#youth_section?Boolean

Check if this section is one of the youth sections

Returns:

  • (Boolean)


336
337
338
# File 'lib/osm/section.rb', line 336

def youth_section?
  [:beavers, :cubs, :scouts, :explorers].include?(type)
end