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:


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
95
96
97
98
99
100
# File 'lib/osm/badges.rb', line 60

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("ext/badges/due/?action=get&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 = data['pending'] || {}

  by_member = {}
  member_names = {}
  badge_names = {}
  badge_stock = {}

  pending.each do |badge_identifier, members|
    members.each do |member|
      badge_level_identifier = badge_identifier + "_#{member['completed']}"
      member_id = Osm.to_i_or_nil(member['scout_id'])
      badge_names[badge_level_identifier] = "#{member['label']} - #{member['name']}" + (!member['extra'].nil? ? " (#{member['extra']})" : '')
      badge_stock[badge_level_identifier] = member['current_stock'].to_i
      by_member[member_id] ||= []
      by_member[member_id].push(badge_level_identifier)
      member_names[member_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,
    :badge_stock => badge_stock,
  )
  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
27
# 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("ext/badges/stock/?action=getBadgeStock&section=#{section.type}&section_id=#{section.id}&term_id=#{term_id}")
  data = (data['items'] || [])
  data.map!{ |i| [i['badge_id_level'], i['stock']] }
  data = Hash[data]

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

.update_stock(api, section, badge_id, badge_level = 1, 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_id (Fixnum, #to_i)

    The badge to set the stock level for

  • badge_level (Fixnum, #to_i) (defaults to: 1)

    The level of a staged badge to set the stock for (default 1)

  • stock_level (Fixnum, #to_i)

    How many of the provided badge there are

Returns:

  • (Boolan)

    whether the update was successfull or not


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/osm/badges.rb', line 36

def self.update_stock(api, section, badge_id, badge_level=1, 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("ext/badges.php?action=updateStock", {
    'stock' => stock_level,
    'sectionid' => section.id,
    'section' => section.type,
    'type' => 'current',
    'level' => badge_level.to_i,
    'badge_id' => badge_id.to_i,
  })
  return data.is_a?(Hash) && data['ok']
end