Method: Osm::Meeting.get_for_section

Defined in:
lib/osm/meeting.rb

.get_for_section(api, section, term = nil, options = {}) ⇒ Array<Osm::Meeting>

Get the programme for a given term

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 programme for

  • term (Osm::term, Fixnum, nil) (defaults to: nil)

    The term (or its ID) to get the programme 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:



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
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
135
136
137
138
139
140
141
# File 'lib/osm/meeting.rb', line 72

def self.get_for_section(api, section, term=nil, options={})
  require_ability_to(api, :read, :programme, section, options)
  section_id = section.to_i
  term_id = term.nil? ? Osm::Term.get_current_term_for_section(api, section).id : term.to_i
  cache_key = ['programme', section_id, term_id]

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

  data = api.perform_query("programme.php?action=getProgramme&sectionid=#{section_id}&termid=#{term_id}")

  result = Array.new
  data = {'items'=>[],'activities'=>{}} if data.is_a? Array
  items = data['items'] || []
  activities = data['activities'] || {}
  badge_links = data['badgelinks'] || {}

  items.each do |item|
    attributes = {}
    attributes[:id] = Osm::to_i_or_nil(item['eveningid'])
    attributes[:section_id] = Osm::to_i_or_nil(item['sectionid'])
    attributes[:title] = item['title'] || 'Unnamed meeting'
    attributes[:notes_for_parents] = item['notesforparents'] || ''
    attributes[:games] = item['games'] || ''
    attributes[:pre_notes] = item['prenotes'] || ''
    attributes[:post_notes] = item['postnotes'] || ''
    attributes[:leaders] = item['leaders'] || ''
    attributes[:start_time] = item['starttime'].nil? ? nil : item['starttime'][0..4]
    attributes[:finish_time] = item['endtime'].nil? ? nil : item['endtime'][0..4]
    attributes[:date] = Osm::parse_date(item['meetingdate'])

    our_activities = activities[item['eveningid']]
    attributes[:activities] = Array.new
    unless our_activities.nil?
      our_activities.each do |activity_data|
        if activity_data.is_a?(Array)
          activity_data = activity_data.find{ |a| a.is_a?(Hash) && a.has_key?('activityid') }
        end
        attributes[:activities].push Osm::Meeting::Activity.new(
          :activity_id => Osm::to_i_or_nil(activity_data['activityid']),
          :title => activity_data['title'],
          :notes => activity_data['notes'],
        )
      end
    end

    our_badge_links = badge_links[item['eveningid']]
    attributes[:badge_links] = Array.new
    unless our_badge_links.nil?
      our_badge_links.each do |badge_data|
        attributes[:badge_links].push Osm::Meeting::BadgeLink.new(
          :badge_type => badge_data['badgetype'].to_sym,
          :badge_section => badge_data['section'].to_sym,
          :badge_name => badge_data['badgeLongName'],
          :badge_id => Osm::to_i_or_nil(badge_data['badge_id']),
          :badge_version => Osm::to_i_or_nil(badge_data['badge_version']),
          :requirement_id => Osm::to_i_or_nil(badge_data['column_id']),
          :requirement_label => badge_data['columnnameLongName'],
          :data => badge_data['data'],
        )
      end
    end

    result.push new(attributes)
  end

  cache_write(api, cache_key, result)
  return result
end