Class: Osm::Badge
Direct Known Subclasses
Defined Under Namespace
Classes: Data, Requirement
Instance Attribute Summary collapse
-
#name ⇒ String
The name of the badge.
- #needed_from_section ⇒ Hash
-
#osm_key ⇒ String
The key for the badge in OSM.
-
#osm_long_key ⇒ String
The long key for the badge in osm (used for getting stock).
-
#requirement_notes ⇒ String
A description of the badge.
- #requirements ⇒ Array<Osm::Badge::Requirement>
- #sections_needed ⇒ Fixnum
- #total_needed ⇒ Fixnum
Class Method Summary collapse
-
.get_badges_for_section(api, section, section_type = nil, options = {}) ⇒ Array<Osm::Badge>
Get badges.
-
.get_summary_for_section(api, section, term = nil, options = {}) ⇒ Array<Hash>
Get a summary of badges earnt by members.
- .type ⇒ Object
Instance Method Summary collapse
-
#<=>(another) ⇒ Object
Compare Badge based on name then osm_key.
-
#get_data_for_section(api, section, term = nil, options = {}) ⇒ Array<Osm::Badge::Data>
Get a list of badge requirements met by members.
-
#initialize ⇒ Object
constructor
Initialize a new Badge.
- #type ⇒ Object
Methods inherited from Model
#<, #<=, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i
Constructor Details
#initialize ⇒ Object
Initialize a new Badge
|
# File 'lib/osm/badge.rb', line 46
|
Instance Attribute Details
#name ⇒ String
Returns the name of the badge.
23 |
# File 'lib/osm/badge.rb', line 23 attribute :name, :type => String |
#needed_from_section ⇒ Hash
23 |
# File 'lib/osm/badge.rb', line 23 attribute :name, :type => String |
#osm_key ⇒ String
Returns the key for the badge in OSM.
23 |
# File 'lib/osm/badge.rb', line 23 attribute :name, :type => String |
#osm_long_key ⇒ String
Returns 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_notes ⇒ String
Returns a description of the badge.
23 |
# File 'lib/osm/badge.rb', line 23 attribute :name, :type => String |
#requirements ⇒ Array<Osm::Badge::Requirement>
23 |
# File 'lib/osm/badge.rb', line 23 attribute :name, :type => String |
#sections_needed ⇒ Fixnum
23 |
# File 'lib/osm/badge.rb', line 23 attribute :name, :type => String |
#total_needed ⇒ 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
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, ={}) 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, ) section = Osm::Section.get(api, section, ) unless section.is_a?(Osm::Section) section_type ||= section.type cache_key = ['badges', section_type, type] if ![: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, ).to_i badges = [] data = api.perform_query("challenges.php?action=getInitialBadges&type=#{type}§ionid=#{section.id}§ion=#{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
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, ={}) 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, ) section = Osm::Section.get(api, section, ) unless section.is_a?(Osm::Section) term_id = (term.nil? ? Osm::Term.get_current_term_for_section(api, section, ) : term).to_i cache_key = ['badge-summary', section.id, term_id, type] if ![: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§ion=#{section.type}§ionid=#{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 |
.type ⇒ Object
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
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, ={}) 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, ) section = Osm::Section.get(api, section, ) unless section.is_a?(Osm::Section) term_id = (term.nil? ? Osm::Term.get_current_term_for_section(api, section, ) : term).to_i cache_key = ['badge_data', section.id, term_id, osm_key] if ![: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}§ion=#{section.type}&c=#{osm_key}§ionid=#{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 |
#type ⇒ Object
191 192 193 |
# File 'lib/osm/badge.rb', line 191 def type self.class.type end |