Class: Osm::Badge

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

Direct Known Subclasses

ActivityBadge, ChallengeBadge, CoreBadge, StagedBadge

Defined Under Namespace

Classes: Data, Requirement

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 Badge

Parameters:

  • attributes (Hash)

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



# File 'lib/osm/badge.rb', line 46

Instance Attribute Details

#nameString

Returns the name of the badge.

Returns:

  • (String)

    the name of the badge



23
# File 'lib/osm/badge.rb', line 23

attribute :name, :type => String

#needed_from_sectionHash

Returns:

  • (Hash)


23
# File 'lib/osm/badge.rb', line 23

attribute :name, :type => String

#osm_keyString

Returns the key for the badge in OSM.

Returns:

  • (String)

    the key for the badge in OSM



23
# File 'lib/osm/badge.rb', line 23

attribute :name, :type => String

#osm_long_keyString

Returns the long key for the badge in osm (used for getting stock).

Returns:

  • (String)

    the long key for the badge in osm (used for getting stock)



23
# File 'lib/osm/badge.rb', line 23

attribute :name, :type => String

#requirement_notesString

Returns a description of the badge.

Returns:

  • (String)

    a description of the badge



23
# File 'lib/osm/badge.rb', line 23

attribute :name, :type => String

#requirementsArray<Osm::Badge::Requirement>

Returns:



23
# File 'lib/osm/badge.rb', line 23

attribute :name, :type => String

#sections_neededFixnum

Returns:

  • (Fixnum)


23
# File 'lib/osm/badge.rb', line 23

attribute :name, :type => String

#total_neededFixnum

Returns:

  • (Fixnum)


23
# File 'lib/osm/badge.rb', line 23

attribute :name, :type => String

Class Method Details

.get_badges_for_section(api, section, section_type = nil, options = {}) ⇒ Array<Osm::Badge>

Get 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

  • section_type (Symbol) (defaults to: nil)

    The type of section to get badges for (if nil uses the type of the section param)

Returns:

Raises:



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
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/osm/badge.rb', line 57

def self.get_badges_for_section(api, section, section_type=nil, options={})
  raise Error, 'This method must be called on one of the subclasses (CoreBadge, ChallengeBadge, StagedBadge or ActivityBadge)' if type.nil?
  require_ability_to(api, :read, :badge, section, options)
  section = Osm::Section.get(api, section, options) unless section.is_a?(Osm::Section)
  section_type ||= section.type
  cache_key = ['badges', section_type, type]

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

  term_id = Osm::Term.get_current_term_for_section(api, section, options).to_i
  badges = []

  data = api.perform_query("challenges.php?action=getInitialBadges&type=#{type}&sectionid=#{section.id}&section=#{section_type}&termid=#{term_id}")
  badge_order = data["badgeOrder"].to_s.split(',')
  structures = data["structure"] || {}
  details = data["details"] || {}
  badge_order.each do |b|
    structure = structures[b]
    detail = details[b]
    config = ActiveSupport::JSON.decode(detail['config'] || '{}')

    badge = new(
      :name => detail['name'],
      :requirement_notes => detail['description'],
      :osm_key => detail['shortname'],
      :osm_long_key => detail['table'],
      :sections_needed => config['sectionsneeded'].to_i,
      :total_needed => config['totalneeded'].to_i,
      :needed_from_section => (config['sections'] || {}).inject({}) { |h,(k,v)| h[k] = v.to_i; h },
    )

    requirements = []
    ((structure[1] || {})['rows'] || []).each do |r|
      requirements.push Osm::Badge::Requirement.new(
        :badge => badge,
        :name => r['name'],
        :description => r['tooltip'],
        :field => r['field'],
        :editable => r['editable'].eql?('true'),
      )
    end
    badge.requirements = requirements

    badges.push badge
  end

  cache_write(api, cache_key, badges)
  return badges
end

.get_summary_for_section(api, section, term = nil, options = {}) ⇒ Array<Hash>

Get a summary of badges earnt by members

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

Returns:

  • (Array<Hash>)

Raises:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/osm/badge.rb', line 115

def self.get_summary_for_section(api, section, term=nil, options={})
  raise Error, 'This method must be called on one of the subclasses (CoreBadge, ChallengeBadge, StagedBadge or ActivityBadge)' if type.nil?
  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 = ['badge-summary', section.id, term_id, type]

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

  summary = []
  data = api.perform_query("challenges.php?action=summary&section=#{section.type}&sectionid=#{section.id}&termid=#{term_id}&type=#{type}")
  data['items'].each do |item|
    new_item = {
      :first_name => item['firstname'],
      :last_name => item['lastname'],
    }
    (item.keys - ['firstname', 'lastname']).each do |key|
      new_item[key] = item[key]
    end
    summary.push new_item
  end

  cache_write(api, cache_key, summary)
  return summary
end

.typeObject



188
189
190
# File 'lib/osm/badge.rb', line 188

def self.type
  nil
end

Instance Method Details

#<=>(another) ⇒ Object

Compare Badge based on name then osm_key



181
182
183
184
185
# File 'lib/osm/badge.rb', line 181

def <=>(another)
  result = self.name <=> another.try(:name)
  result = self.osm_key <=> another.try(:osm_key) if result == 0
  return result
end

#get_data_for_section(api, section, term = nil, options = {}) ⇒ Array<Osm::Badge::Data>

Get a list of badge requirements met by members

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

Returns:

Raises:



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/osm/badge.rb', line 149

def get_data_for_section(api, section, term=nil, options={})
  raise Error, 'This method must be called on one of the subclasses (CoreBadge, ChallengeBadge, StagedBadge or ActivityBadge)' if type.nil?
  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 = ['badge_data', section.id, term_id, osm_key]

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

  datas = []
  data = api.perform_query("challenges.php?termid=#{term_id}&type=#{type}&section=#{section.type}&c=#{osm_key}&sectionid=#{section.id}")
  data['items'].each do |d|
    datas.push Osm::Badge::Data.new(
      :member_id => d['scoutid'],
      :first_name => d['firstname'],
      :last_name => d['lastname'],
      :completed => d['completed'].to_i,
      :awarded => d['awarded'].to_i,
      :awarded_date => Osm.parse_date(d['awardeddate']),
      :requirements => d.select{ |k,v| k.include?('_') },
      :section_id => section.id,
      :badge => self,
    )
  end

  cache_write(api, cache_key, datas)
  return datas
end

#typeObject



191
192
193
# File 'lib/osm/badge.rb', line 191

def type
  self.class.type
end