Class: Osm::Sms

Inherits:
Object
  • Object
show all
Defined in:
lib/osm/sms.rb

Defined Under Namespace

Classes: DeliveryReport

Class Method Summary collapse

Class Method Details

.send_sms(api, section, members, mobile_numbers, source_address, message) ⇒ Hash

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 send the message to

  • members (Array<Osm::Member, Fixnum, #to_i>, Osm::Member, Fixnum, #to_i)

    The members (or their IDs) to send the message to

  • mobile_numbers (Symbol, String)

    Wheather to send the message to all numbers for a member (:all) or just the first mobile one (:first) or a selection (e.g. “12”, “24” etc.)

  • source_address (String, #to_s)

    The number to claim the message is from

  • message (String, #to_s)

    The text of the message to send

Returns:

  • (Hash)

    with keys :sent (Fixnum), :result (Boolean) and :message (String)

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/osm/sms.rb', line 13

def self.send_sms(api, section, members, mobile_numbers, source_address, message)
  raise ArgumentError, 'mobile_numbers must be either :all, :first or a String containing numbers 1-4' unless ([:all, :first, :one].include?(mobile_numbers) || mobile_numbers.match(/[1234]{1,4}/))
  Osm::Model.require_access_to_section(api, section)      
  mobile_numbers = :one if mobile_numbers.eql?(:first)

  data = api.perform_query("sms.php?action=sendText&sectionid=#{section.to_i}", {
    'msg' => message,
    'scouts' => [*members].join(','),
    'source' => source_address,
    'type' => mobile_numbers,
    'scheduled' => 'now',
  })

  data.select!{ |k,v| !['debug', 'config'].include?(k) }
  data = data.map do |k,v|
    k = 'message' if k.eql?('msg')
    k = 'sent' if k.eql?('sent_to')
    [k.to_sym, v]
  end
  return Hash[*data.flatten]
end