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, all_or_one, 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

  • all_or_one (Symbol)

    Wheather to send the message to all numbers for a member (:all) or just the first mobile one (:one)

  • 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
# File 'lib/osm/sms.rb', line 13

def self.send_sms(api, section, members, all_or_one, source_address, message)
  raise ArgumentError, 'all_or_one must be either :all or :one' unless [:all, :one].include?(all_or_one)
  Osm::Model.require_access_to_section(api, section)

  data = api.perform_query("sms.php?action=sendText&sectionid=#{section.to_i}", {
    'msg' => message,
    'scouts' => [*members].join(','),
    'source' => source_address,
    'all' => all_or_one,
    '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