Class: Osm::Badges

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

Defined Under Namespace

Classes: DueBadges

Class Method Summary collapse

Class Method Details

.get_due_badges(api, section, term = nil, options = {}) ⇒ Osm::Badges::DueBadges

Get due badges

Options Hash (options):

  • :no_cache (Boolean) — default: optional

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/osm/badges.rb', line 56

def self.get_due_badges(api, section, term=nil, options={})
  Osm::Model.require_ability_to(api, :read, :badge, section, options)
  section = Osm::Section.get(api, section, options) unless section.is_a?(Osm::Section)
  term_id = (term.nil? ? Osm::Term.get_current_term_for_section(api, section, options) : term).to_i
  cache_key = ['due_badges', section.id, term_id]

  if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key)
    return Osm::Model.cache_read(api, cache_key)
  end

  data = api.perform_query("challenges.php?action=outstandingBadges&section=#{section.type}&sectionid=#{section.id}&termid=#{term_id}")

  data = {} unless data.is_a?(Hash) # OSM/OGM returns an empty array to represent no badges
  pending_raw = data['pending'] || {}
  descriptions_raw = data['description'] || {}

  by_member = {}
  member_names = {}
  badge_names = {}
  pending_raw.each do |key, members|
    members.each do |member|
      id = Osm.to_i_or_nil(member['scoutid'])
      description = descriptions_raw[key]['name'] + (descriptions_raw[key]['section'].eql?('staged') ? " (Level #{member['level']})" : '')
      description_key = key + (descriptions_raw[key]['section'].eql?('staged') ? "_#{member['level']}" : '_1')
      badge_names[description_key] = description
      by_member[id] ||= []
      by_member[id].push(description_key)
      member_names[id] = "#{member['firstname']} #{member['lastname']}"
    end
  end

  due_badges = Osm::Badges::DueBadges.new(
    :by_member => by_member,
    :member_names => member_names,
    :badge_names => badge_names,
  )
  Osm::Model.cache_write(api, cache_key, due_badges)
  return due_badges
end

.get_stock(api, section, options = {}) ⇒ Object

Get badge stock levels for a section

Options Hash (options):

  • :no_cache (Boolean) — default: optional

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/osm/badges.rb', line 10

def self.get_stock(api, section, options={})
  Osm::Model.require_ability_to(api, :read, :badge, section, options)
  section = Osm::Section.get(api, section, options) unless section.is_a?(Osm::Section)
  term_id = Osm::Term.get_current_term_for_section(api, section).id
  cache_key = ['badge_stock', section.id]

  if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key)
    return Osm::Model.cache_read(api, cache_key)
  end

  data = api.perform_query("challenges.php?action=getInitialBadges&type=core&sectionid=#{section.id}&section=#{section.type}&termid=#{term_id}")
  data = (data['stock'] || {}).select{ |k,v| !k.eql?('sectionid') }.
                               inject({}){ |new_hash,(badge, level)| new_hash[badge] = level.to_i; new_hash }

  Osm::Model.cache_write(api, cache_key, data)
  return data
end

.update_stock(api, section, badge_key, stock_level) ⇒ Boolan

Update badge stock levels



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/osm/badges.rb', line 34

def self.update_stock(api, section, badge_key, stock_level)
  Osm::Model.require_ability_to(api, :write, :badge, section)
  section = Osm::Section.get(api, section) unless section.is_a?(Osm::Section)

  Osm::Model.cache_delete(api, ['badge_stock', section.id])

  data = api.perform_query("challenges.php?action=updateStock", {
    'stock' => stock_level,
    'table' => badge_key,
    'sectionid' => section.id,
    'section' => section.type,
  })
  return data.is_a?(Hash) && (data['sectionid'].to_i == section.id) && (data[badge_key.to_s].to_i == stock_level)
end