Class: Apparat::MessagingClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={} )
	@key        = key
	@wsdl       = options[:wsdl]       || "http://sim.apparat.no/api/messaging/v1/wsdl"
	@product_id = options[:product_id]
	@sender     = options[:sender]
	@driver     = SOAP::WSDLDriverFactory.new( @wsdl ).create_rpc_driver 
end

Instance Attribute Details

#product_idObject

Returns the value of attribute product_id.



5
6
7
# File 'lib/apparat.rb', line 5

def product_id
  @product_id
end

#senderObject

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, options={} )
	options[:sender]     ||= @sender
	options[:product_id] ||= @product_id
	recipient = cleanup_msisdn( recipient )
	
	raise "Cannot send SMS without a product_id" unless options[:product_id]
	raise "Cannot send SMS, no license key set"  unless @key
	raise "Cannot send SMS without a sender"     unless options[:sender]

	return nil unless valid_msisdn?( recipient )
	uuid = @driver.SendSms( recipient, body, options[:sender], @key, options[: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, options={} )
	recipients = [ recipients ].flatten
	uuids = recipients.collect { |r| send( r, body, options ) }
end

#valid_msisdn?(msisdn) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/apparat.rb', line 34

def valid_msisdn?( msisdn )
	( msisdn.match( /^47[\d]{8}$/ ) ) ? true : false 
end