sms_sender 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,80 @@
1
+ SmsSender
2
+ =========
3
+
4
+ This plugin aims to provide very easy way to send an sms from your Rails app.
5
+ The service used for sending sms messages is Polish http://smsapi.pl. In order
6
+ to use the plugin you have to sign up at http://smsapi.pl. They give you a few
7
+ free smses, so you can test how the plugin works for you without paying.
8
+
9
+ Installation
10
+ =============
11
+
12
+ To install the plugin, just add it in your gemfile:
13
+
14
+ gem "sms_sender"
15
+
16
+ Configuration
17
+ ==============
18
+
19
+ All you need to do is add following lines in your config/application.rb:
20
+
21
+ config.sms_sender_login="your login at smsapi.pl"
22
+ config.sms_sender_password="your password at smsapi.pl"
23
+ config.sms_sender_eco=1
24
+
25
+ Alternatively, if you don't want to send eco smses, you comment the last line and add:
26
+
27
+ #config.sms_sender_eco=1
28
+ config.sms_sender_from="5555"
29
+
30
+ You may also wish to send test requests to smsapi while you develop your app. I always use
31
+ it development mode. Just add in config/development.rb:
32
+
33
+ config.sms_sender_test=1
34
+
35
+ With this the plugin will send requests to smsapi, but you will not be charged and people
36
+ will not get any messages.
37
+
38
+ Usage
39
+ =====
40
+
41
+ The plugin adds a method:
42
+
43
+ send_sms(telephone, text)
44
+
45
+ This method is available in all your controllers. When sth goes wrong it will raise SmsApiError,
46
+ so it's wise to write:
47
+
48
+ begin
49
+ send_sms("666", "hello world")
50
+ rescue SmsApiError => e
51
+ logger.error "Sms api failed with error #{e.error_code}"
52
+ .... other error handling code
53
+ end
54
+
55
+ Testing
56
+ =======
57
+
58
+ When you run your tests the plugin does not send any requests to smsapi. Instead it uses
59
+ a fake implementation of sender and gives you two extra assertions. In your tests you may write:
60
+
61
+ assert_sms_sent "6666", "hello world"
62
+
63
+ This will be passed if somewhere in your code there was a call
64
+
65
+ send_sms "6666", "hellow world"
66
+
67
+ Be careful though, assert_sms_sent will be passed once only. If you call it twice without
68
+ a proper call to send_sms, it will fail.
69
+
70
+ On the other hand you may want to check:
71
+
72
+ assert_no_sms_sent
73
+
74
+ That will be passed iff there was no calls to send_sms.
75
+
76
+ Both assertions are added to Test::Unit::Assertions. They work in test/unit tests, as well as
77
+ in RSpec and Cucumber.
78
+
79
+ ======
80
+ Copyright (c) 2011 RocketMind Software, released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the sms_sender plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ desc 'Generate documentation for the sms_sender plugin.'
18
+ Rake::RDocTask.new(:rdoc) do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'SmsSender'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
25
+
26
+ PKG_FILES = FileList[
27
+ '[a-zA-Z]*',
28
+ 'generators/**/*',
29
+ 'lib/**/*',
30
+ 'rails/**/*',
31
+ 'tasks/**/*',
32
+ 'test/**/*'
33
+ ]
34
+
35
+ spec = Gem::Specification.new do |s|
36
+ s.name = "sms_sender"
37
+ s.version = "0.0.3"
38
+ s.author = "Maciej Zubala - RocketMind Software"
39
+ s.email = "maciej.zubala@rocketmind.pl"
40
+ s.platform = Gem::Platform::RUBY
41
+ s.summary = "Sends sms messages using http://smsapi.pl"
42
+ s.homepage = "https://github.com/mzubala/sms_sender"
43
+ s.files = PKG_FILES.to_a
44
+ s.require_path = "lib"
45
+ s.has_rdoc = false
46
+ s.extra_rdoc_files = ["README"]
47
+ end
48
+
49
+ desc 'Turn this plugin into a gem.'
50
+ Rake::GemPackageTask.new(spec) do |pkg|
51
+ pkg.gem_spec = spec
52
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "sms_sender"
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,15 @@
1
+ module ActionController
2
+
3
+ class Base
4
+
5
+ include SmsSender
6
+
7
+ def send_sms telephone, text
8
+ message = Message.new telephone, text
9
+ sender = create_sms_sender
10
+ sender.send_sms message
11
+ end
12
+
13
+ end
14
+
15
+ end
data/lib/message.rb ADDED
@@ -0,0 +1,28 @@
1
+ module SmsSender
2
+
3
+ class Message
4
+
5
+ attr_accessor :text, :telephone
6
+
7
+ def initialize telephone, text
8
+ @text = text
9
+ @telephone = normalized_telephone telephone
10
+ end
11
+
12
+ def == other
13
+ ((other.text || "") == self.text) && ((other.telephone || "") == self.telephone)
14
+ end
15
+
16
+ def to_s
17
+ "#{@telephone} - #{@text}"
18
+ end
19
+
20
+ private
21
+
22
+ def normalized_telephone telephone
23
+ telephone.gsub(/[^\d]/, "")
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,89 @@
1
+ require 'net/http'
2
+ require 'digest/md5'
3
+
4
+ module SmsSender
5
+
6
+ class SmsApiSender
7
+
8
+ SMS_API_URL = "https://ssl.smsapi.pl/send.do"
9
+
10
+ attr_writer :from, :test, :eco, :login, :password
11
+
12
+ def initialize logger
13
+ @logger = logger
14
+ end
15
+
16
+ def send_sms message
17
+ url = URI.parse(SMS_API_URL)
18
+ req = Net::HTTP::Post.new(url.path)
19
+ params = basic_params message
20
+ add_extra_params(params)
21
+ req.set_form_data(params)
22
+ @logger.info "Requesting sms api to send message: #{truncated_text(message)}"
23
+ http = Net::HTTP.new(url.host, url.port)
24
+ http.use_ssl = true
25
+ res = http.start { |http| http.request(req) }
26
+ @logger.info "Sms api request finished processing with response #{res.body}"
27
+ if sms_not_sent? res
28
+ raise SmsApiError res.code
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def basic_params message
35
+ {
36
+ "username" => Rails.configuration.sms_sender_login,
37
+ "password" => Digest::MD5.hexdigest(Rails.configuration.sms_sender_password),
38
+ "to" => message.telephone,
39
+ "message" => truncated_text(message),
40
+ "encoding" => "utf-8"
41
+ }
42
+ end
43
+
44
+ def add_extra_params(params)
45
+ params.merge! "from" => Rails.configuration.sms_sender_from if Rails.configuration.respond_to?(:sms_sender_from)
46
+ params.merge! "test" => "1" if Rails.configuration.respond_to?(:sms_sender_test)
47
+ params.merge! "eco" => "1" if Rails.configuration.respond_to?(:sms_sender_eco)
48
+ end
49
+
50
+ def sms_not_sent? response
51
+ !response.is_a?(Net::HTTPSuccess) || !(response.body =~ /.*OK.*/)
52
+ end
53
+
54
+ def truncated_text message
55
+ text = message.text
56
+ length = [text.length, max_len_of_text(text)].min
57
+ text[0, length]
58
+ end
59
+
60
+ def max_len_of_text text
61
+ if text.match SPECIAL_CHARS
62
+ MAX_LEN_SHORT
63
+ else
64
+ MAX_LEN_LONG
65
+ end
66
+ end
67
+
68
+ MAX_LEN_SHORT = 201
69
+ MAX_LEN_LONG = 457
70
+
71
+ SPECIAL_CHARS = /[^@\u00A3\$\u00A5\u00E8\u00E9\u00F9\u00EC\u00F2\u00C7\u00D8\u00F8\u00C5\u00E5_\^\{\}\[~\]\|\u00C6\u00E6\u00DF\u00C9!"#\u00A4%&'\(\)\*\+\,\-\.\/0-9:;<=>\?A-Z\u00C4\u00D6\u00D1\u00DC\u00A7\u00BFa-z\u00E4\u00F6\u00F1\u00FC\u00E0 \r\n]/
72
+
73
+ end
74
+
75
+ class SmsApiError
76
+
77
+ attr_reader :error_code
78
+
79
+ def initialize error_code
80
+ @error_code = error_code
81
+ end
82
+
83
+ def to_s
84
+ "Sms Api error code: #{@error_code}"
85
+ end
86
+
87
+ end
88
+
89
+ end
data/lib/sms_sender.rb ADDED
@@ -0,0 +1,16 @@
1
+ module SmsSender
2
+
3
+ def create_sms_sender
4
+ if Rails.env == "test"
5
+ TestSender.new
6
+ else
7
+ SmsApiSender.new logger
8
+ end
9
+ end
10
+
11
+ end
12
+
13
+ require "message"
14
+ require "test_sender"
15
+ require "sms_api_sender"
16
+ require "action_controller_ext"
@@ -0,0 +1,50 @@
1
+ module SmsSender
2
+
3
+ class TestSender
4
+
5
+ @@sent_messages = []
6
+
7
+ def send_sms message
8
+ @@sent_messages << message
9
+ end
10
+
11
+ def self.sent? message
12
+ @@sent_messages.include? message
13
+ end
14
+
15
+ def self.sent_count
16
+ @@sent_messages.size
17
+ end
18
+
19
+ def self.clear
20
+ @@sent_messages = []
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
27
+ module Test
28
+
29
+ module Unit
30
+
31
+ module Assertions
32
+
33
+ def assert_sms_sent telephone, text
34
+ msg = SmsSender::Message.new(telephone, text)
35
+ sent = SmsSender::TestSender.sent?(msg)
36
+ SmsSender::TestSender.clear
37
+ assert(sent, "Message #{msg} should be sent, but it was not sent.")
38
+ end
39
+
40
+ def assert_no_sms_sent
41
+ c = SmsSender::TestSender.sent_count
42
+ SmsSender::TestSender.clear
43
+ assert(c == 0, "No messages should be sent, but #{c} messages was sent")
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class SmsSenderTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sms_sender
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.3
6
+ platform: ruby
7
+ authors:
8
+ - Maciej Zubala - RocketMind Software
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-18 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email: maciej.zubala@rocketmind.pl
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README
25
+ files:
26
+ - init.rb
27
+ - install.rb
28
+ - MIT-LICENSE
29
+ - Rakefile
30
+ - README
31
+ - uninstall.rb
32
+ - lib/action_controller_ext.rb
33
+ - lib/message.rb
34
+ - lib/sms_api_sender.rb
35
+ - lib/sms_sender.rb
36
+ - lib/test_sender.rb
37
+ - test/sms_sender_test.rb
38
+ - test/test_helper.rb
39
+ has_rdoc: true
40
+ homepage: https://github.com/mzubala/sms_sender
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.5.0
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Sends sms messages using http://smsapi.pl
67
+ test_files: []
68
+