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.
-
#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 40
|
Instance Attribute Details
#name ⇒ String
Returns the name of the badge.
21 |
# File 'lib/osm/badge.rb', line 21 attribute :name, :type => String |
#needed_from_section ⇒ Hash
21 |
# File 'lib/osm/badge.rb', line 21 attribute :name, :type => String |
#osm_key ⇒ String
Returns the key for the badge in OSM.
21 |
# File 'lib/osm/badge.rb', line 21 attribute :name, :type => String |
#requirement_notes ⇒ String
Returns a description of the badge.
21 |
# File 'lib/osm/badge.rb', line 21 attribute :name, :type => String |
#requirements ⇒ Array<Osm::Badge::Requirement>
21 |
# File 'lib/osm/badge.rb', line 21 attribute :name, :type => String |
#sections_needed ⇒ Fixnum
21 |
# File 'lib/osm/badge.rb', line 21 attribute :name, :type => String |
#total_needed ⇒ Fixnum
21 |
# File 'lib/osm/badge.rb', line 21 attribute :name, :type => String |
Class Method Details
.get_badges_for_section(api, section, section_type = nil, options = {}) ⇒ Array<Osm::Badge>
Get badges
51 52 53 54 55 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 95 96 97 98 99 100 |
# File 'lib/osm/badge.rb', line 51 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'], :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
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/osm/badge.rb', line 108 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
181 182 183 |
# File 'lib/osm/badge.rb', line 181 def self.type nil end |
Instance Method Details
#<=>(another) ⇒ Object
Compare Badge based on name then osm_key
174 175 176 177 178 |
# File 'lib/osm/badge.rb', line 174 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
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/osm/badge.rb', line 142 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
184 185 186 |
# File 'lib/osm/badge.rb', line 184 def type self.class.type end |