class Osm::Sms

Public Class Methods

number_selected(api, section, members) click to toggle source

Get the number of SMS credits which will be used sending a message to the passed members @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 send the message to @param [Array<Osm::Member, Fixnum, to_i>, Osm::Member, Fixnum, to_i] members The members (or their IDs) to send the message to @return [Fixnum] the number of SMS credits which will be used

# File lib/osm/sms.rb, line 59
def self.number_selected(api, section, members)
  Osm::Model.require_access_to_section(api, section) 

  members = [*members]
  data = api.perform_query("ext/members/sms/?action=getNumbers&sectionid=#{section.to_i}&type=", {
    'scouts' => [*members.map{ |m| m.to_i }].join(',')
  })

  Osm::Model.cache_write(api, ['sms_credits', section.to_i], data['sms_remaining'])
  return data['numbers']
end
remaining_credits(api, section, options={}) click to toggle source

Get the number of remaining SMS credits for a section @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 send the message to @!macro options_get @return [Fixnum] the number of remaining SMS credits for the section

# File lib/osm/sms.rb, line 37
def self.remaining_credits(api, section, options={})
  Osm::Model.require_access_to_section(api, section) 
  cache_key = ['sms_credits', section.to_i]

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

  data = api.perform_query("ext/members/sms/?action=getNumbers&sectionid=#{section.to_i}&type=", {
    'scouts' => '0'
  })
  data = data['sms_remaining']

  Osm::Model.cache_write(api, cache_key, data)
  return data
end
send_sms(api, section, members, source_address, message) click to toggle source

Send an SMS to some members on their enabled numbers @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 send the message to @param [Array<Osm::Member, Fixnum, to_i>, Osm::Member, Fixnum, to_i] members The members (or their IDs) to send the message to @param [String, to_s] source_address The number to claim the message is from @param [String, to_s] message The text of the message to send @return [Boolean] whether the messages were sent @raise [Osm::Error] If the section doesn't have enough credits to send the message

# File lib/osm/sms.rb, line 13
def self.send_sms(api, section, members, source_address, message)
  Osm::Model.require_access_to_section(api, section)
  raise Osm::Error, 'You do not have enough credits to send that message.' if number_selected(api, section, members) > remaining_credits(api, section) 

  members = [*members]
  data = api.perform_query("ext/members/sms/?action=sendText&sectionid=#{section.to_i}", {
    'msg' => message,
    'scouts' => [*members.map{ |m| m.to_i }].join(','),
    'source' => source_address,
    'type' => '',
  })

  if data.is_a?(Hash) && data['result']
    Osm::Model.cache_write(api, ['sms_credits', section.to_i], data['msg'].match(/\A[^\d]*(\d+)[^\d]*\Z/)[1])
    return true
  end
  return false
end