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

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section (Osm::Section, Fixnum, #to_i)

    The section (or its ID) to get the due badges for

  • term (Osm::Term, Fixnum, #to_i, nil) (defaults to: nil)

    The term (or its ID) to get the due badges for, passing nil causes the current term to be used

  • 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:



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

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section (Osm::Section, Fixnum, #to_i)

    The section (or its ID) to get the badge stock for

  • 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:

  • Hash



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

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section (Osm::Section, Fixnum, #to_i)

    The section (or its ID) to update ther badge stock for

  • badge_key (Sring, #to_s)

    The badge to set the stock level for

  • stock_level (Fixnum, #to_i)

    How many of the provided badge there are

Returns:

  • (Boolan)

    whether the update was successfull or not



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