kannel_rails 0.0.1

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 2012 YOURNAME
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.md ADDED
@@ -0,0 +1,67 @@
1
+ ## KannelRails
2
+
3
+ KannelRails is a Rails engine that allows you to easily integrate with Kannel smsbox via HTTP to send and receive SMS
4
+
5
+ ### Installing
6
+
7
+ Just add KannelRails to your Gemfile:
8
+
9
+ ```ruby
10
+ gem 'kannel_rails'
11
+ ```
12
+
13
+ KannelRails is tested on Rails 3.2. It might work for Rails 3.0 and up.
14
+
15
+ ### Configuration
16
+
17
+ In your Rails app, add a configuration file: config/kannel_rails.yml
18
+
19
+ Sample content:
20
+
21
+ ```yaml
22
+ development:
23
+ kannel_url: http://kannel_server:13013 # Kannel smsbox sendsms-port
24
+ username: username # Kannel sendsms-user username
25
+ password: password # Kannel sendsms-user password
26
+ api_secret: testing # A secret key you will also configure in Kannel for extra security
27
+ ```
28
+
29
+ In config/routes.rb, add:
30
+
31
+ ```ruby
32
+ mount KannelRails::Engine => '/sms'
33
+ ```
34
+
35
+ In your Kannel config, configure your sms-service like so:
36
+
37
+ ```
38
+ group = sms-service
39
+ omit-empty = true
40
+ get-url = "http://rails_app/sms/receive_message?api_secret=api_secret_you_set_in_kannel_rails_yml&from=%p&text=%a"
41
+ ```
42
+
43
+ You can change the mount point of the rails engine to anything you want but make sure you also change the kannel get-url
44
+
45
+ ### Sending SMS
46
+
47
+ Sending an SMS is as simple as:
48
+
49
+ ```ruby
50
+ KannelRails.send_message("+639001234567", "Hello World!")
51
+ ```
52
+
53
+ ### Receiving SMS
54
+
55
+ To handle incoming SMS, create handler classes and register them.
56
+
57
+ Classes have an invoke method which is called when handle? is true
58
+
59
+ The return value of invoke will be the response to Kannel, which will be sent back to the sender as SMS. omit-empty is set in the sms-service so that empty strings will not be sent back.
60
+
61
+ Sample handler is in spec/dummy/lib/echo_handler.rb
62
+
63
+ Register the handler class in config/initializers/sms_handlers.rb (or some other place if you want):
64
+
65
+ ```ruby
66
+ KannelRails::Handlers.register HandlerClass
67
+ ```
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'KannelRails'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,4 @@
1
+ module KannelRails
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,24 @@
1
+ module KannelRails
2
+ class SmsController < ApplicationController
3
+
4
+ def receive_message
5
+ if KannelRails.config.api_secret and KannelRails.config.api_secret != params[:api_secret]
6
+ render :status => :forbidden, :text => "Unauthorized Request"
7
+ else
8
+ reply = ""
9
+
10
+ KannelRails::Handlers.each do |handler_class|
11
+ handler = handler_class.new(params[:from], params[:text])
12
+
13
+ if handler.handle?
14
+ reply = handler.invoke
15
+ break
16
+ end
17
+ end
18
+
19
+ render :text => reply
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ module KannelRails
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>KannelRails</title>
5
+ <%= stylesheet_link_tag "kannel_rails/application", :media => "all" %>
6
+ <%= javascript_include_tag "kannel_rails/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ KannelRails::Engine.routes.draw do
2
+
3
+ match 'receive_message' => 'sms#receive_message'
4
+
5
+ end
@@ -0,0 +1,35 @@
1
+ require "kannel_rails/engine"
2
+ require "kannel_rails/config"
3
+ require "kannel_rails/handlers"
4
+ require "kannel_rails/handlers/base"
5
+ require "net/http"
6
+
7
+ module KannelRails
8
+
9
+ def self.send_message(recipient, message)
10
+ request_url = config.kannel_url.chomp('/') + "/cgi-bin/sendsms?" +
11
+ "username=#{CGI.escape(config.username.to_s)}&password=#{CGI.escape(config.password.to_s)}" +
12
+ "&to=#{CGI.escape(recipient.to_s)}&text=#{CGI.escape(message.to_s)}"
13
+
14
+ response = Net::HTTP.get_response(URI.parse(
15
+ request_url
16
+ ))
17
+
18
+ response.is_a? Net::HTTPSuccess
19
+ end
20
+
21
+ def self.config
22
+ unless defined?(@config)
23
+ config_file = YAML.load_file(File.join(Rails.root, 'config/kannel_rails.yml'))
24
+
25
+ if config_file[Rails.env]
26
+ @config = KannelRails::Config.new(config_file[Rails.env])
27
+ else
28
+ raise "Missing section #{Rails.env} in config/kannel_rails.yml!"
29
+ end
30
+ end
31
+
32
+ return @config
33
+ end
34
+
35
+ end
@@ -0,0 +1,17 @@
1
+ class KannelRails::Config
2
+
3
+ attr_accessor :kannel_url
4
+ attr_accessor :username
5
+ attr_accessor :password
6
+ attr_accessor :dlr_url
7
+ attr_accessor :api_secret
8
+
9
+ def initialize(config_hash = {})
10
+ self.kannel_url = config_hash['kannel_url']
11
+ self.username = config_hash['username']
12
+ self.password = config_hash['password']
13
+ self.dlr_url = config_hash['dlr_url']
14
+ self.api_secret = config_hash['api_secret']
15
+ end
16
+
17
+ end
@@ -0,0 +1,5 @@
1
+ module KannelRails
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace KannelRails
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ module KannelRails::Handlers
2
+
3
+ @handlers = []
4
+
5
+ def self.register(handler_class)
6
+ @handlers << handler_class
7
+ end
8
+
9
+ def self.each(&block)
10
+ @handlers.each(&block)
11
+ end
12
+
13
+ end
@@ -0,0 +1,18 @@
1
+ class KannelRails::Handlers::Base
2
+
3
+ attr_accessor :sender, :message
4
+
5
+ def initialize(sender, message)
6
+ self.sender = sender
7
+ self.message = message
8
+ end
9
+
10
+ def handle?
11
+ raise "Please override this method!"
12
+ end
13
+
14
+ def invoke
15
+ raise "Please override this method!"
16
+ end
17
+
18
+ end
@@ -0,0 +1,3 @@
1
+ module KannelRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :kannel_rails do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kannel_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Heinrich Lee Yu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &69194230 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *69194230
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec-rails
27
+ requirement: &69193660 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *69193660
36
+ - !ruby/object:Gem::Dependency
37
+ name: webmock
38
+ requirement: &69193370 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *69193370
47
+ description: Kannel integration for Rails
48
+ email:
49
+ - hleeyu@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - app/controllers/kannel_rails/application_controller.rb
55
+ - app/controllers/kannel_rails/sms_controller.rb
56
+ - app/helpers/kannel_rails/application_helper.rb
57
+ - app/assets/javascripts/kannel_rails/application.js
58
+ - app/assets/stylesheets/kannel_rails/application.css
59
+ - app/views/layouts/kannel_rails/application.html.erb
60
+ - config/routes.rb
61
+ - lib/tasks/kannel_rails_tasks.rake
62
+ - lib/kannel_rails/handlers/base.rb
63
+ - lib/kannel_rails/engine.rb
64
+ - lib/kannel_rails/handlers.rb
65
+ - lib/kannel_rails/config.rb
66
+ - lib/kannel_rails/version.rb
67
+ - lib/kannel_rails.rb
68
+ - MIT-LICENSE
69
+ - Rakefile
70
+ - README.md
71
+ homepage:
72
+ licenses: []
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ segments:
84
+ - 0
85
+ hash: 567176499
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ segments:
93
+ - 0
94
+ hash: 567176499
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 1.8.10
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Kannel integration for Rails
101
+ test_files: []