class Osm::Sms::DeliveryReport

Constants

VALID_STATUSES

Public Class Methods

get_for_section(api, section, options={}) click to toggle source

Get delivery reports @param [Osm::Api] api The api to use to make the request @param [Osm::Section, Fixnum, to_i] section The section (or its ID) to get the reports for @!macro options_get @return [Array<Osm::Sms::DeliveryReport>]

# 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