Method: Osm::Sms::DeliveryReport.get_for_section

Defined in:
lib/osm/sms.rb

.get_for_section(api, section, options = {}) ⇒ Array<Osm::Sms::DeliveryReport>

Get delivery reports

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

Returns:



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
172
173
174
175
176
177
178
# File 'lib/osm/sms.rb', line 144

def self.get_for_section(api, section, options={})
  require_access_to_section(api, section, options)
  section_id = section.to_i
  cache_key = ['sms_delivery_reports', section_id]

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

  reports = []
  get_name_number_regex = /\A(?<name>.*\w)\W+(?<number>\d*)\Z/
  data = api.perform_query("sms.php?action=deliveryReports&sectionid=#{section_id}&dateFormat=generic")
  data['items'].each do |report|
    from = report['from'].match(get_name_number_regex)
    to = report['to'].match(get_name_number_regex)
    reports.push new(
      :sms_id => Osm.to_i_or_nil(report['smsid']),
      :user_id => Osm.to_i_or_nil(report['userid']),
      :member_id => Osm.to_i_or_nil(report['scoutid']),
      :section_id => Osm.to_i_or_nil(report['sectionid']),
      :from_name => from[:name],
      :from_number => "+#{from[:number]}",
      :to_name => to[:name],
      :to_number => "+#{to[:number]}",
      :message => report['message'],
      :scheduled => Osm.parse_datetime(report['schedule']),
      :last_updated => Osm.parse_datetime(report['lastupdated']),
      :credits => Osm.to_i_or_nil(report['credits']),
      :status => (report['status'] || 'error').downcase.to_sym,
    )
  end

  cache_write(api, cache_key, reports)
  return reports
end