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

.number_selected(api, section, members) ⇒ Fixnum

Get the number of SMS credits which will be used sending a message to the passed members

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

Returns:

  • (Fixnum)

    the number of SMS credits which will be used



59
60
61
62
63
64
65
66
67
68
69
# 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 = {}) ⇒ Fixnum

Get the number of remaining SMS credits for a section

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

Returns:

  • (Fixnum)

    the number of remaining SMS credits for the section



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Boolean

Send an SMS to some members on their enabled numbers

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

  • 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:

  • (Boolean)

    whether the messages were sent

Raises:

  • (Osm::Error)

    If the section doesn’t have enough credits to send the message



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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