Skip to content

Commit

Permalink
Added timed message functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Maran Hidskes committed Jan 6, 2009
1 parent 30f785b commit 1890870
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 42 deletions.
22 changes: 20 additions & 2 deletions README
@@ -1,7 +1,7 @@
# == Mollie SMS ==
#
# author: Tom-Eric Gerritsen <tomeric@i76.nl>
#
#
# A library for sending SMSes using the Mollie.nl SMS gateway.
#
# The SMS class allows you to send multiple sms messages using a single
Expand All @@ -20,4 +20,22 @@
# # sending a message to multiple receivers
# sms = Mollie::SMS.new('username', 'password')
# sms.originator = '0612345678'
# sms.send(['0687654321', '0612435687'], 'Hello, these are SMSes!')
# sms.send(['0687654321', '0612435687'], 'Hello, these are SMSes!')
#
# # sending a timed message
# sms = Mollie::SMS.new('username', 'password')
# sms.orginator = '0612345678'
# sms.send("0687654321", "This is a message from the past!", {:delivery_date => "20090202120200", :reference => "scarymessage"})
#
# # cancelling a message
# sms = Mollie::SMS.new('username', 'password')
# sms.orginator = '0612345678'
# sms.cancel("scarymessage")

# Install Install
# cd vendor/plugins
# git clone git://github.com/i76/mollie.git
# cd mollie
# git checkout
# cd ..
# rm -rf mollie/.git
41 changes: 41 additions & 0 deletions README.rdoc
@@ -0,0 +1,41 @@
# == Mollie SMS ==
#
# author: Tom-Eric Gerritsen <tomeric@i76.nl>
#
# A library for sending SMSes using the Mollie.nl SMS gateway.
#
# The SMS class allows you to send multiple sms messages using a single
# configuration. As an alternative, you can specify all settings as a hash
# when initializing an object, or set the different settings after an object
# is initialized using the setter methods.
#
# == Usage
# require 'mollie'
#
# # sending a message to one receiver
# sms = Mollie::SMS.new('username', 'password')
# sms.originator = '0612345678'
# sms.send('0687654321', 'Hello, this is an SMS!')
#
# # sending a message to multiple receivers
# sms = Mollie::SMS.new('username', 'password')
# sms.originator = '0612345678'
# sms.send(['0687654321', '0612435687'], 'Hello, these are SMSes!')
#
# # sending a timed message
# sms = Mollie::SMS.new('username', 'password')
# sms.orginator = '0612345678'
# sms.send("0687654321", "This is a message from the past!", {:delivery_date => "20090202120200", :reference => "scarymessage"})
#
# # cancelling a message
# sms = Mollie::SMS.new('username', 'password')
# sms.orginator = '0612345678'
# sms.cancel("scarymessage")

# Install Install
# cd vendor/plugins
# git clone git://github.com/i76/mollie.git
# cd mollie
# git checkout
# cd ..
# rm -rf mollie/.git
154 changes: 114 additions & 40 deletions lib/mollie.rb
@@ -1,5 +1,6 @@
# mollie.rb: Library for sending SMS using the mollie.nl API
# author: Tom-Eric Gerritsen <tomeric@i76.nl>
# additional functions: Maran Hidskes <maran@noxa.nl>

require 'rubygems'
require 'uri'
Expand All @@ -8,7 +9,7 @@

module Mollie
##
# == SMS ==
# == SMS
#
# The SMS class allows you to send multiple sms messages using a single
# configuration. As an alternative, you can specify all settings as a hash
Expand All @@ -27,6 +28,17 @@ module Mollie
# sms = Mollie::SMS.new('username', 'password')
# sms.originator = '0612345678'
# sms.send(['0687654321', '0612435687'], 'Hello, these are SMSes!')
#
# # sending a timed message
# sms = Mollie::SMS.new('username', 'password')
# sms.orginator = '0612345678'
# sms.send("0687654321", "This is a message from the past!", {:delivery_date => "20090202120200", :reference => "scarymessage"})
#
# # cancelling a message
# sms = Mollie::SMS.new('username', 'password')
# sms.orginator = '0612345678'
# sms.cancel("scarymessage")

class SMS
DEFAULT_GATEWAY = "http://www.mollie.nl/xml/sms/"

Expand All @@ -42,43 +54,82 @@ def initialize(username, password, hash = {})
self.originator = hash[:originator] if hash[:originator]
self.gateway = hash[:gateway] if hash[:gateway]
end

def send(recipients, message)
uri = prepare_uri(recipients, message)
res = Net::HTTP.get_response(uri)

if res.code.to_i == 200

