sinatra-twilio 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'sinatra', :require => false
4
+ gem 'twiliolib'
5
+
6
+ group :development do
7
+ gem "rack-test"
8
+ gem "rspec", "~> 2.3.0"
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.6.2"
11
+ gem "rcov", ">= 0"
12
+ end
@@ -0,0 +1,41 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ builder (3.0.0)
5
+ diff-lcs (1.1.2)
6
+ git (1.2.5)
7
+ jeweler (1.6.2)
8
+ bundler (~> 1.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rack (1.3.0)
12
+ rack-test (0.6.0)
13
+ rack (>= 1.0)
14
+ rake (0.9.2)
15
+ rcov (0.9.9)
16
+ rspec (2.3.0)
17
+ rspec-core (~> 2.3.0)
18
+ rspec-expectations (~> 2.3.0)
19
+ rspec-mocks (~> 2.3.0)
20
+ rspec-core (2.3.1)
21
+ rspec-expectations (2.3.0)
22
+ diff-lcs (~> 1.1.2)
23
+ rspec-mocks (2.3.0)
24
+ sinatra (1.2.6)
25
+ rack (~> 1.1)
26
+ tilt (< 2.0, >= 1.2.2)
27
+ tilt (1.3.2)
28
+ twiliolib (2.0.7)
29
+ builder (>= 2.1.2)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ bundler (~> 1.0.0)
36
+ jeweler (~> 1.6.2)
37
+ rack-test
38
+ rcov
39
+ rspec (~> 2.3.0)
40
+ sinatra
41
+ twiliolib
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Bodaniel Jeanes
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.
@@ -0,0 +1,81 @@
1
+ = sinatra-twilio
2
+
3
+ This is a library for creating simple web apps to handle incoming
4
+ SMSes/Calls with Sinatra using a nice DSL.
5
+
6
+ == Installation
7
+
8
+ === Using Bundler
9
+
10
+ gem 'sinatra-twilio', :require => 'sinatra/twilio'
11
+
12
+ === Not using bundler
13
+
14
+ Run:
15
+
16
+ gem install sinatra-twilio
17
+
18
+ Then in you Sinatra app, add the following lines after you require
19
+ "sinatra":
20
+
21
+ require 'sinatra/twilio'
22
+
23
+ == A totally contrived and un-tested example
24
+
25
+ require "sinatra"
26
+ require "sinatra/twilio"
27
+
28
+ callers = %w[+15551234567]
29
+ pin = "1234"
30
+
31
+ respond "/call" do
32
+ addSay "Welcome caller."
33
+
34
+ if callers.include? params[:From]
35
+ addRedirect "/allowed_call"
36
+ else
37
+ addRedirect "/disallowed_call"
38
+ end
39
+ end
40
+
41
+ respond "/allowed_call" do
42
+ addPlay "/latest_message.mp3"
43
+ end
44
+
45
+ respond "/disallowed_call" do
46
+ gather = Twilio::Gather.new(:action => "/authenticate")
47
+ gather.addSay "Please enter your PIN now:"
48
+ append gather
49
+
50
+ addSay "You did not enter a pin. Good bye!"
51
+ addHangup
52
+ end
53
+
54
+ respond "/authenticate" do
55
+ if params[:Digits] == pin
56
+ addRedirect "/allowed_call"
57
+ else
58
+ addRedirect "/disallowed_call"
59
+ end
60
+ end
61
+
62
+ get "/latest_message.mp3" do
63
+ # Assuming we have an ORM...
64
+ send_file Message.last.path, :stream => true
65
+ end
66
+
67
+ == Contributing to sinatra-twilio
68
+
69
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
70
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
71
+ * Fork the project
72
+ * Start a feature/bugfix branch
73
+ * Commit and push until you are happy with your contribution
74
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
75
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
76
+
77
+ == Copyright
78
+
79
+ Copyright (c) 2011 Bodaniel Jeanes. See LICENSE.txt for
80
+ further details.
81
+
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "sinatra-twilio"
18
+ gem.homepage = "http://github.com/bjeanes/sinatra-twilio"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Easily create Twilio apps with Sinatra}
21
+ gem.description = %Q{Easily create Twilio apps with Sinatra}
22
+ gem.email = "me@bjeanes.com"
23
+ gem.authors = ["Bodaniel Jeanes"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "sinatra-twilio #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "sinatra", "twilio")
@@ -0,0 +1,26 @@
1
+ require "sinatra/base"
2
+ require "sinatra/twilio/response"
3
+
4
+ module Sinatra
5
+ module Twilio
6
+ module Helpers
7
+ end
8
+
9
+ def respond(route, conditions = {}, &block)
10
+ action = Proc.new do
11
+ twilio = Response.new(self)
12
+ twilio.instance_eval &block if block_given?
13
+ twilio.respond
14
+ end
15
+
16
+ get route, conditions, &action
17
+ post route, conditions, &action
18
+ end
19
+
20
+ def self.registered(app)
21
+ app.helpers Helpers
22
+ end
23
+ end
24
+
25
+ register Twilio
26
+ end
@@ -0,0 +1,24 @@
1
+ require "twiliolib"
2
+
3
+ module Sinatra
4
+ module Twilio
5
+ class Response < ::Twilio::Response
6
+ attr_accessor :target
7
+
8
+ def initialize(target)
9
+ super(nil)
10
+ self.target = target
11
+ end
12
+
13
+ def method_missing(method, *args, &block)
14
+ target.send(method, *args, &block)
15
+ rescue NoMethodError
16
+ super(method, *args, &block)
17
+ end
18
+
19
+ def self.allowed_verbs
20
+ superclass.allowed_verbs
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,79 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Sinatra::Application do
4
+ # Create a new sub-class each time to avoid bleeding routes between
5
+ # tests
6
+ let(:app) { Class.new(Sinatra::Application) }
7
+
8
+ describe "#respond" do
9
+ context "defining routes" do
10
+ let(:route) { "/foo" }
11
+ let(:action) { Proc.new {} }
12
+
13
+ before(:all) { app.respond(route, &action) }
14
+
15
+ it "defines a GET route" do
16
+ app.routes["GET"].should have(1).item
17
+ end
18
+
19
+ it "defines a POST route" do
20
+ app.routes["POST"].should have(1).item
21
+ end
22
+
23
+ it "should have identical GET and POST routes" do
24
+ t = Proc.new {|res| [res.status, res.headers, res.body] }
25
+
26
+ get_response = get(route)
27
+ post_response = post(route)
28
+
29
+ t[get_response].should == t[post_response]
30
+ end
31
+ end
32
+
33
+ context "returning TwiML response" do
34
+ it "allows empty block" do
35
+ lambda {
36
+ app.respond "/"
37
+ }.should_not raise_error
38
+ end
39
+
40
+ it "always returns a <Response> object" do
41
+ app.respond "/"
42
+ get("/").body.should =~ /^<Response>.*<\/Response>$/
43
+ end
44
+
45
+ it "doesn't return the response of the block" do
46
+ app.respond("/") { "foobar123" }
47
+ get("/").body.should_not =~ /foobar123/
48
+ end
49
+
50
+ it "creates a new Twilio::Response for each route" do
51
+ app.respond("/1") { addSay "1" }
52
+ app.respond("/2") { addSay "2" }
53
+
54
+ get("/1").body.should_not =~ /2/
55
+ get("/2").body.should_not =~ /1/
56
+ end
57
+ end
58
+
59
+ context "action block has access to normal Sinatra helpers" do
60
+ %w[params request response].each do |helper|
61
+ it "can access #{helper.inspect} method" do
62
+ app.respond("/") { eval(helper) }
63
+ get("/").status.should == 200
64
+ end
65
+ end
66
+ end
67
+
68
+ context "building Twilio::Response" do
69
+ it "can access instance methods on Twilio::Response transparently" do
70
+ app.respond("/") { addSay "1" }
71
+
72
+ get "/"
73
+
74
+ last_response.status.should == 200
75
+ last_response.body.should == "<Response><Say>1</Say></Response>"
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rspec'
5
+ require 'rack/test'
6
+
7
+ require 'sinatra/twilio'
8
+
9
+ # Requires supporting files with custom matchers and macros, etc,
10
+ # in ./support/ and its subdirectories.
11
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
12
+
13
+ RSpec.configure do |config|
14
+ config.include Rack::Test::Methods
15
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-twilio
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Bodaniel Jeanes
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-14 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: sinatra
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: twiliolib
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rack-test
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: 2.3.0
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: bundler
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: 1.0.0
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: jeweler
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: 1.6.2
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: *id006
82
+ - !ruby/object:Gem::Dependency
83
+ name: rcov
84
+ requirement: &id007 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: *id007
93
+ description: Easily create Twilio apps with Sinatra
94
+ email: me@bjeanes.com
95
+ executables: []
96
+
97
+ extensions: []
98
+
99
+ extra_rdoc_files:
100
+ - LICENSE.txt
101
+ - README.rdoc
102
+ files:
103
+ - .document
104
+ - .rspec
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - LICENSE.txt
108
+ - README.rdoc
109
+ - Rakefile
110
+ - VERSION
111
+ - lib/sinatra-twilio.rb
112
+ - lib/sinatra/twilio.rb
113
+ - lib/sinatra/twilio/response.rb
114
+ - spec/sinatra/twilio_spec.rb
115
+ - spec/spec_helper.rb
116
+ has_rdoc: true
117
+ homepage: http://github.com/bjeanes/sinatra-twilio
118
+ licenses:
119
+ - MIT
120
+ post_install_message:
121
+ rdoc_options: []
122
+
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ hash: 1766460854460249750
131
+ segments:
132
+ - 0
133
+ version: "0"
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: "0"
140
+ requirements: []
141
+
142
+ rubyforge_project:
143
+ rubygems_version: 1.6.2
144
+ signing_key:
145
+ specification_version: 3
146
+ summary: Easily create Twilio apps with Sinatra
147
+ test_files: []
148
+