routo 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ require 'routo/exception'
2
+ require 'routo/number'
3
+ require 'routo/message'
4
+ require 'pony'
5
+ module Routo
6
+ mattr_accessor :username
7
+ @@username = ""
8
+ mattr_accessor :password
9
+ @@password = ""
10
+ mattr_accessor :http_api_url
11
+ @@http_api_url = "http://smsc5.routotelecom.com/SMSsend"
12
+ mattr_accessor :http_api_url_backup
13
+ @@http_api_url_backup = "http://smsc56.routotelecom.com/SMSsend"
14
+ mattr_accessor :ownnum
15
+ mattr_accessor :type
16
+ mattr_accessor :delivery
17
+ @@delivery = 0
18
+
19
+ # Email Options
20
+ # config.email : recipient for the email notifications (default: nil)
21
+ # config.email_options : Hash with email config (smtp, sendmail, ...). We use the Pony gem to send mails, this hash will be send to Pony.options
22
+ # config.seconds_between_mails : Minimum time (in seconds) between 2 mails with the same subject
23
+ mattr_accessor :email
24
+ mattr_accessor :email_options
25
+ @@email_options = {}
26
+ mattr_accessor :seconds_between_mails
27
+ @@seconds_between_mails = 600
28
+
29
+ mattr_accessor :no_credits_left
30
+ @@no_credits_left = {:subject => 'Routo Messaging: no credits left', :body => 'You do not have any credit left on Routo messaging. You should buy some more credits in order to be able to send more SMS.'}
31
+ mattr_accessor :no_credits_left_sent_at
32
+
33
+ mattr_accessor :using_backup_url
34
+ @@using_backup_url = {:subject => 'Routo Messaging: using backup url', :body => 'The primary Routo Messaging http API url was not responding correctly. We tried the backup API.'}
35
+ mattr_accessor :using_backup_url_sent_at
36
+
37
+ def self.config
38
+ yield self
39
+ Pony.options = @@email_options
40
+ end
41
+
42
+ def self.send_sms msg, *numbers
43
+ Message.new(msg).send_sms(*numbers)
44
+ end
45
+
46
+ end
@@ -0,0 +1,12 @@
1
+ module Routo
2
+
3
+ module Exception
4
+ class Base < ::Exception
5
+ def initialize msg
6
+ super(msg)
7
+ end
8
+ end
9
+ end
10
+
11
+ end
12
+ Dir[File.expand_path('../exception/', __FILE__)+'/*.rb'].each {|file| require(file) }
@@ -0,0 +1,7 @@
1
+ module Routo
2
+ module Exception
3
+ class AuthFailed < Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Routo
2
+ module Exception
3
+ class BadOperator < Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Routo
2
+ module Exception
3
+ class Error < Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Routo
2
+ module Exception
3
+ class Failed < Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module Routo
2
+ module Exception
3
+ class NoCreditsLeft < Base
4
+ def initialize msg
5
+ super(msg)
6
+ Pony.mail(:to => Routo.email, :subject => Routo.no_credits_left[:subject], :body => Routo.no_credits_left[:body]) if Routo.email && Routo.no_credits_left.present? && Routo.no_credits_left_sent_at.to_i<(Time.now.to_i-Routo.seconds_between_mails)
7
+ Routo.no_credits_left_sent_at = Time.now
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module Routo
2
+ module Exception
3
+ class NoMessage < Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Routo
2
+ module Exception
3
+ class NotAllowed < Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Routo
2
+ module Exception
3
+ class SystemError < Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Routo
2
+ module Exception
3
+ class TooLong < Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Routo
2
+ module Exception
3
+ class TooManyNumbers < Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Routo
2
+ module Exception
3
+ class WrongFormat < Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Routo
2
+ module Exception
3
+ class WrongMessage < Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Routo
2
+ module Exception
3
+ class WrongNumber < Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Routo
2
+ module Exception
3
+ class WrongType < Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,70 @@
1
+ module Routo
2
+
3
+ class Message
4
+
5
+ attr_accessor :text
6
+
7
+ def initialize text
8
+ @text = text
9
+ generate_mess_id
10
+ end
11
+
12
+ def send_sms *numbers
13
+ options = numbers.last.is_a?(Hash) ? numbers.last : {}
14
+ recipients = numbers.map{|n| Number.new(n)}
15
+ begin
16
+ parse_response(Net::HTTP.post_form(URI.parse(Routo.http_api_url), params(*recipients, options)))
17
+ rescue Routo::Exception::SystemError, Routo::Exception::Failed # trying with backup url if Routo returns an internal error
18
+ begin # here a mail error should not prevent the sms to be send
19
+ Pony.mail(:to => Routo.email, :subject => Routo.using_backup_url[:subject], :body => Routo.using_backup_url[:body]) if Routo.email && Routo.using_backup_url.present? && Routo.using_backup_url_sent_at.to_i<(Time.now.to_i-Routo.seconds_between_mails)
20
+ Routo.using_backup_url_sent_at = Time.now
21
+ ensure
22
+ parse_response(Net::HTTP.post_form(URI.parse(Routo.http_api_url_backup), params(*recipients, options)))
23
+ end
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def params *number_objects, options
30
+ {:user => Routo.username,
31
+ :pass => URI.encode(Routo.password),
32
+ :number => number_objects.map(&:number).join(','),
33
+ :ownnum => Routo.ownnum,
34
+ :message => URI.encode(@text),
35
+ :type => Routo.type,
36
+ :delivery => Routo.delivery,
37
+ :mess_id => @mess_id}.merge(options)
38
+ end
39
+
40
+ def generate_mess_id
41
+ chars = ("A".."Z").to_a + ("0".."9").to_a
42
+ @mess_id = Array.new(30){||chars[rand(chars.size)]}.join
43
+ end
44
+
45
+ def parse_response rep
46
+ Rails.logger.debug "body: "+rep.body.inspect
47
+ Rails.logger.debug "msg: "+rep.message.inspect
48
+ case rep.body
49
+ when /success/i then return true
50
+ when /error/i then raise Routo::Exception::Error.new(rep.body)
51
+ when /auth_failed/i then raise Routo::Exception::AuthFailed.new(rep.body)
52
+ when /wrong_number/i then raise Routo::Exception::WrongNumber.new(rep.body)
53
+ when /not_allowed/i then raise Routo::Exception::NotAllowed.new(rep.body)
54
+ when /too_many_numbers/i then raise Routo::Exception::TooManyNumbers.new(rep.body)
55
+ when /no_message/i then raise Routo::Exception::NoMessage.new(rep.body)
56
+ when /too_long/i then raise Routo::Exception::TooLong.new(rep.body)
57
+ when /wrong_type/i then raise Routo::Exception::WrongType.new(rep.body)
58
+ when /wrong_message/i then raise Routo::Exception::WrongMessage.new(rep.body)
59
+ when /wrong_format/i then raise Routo::Exception::WrongFormat.new(rep.body)
60
+ when /bad_operator/i then raise Routo::Exception::BadOperator.new(rep.body)
61
+ when /failed/i then raise Routo::Exception::Failed.new(rep.body)
62
+ when /sys_error/i then raise Routo::Exception::SystemError.new(rep.body)
63
+ when /no credits left/i then raise Routo::Exception::NoCreditsLeft.new(rep.body)
64
+ else raise Routo::Exception::Base.new(rep.body)
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ end
@@ -0,0 +1,13 @@
1
+ module Routo
2
+
3
+ class Number
4
+ attr_accessor :number
5
+ def initialize number
6
+ @number = number
7
+ end
8
+ def send_sms msg
9
+ Message.new(msg).send_sms(@number)
10
+ end
11
+ end
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: routo
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.2
6
+ platform: ruby
7
+ authors:
8
+ - Adrien Rambert
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-22 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: pony
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 1.0.0
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ description: Sending sms with Routo Messaging HTTP API.
28
+ email: arambert@weboglobin.com
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - lib/routo/exception/auth_failed.rb
37
+ - lib/routo/exception/bad_operator.rb
38
+ - lib/routo/exception/error.rb
39
+ - lib/routo/exception/failed.rb
40
+ - lib/routo/exception/no_credits_left.rb
41
+ - lib/routo/exception/no_message.rb
42
+ - lib/routo/exception/not_allowed.rb
43
+ - lib/routo/exception/system_error.rb
44
+ - lib/routo/exception/too_long.rb
45
+ - lib/routo/exception/too_many_numbers.rb
46
+ - lib/routo/exception/wrong_format.rb
47
+ - lib/routo/exception/wrong_message.rb
48
+ - lib/routo/exception/wrong_number.rb
49
+ - lib/routo/exception/wrong_type.rb
50
+ - lib/routo/exception.rb
51
+ - lib/routo/message.rb
52
+ - lib/routo/number.rb
53
+ - lib/routo.rb
54
+ has_rdoc: true
55
+ homepage: http://www.weboglobin.com
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.8.7
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 1.3.6
75
+ requirements:
76
+ - none
77
+ rubyforge_project:
78
+ rubygems_version: 1.5.0
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Sending sms with Routo Messaging.
82
+ test_files: []
83
+