# Send a message through mollie. Takes two obligatory arguments and one optional options hash.
# The options hash can include a reference and delivery_date for timed messages
def send(recipients, message, options = {})
unless options[:delivery_date].blank?
raise DeliveryDateButNoReferenceException if options.include?(:delivery_date) && !options.include?(:reference)
raise WrongDateFormatException if options[:delivery_date].size < 14 || (options[:delivery_date].match(/\D+/) != nil)
end

uri = prepare_send_uri(recipients, message, options)
res = Net::HTTP.get_response(uri)
parse_response_code(res)
end

# Cancels a timed message through the reference
def cancel(reference)
raise NoReferenceException if reference.blank?
self.gateway = "http://www.mollie.nl/xml/sms_cancel/"
arguments = {
:username => self.username,
:password => self.password,
:reference => reference
}

uri = parse_uri(arguments)
res = Net::HTTP.get_response(uri)
parse_response_code(res, false)
end


private

def parse_response_code(res, send = true)
if res.code.to_i == 200
doc = Hpricot(res.body)

resultcode = (doc/"resultcode").inner_html.to_i

if resultcode == 10
return true
else
raise MollieException.by_code(resultcode)

if send == true
raise MollieException.by_cancel_code(resultcode)
else
raise MollieException.by_send_code(resultcode)
end
return false
end
else
raise MollieException

return false
end
end

private
def prepare_uri(recipients, message)
end

def prepare_send_uri(recipients, message, options = {})
recipients = [recipients] unless recipients.is_a?(Array)

arguments = {
:recipients => recipients.join(','),
:username => self.username,
:password => self.password,
:originator => self.originator,
:message => message
:message => message
}

query = arguments.map do |key, value|

if options.include?(:delivery_date)
arguments[:deliverydate] = options[:delivery_date]
arguments[:reference] = options[:reference]
end

return parse_uri(arguments)

end

def parse_uri(arguments)
query = arguments.map do |key, value|
URI.encode(key.to_s) + "=" + URI.encode(value.to_s) if value
end

Expand All @@ -87,19 +138,33 @@ def prepare_uri(recipients, message)
uri = URI.parse(self.gateway || DEFAULT_GATEWAY)
uri.query = query.join('&')

uri
end
uri
end
end


class MollieException < Exception
@@resultcode = -1
attr_reader :resultcode

@resultcode = -10

def resultcode
@@resultcode
end

class << self
def by_code(code)
def by_cancel_code(code)
case code.to_i
when 20
NoUserNameException
when 21
NoPasswordException
when 22
NoReferenceException
when 30
AuthenticationException
when 40
ReferencedMessageNotFound
end
end

def by_send_code(code)
case code.to_i
when 20
NoUserNameException
Expand Down Expand Up @@ -132,17 +197,26 @@ def by_code(code)
end
end

class NoUserNameException < MollieException; @@resultcode = 20; end
class NoPasswordException < MollieException; @@resultcode = 21; end
class InvalidOriginatorException < MollieException; @@resultcode = 22; end
class RecipientMissingException < MollieException; @@resultcode = 23; end
class MessageMissingException < MollieException; @@resultcode = 24; end
class InvalidRecipientException < MollieException; @@resultcode = 25; end
class InvalidOriginatorException < MollieException; @@resultcode = 26; end
class InvalidMessageException < MollieException; @@resultcode = 27; end
class ParameterException < MollieException; @@resultcode = 29; end
class AuthenticationException < MollieException; @@resultcode = 30; end
class InsufficientCreditsException < MollieException; @@resultcode = 31; end
class GatewayUnreachableException < MollieException; @@resultcode = 98; end
class UnknownException < MollieException; @@resultcode = 99; end
end
# Mollie cancelled errors
class NoReferenceException < MollieException; @resultcode = 22; end
class ReferencedMessageNotFound < MollieException; @resultcode = 40; end

# Self proclaimed errors
class WrongDateFormatException < MollieException; @resultcode = -2; end
class DeliveryDateButNoReferenceException < MollieException; @resultcode = -1; end

# Mollie send errors
class NoUserNameException < MollieException; @resultcode = 20; end
class NoPasswordException < MollieException; @resultcode = 21; end
class InvalidOriginatorException < MollieException; @resultcode = 22; end
class RecipientMissingException < MollieException; @resultcode = 23; end
class MessageMissingException < MollieException; @resultcode = 24; end
class InvalidRecipientException < MollieException; @resultcode = 25; end
class InvalidOriginatorException < MollieException; @resultcode = 26; end
class InvalidMessageException < MollieException; @resultcode = 27; end
class ParameterException < MollieException; @resultcode = 29; end
class AuthenticationException < MollieException; @resultcode = 30; end
class InsufficientCreditsException < MollieException; @resultcode = 31; end
class GatewayUnreachableException < MollieException; @resultcode = 98; end
class UnknownException < MollieException; @resultcode = 99; end
end

0 comments on commit 1890870

Please sign in to comment.