Class: Apparat::MessagingClient
- Inherits:
-
Object
- Object
- Apparat::MessagingClient
- Defined in:
- lib/apparat.rb
Instance Attribute Summary collapse
-
#product_id ⇒ Object
Returns the value of attribute product_id.
-
#sender ⇒ Object
Returns the value of attribute sender.
Instance Method Summary collapse
- #cleanup_msisdn(msisdn) ⇒ Object
-
#initialize(key, options = {}) ⇒ MessagingClient
constructor
create a new client object.
-
#send(recipient, body, options = {}) ⇒ Object
send a message.
-
#send_many(recipients, body, options = {}) ⇒ Object
same as
MessagingClient.send, except that it operates on an array of recipients and returns an array of uuids. - #valid_msisdn?(msisdn) ⇒ Boolean
Constructor Details
#initialize(key, options = {}) ⇒ MessagingClient
create a new client object.
example:
client = Apparat::MessagingClient.new( "my_key" )
client.sender = "myName"
client.product_id = 10
attributes can also be set in the constructor call:
client = Apparat::MessagingClient.new( "my_key", :product_id => 10, :sender => "myName" )
19 20 21 22 23 24 25 |
# File 'lib/apparat.rb', line 19 def initialize( key, ={} ) @key = key @wsdl = [:wsdl] || "http://sim.apparat.no/api/messaging/v1/wsdl" @product_id = [:product_id] @sender = [:sender] @driver = SOAP::WSDLDriverFactory.new( @wsdl ).create_rpc_driver end |
Instance Attribute Details
#product_id ⇒ Object
Returns the value of attribute product_id.
5 6 7 |
# File 'lib/apparat.rb', line 5 def product_id @product_id end |
#sender ⇒ Object
Returns the value of attribute sender.
5 6 7 |
# File 'lib/apparat.rb', line 5 def sender @sender end |
Instance Method Details
#cleanup_msisdn(msisdn) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/apparat.rb', line 27 def cleanup_msisdn( msisdn ) msisdn.gsub!( /[^\d]/, '' ) if msisdn.length == 8 msisdn = "47"+msisdn end end |
#send(recipient, body, options = {}) ⇒ Object
send a message.
examples:
msisdn = "4712345678"
client.send( msisdn, "message" )
client.send( msisdn, "message", :product_id => 15 )
client.send( msisdn, "message", :sender => "myOtherName" )
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/apparat.rb', line 47 def send( recipient, body, ={} ) [:sender] ||= @sender [:product_id] ||= @product_id recipient = cleanup_msisdn( recipient ) raise "Cannot send SMS without a product_id" unless [:product_id] raise "Cannot send SMS, no license key set" unless @key raise "Cannot send SMS without a sender" unless [:sender] return nil unless valid_msisdn?( recipient ) uuid = @driver.SendSms( recipient, body, [:sender], @key, [:product_id], "utf-8" ) rescue nil end |
#send_many(recipients, body, options = {}) ⇒ Object
same as MessagingClient.send, except that it operates on an array of recipients and returns an array of uuids.
62 63 64 65 |
# File 'lib/apparat.rb', line 62 def send_many( recipients, body, ={} ) recipients = [ recipients ].flatten uuids = recipients.collect { |r| send( r, body, ) } end |
#valid_msisdn?(msisdn) ⇒ Boolean
34 35 36 |
# File 'lib/apparat.rb', line 34 def valid_msisdn?( msisdn ) ( msisdn.match( /^47[\d]{8}$/ ) ) ? true : false end |