twiml_template 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f90ef5352b1a656e01def52b0e9957530b91be9e
4
+ data.tar.gz: a3645b36b5ba6a3109b8f7927ccfdeb08dd32e99
5
+ SHA512:
6
+ metadata.gz: 8804cf2316db6d4e5dc79ccf5196b1f065eb717341addbb385bcbe4cffccc38b236ddb3b38ee8a478cfb84b63585579ad1271a23b32f3b630dfbbffb816f2ea8
7
+ data.tar.gz: 5542483ad83bbd71108132043b10a4c8064acb9ae3457301bdd3d344e6b4b8a46c7f4bcdf94ab5efb598d13b7ae54454e05fd86990306373bdf14ec6e8585e5a
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ before_install: gem install bundler
3
+ install:
4
+ - bundle install --retry=3
5
+ cache: bundler
6
+ rvm:
7
+ - 1.9.3
8
+ - 2.0.0
9
+ - 2.1.0
10
+ - jruby-19mode
11
+ - rbx
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in twiml_template.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Phil Nash
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # TwimlTemplate
2
+
3
+ [TwiML](https://www.twilio.com/docs/api/twiml) templates for Tilt.
4
+
5
+ An easy way to work with TwiML for responding to [Twilio](http://twilio.com) webhooks in Rails or Sinatra applications using template files.
6
+
7
+ [![Build Status](https://travis-ci.org/philnash/twiml_template.svg)](https://travis-ci.org/philnash/twiml_template)[![Code Climate](https://codeclimate.com/github/philnash/twiml_template/badges/gpa.svg)](https://codeclimate.com/github/philnash/twiml_template)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'twiml_template'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install twiml_template
24
+
25
+ ## Usage
26
+
27
+ ### Rails
28
+
29
+ By including the `twiml_template` gem in your Rails project Gemfile you will be able to write TwiML templates easily.
30
+
31
+ Create a controller, like below:
32
+
33
+ ```ruby
34
+ class VoiceController < ApplicationController
35
+ def index
36
+ @name = "World"
37
+ end
38
+ end
39
+ ```
40
+
41
+ Add a route:
42
+
43
+ ```ruby
44
+ Rails.application.routes.draw do
45
+ get 'voice' => 'voice#index'
46
+
47
+ # Other routes...
48
+ end
49
+ ```
50
+
51
+ And then add your TwiML view:
52
+
53
+ ```ruby
54
+ twiml.Say "Hello #{@name}"
55
+ ```
56
+
57
+ Save the file as `#{RAILS_ROOT}/app/controller/voice/index.twiml`.
58
+
59
+ Run the app using `rails s` and visit [http://localhost:3000/voice](http://localhost:3000/voice) and you will see:
60
+
61
+ ```xml
62
+ <?xml version="1.0" encoding="UTF-8"?>
63
+ <Response>
64
+ <Say>Hello World!</Say>
65
+ </Response>
66
+ ```
67
+
68
+ ### Sinatra
69
+
70
+ Create your application file like below:
71
+
72
+ ```ruby
73
+ require 'sinatra'
74
+ require 'sinatra/twiml'
75
+
76
+ helpers Sinatra::TwiML
77
+
78
+ get '/voice' do
79
+ @name = "World!"
80
+ twiml :voice
81
+ end
82
+ ```
83
+
84
+ And then add your TwiML view:
85
+
86
+ ```ruby
87
+ twiml.Say "Hello #{@name}"
88
+ ```
89
+
90
+ Save the file as `#{APP_ROOT}/views/voice.twiml`.
91
+
92
+ Start the app with `ruby app.rb` and visit [http://localhost:4567/voice](http://localhost:4567/voice) and you will see:
93
+
94
+ ```xml
95
+ <?xml version="1.0" encoding="UTF-8"?>
96
+ <Response>
97
+ <Say>Hello World!</Say>
98
+ </Response>
99
+ ```
100
+
101
+ ## Contributing
102
+
103
+ 1. Fork it ( https://github.com/philnash/twiml_template/fork )
104
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
105
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
106
+ 4. Push to the branch (`git push origin my-new-feature`)
107
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rake/testtask'
4
+ task :default => [:spec]
5
+
6
+ desc 'Run tests (default)'
7
+ Rake::TestTask.new(:spec) do |t|
8
+ t.test_files = FileList['spec/**/*_spec.rb']
9
+ t.ruby_opts = ['-Ispec']
10
+ t.ruby_opts << '-rubygems' if defined? Gem
11
+ end
@@ -0,0 +1,13 @@
1
+ require 'twilio-ruby'
2
+
3
+ module ActionView
4
+ module Template::Handlers
5
+ class TwiML
6
+ def self.call(template)
7
+ "self.output_buffer = ::Twilio::TwiML::Response.new do |twiml|;" +
8
+ template.source +
9
+ ";end.to_xml;"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ require 'tilt/twiml'
2
+
3
+ module Sinatra
4
+ module TwiML
5
+ def twiml(template = nil, options = {}, locals = {}, &block)
6
+ options[:default_content_type] = :xml
7
+ render_ruby(:twiml, template, options, locals, &block)
8
+ end
9
+ end
10
+ end
data/lib/tilt/twiml.rb ADDED
@@ -0,0 +1,36 @@
1
+ require 'twilio-ruby'
2
+ require 'tilt'
3
+
4
+ module Tilt
5
+ class TwiML < Template
6
+ self.default_mime_type = 'text/xml'
7
+
8
+ def prepare; end
9
+
10
+ def evaluate(scope, locals, &block)
11
+ return super(scope, locals, &block) if data.respond_to?(:to_str)
12
+ twiml = ::Twilio::TwiML::Response.new do |response|
13
+ data.call(response)
14
+ end.to_xml
15
+ end
16
+
17
+ def precompiled_template(local_keys)
18
+ data.to_str
19
+ end
20
+
21
+ def precompiled_preamble(locals)
22
+ return super if locals.include? :twiml
23
+ "::Twilio::TwiML::Response.new do |twiml|\n#{super}"
24
+ end
25
+
26
+ def precompiled_postamble(locals)
27
+ "end.to_xml"
28
+ end
29
+ end
30
+
31
+ if defined? register_lazy
32
+ register_lazy 'Tilt::TwiML', 'tilt/twiml', 'twiml'
33
+ else
34
+ register TwiML, '.twiml'
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ require 'twiml_template/version'
2
+
3
+ if defined? Rails
4
+ require 'twiml_template/railtie'
5
+ end
@@ -0,0 +1,17 @@
1
+ require 'action_view/template/handlers/twiml'
2
+
3
+ module TwiML
4
+ class Railtie < Rails::Railtie
5
+ initializer "twiml.configure_templates" do |app|
6
+ config.after_initialize do
7
+ Mime::Type.register "text/xml", :twiml
8
+ ActiveSupport.on_load(:action_view) do
9
+ ActionView::Template.register_template_handler(
10
+ :twiml,
11
+ ActionView::Template::Handlers::TwiML
12
+ )
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module TwimlTemplate
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+ require 'rack/test'
3
+ require 'sinatra/base'
4
+ require 'sinatra/twiml'
5
+
6
+ class TwiMLApp < Sinatra::Base
7
+ set :root, File.dirname(__FILE__)
8
+ helpers Sinatra::TwiML
9
+
10
+ get "/hello" do
11
+ twiml :hello, locals: {name: 'Joe'}
12
+ end
13
+ end
14
+
15
+ describe Sinatra::TwiML do
16
+ include Rack::Test::Methods
17
+
18
+ let(:app) { TwiMLApp }
19
+
20
+ let :twiml_response do
21
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response>" \
22
+ "<Say>Hello Joe!</Say></Response>"
23
+ end
24
+
25
+ it "renders simple template" do
26
+ response = get("/hello")
27
+ response.body.strip.must_equal twiml_response
28
+ end
29
+ end
@@ -0,0 +1 @@
1
+ twiml.Say "Hello #{name}!"
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'bundler'
4
+ Bundler.setup
5
+
6
+ require 'minitest/autorun'
7
+ require 'minitest/mock'
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+ require 'tilt/twiml'
3
+
4
+ describe Tilt::TwiML do
5
+ it "registers for '.twiml' files" do
6
+ Tilt['test.twiml'].must_equal Tilt::TwiML
7
+ Tilt['test.xml.twiml'].must_equal Tilt::TwiML
8
+ end
9
+
10
+ describe 'simple rendering' do
11
+ let :twiml_response do
12
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response>" \
13
+ "<Say>Hello World!</Say></Response>"
14
+ end
15
+
16
+ it "prepares and evaluates the template on #render" do
17
+ template = Tilt::TwiML.new { |t| "twiml.Say 'Hello World!'" }
18
+ template.render.must_equal twiml_response
19
+ end
20
+
21
+ it "can be rendered more than once" do
22
+ template = Tilt::TwiML.new { |t| "twiml.Say 'Hello World!'" }
23
+ 3.times { template.render.must_equal twiml_response }
24
+ end
25
+ end
26
+
27
+ describe 'rendering with locals/scopes' do
28
+
29
+ let :twiml_response do
30
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response>" \
31
+ "<Say>Hello Joe!</Say></Response>"
32
+ end
33
+
34
+ it "passes locals" do
35
+ template = Tilt::TwiML.new do |t|
36
+ "twiml.Say 'Hello ' + name + '!'"
37
+ end
38
+ template.render(Object.new, :name => 'Joe').must_equal twiml_response
39
+ end
40
+
41
+ it "evaluates in an object scope" do
42
+ template = Tilt::TwiML.new do |t|
43
+ "twiml.Say 'Hello ' + @name + '!'"
44
+ end
45
+ scope = Object.new
46
+ scope.instance_variable_set :@name, 'Joe'
47
+ template.render(scope).must_equal twiml_response
48
+ end
49
+
50
+ it "passes a block for yield" do
51
+ template = Tilt::TwiML.new do |t|
52
+ "twiml.Say 'Hello ' + yield + '!'"
53
+ end
54
+ 3.times { template.render { 'Joe' }.must_equal twiml_response }
55
+ end
56
+
57
+ it "takes block style templates" do
58
+ template =
59
+ Tilt::TwiML.new do |t|
60
+ lambda { |twiml| twiml.Say('Hello Joe!') }
61
+ end
62
+ template.render.must_equal twiml_response
63
+ end
64
+
65
+ it "allows nesting raw XML" do
66
+ subtemplate = '<Number>+447712345678</Number>'
67
+ template = Tilt::TwiML.new { "twiml.Dial { twiml << yield }" }
68
+ expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response>" \
69
+ "<Dial><Number>+447712345678</Number></Dial></Response>"
70
+ 3.times do
71
+ template.render { subtemplate }.must_equal expected
72
+ end
73
+ end
74
+
75
+ end
76
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'twiml_template/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "twiml_template"
8
+ spec.version = TwimlTemplate::VERSION
9
+ spec.authors = ["Phil Nash"]
10
+ spec.email = ["philnash@gmail.com"]
11
+ spec.summary = %q{TwiML templates for Rails and Tilt.}
12
+ spec.description = %q{An easy way to work with TwiML for responding to Twilio webhooks in Rails or Sinatra applications using template files.}
13
+ spec.homepage = "https://github.com/philnash/twiml_template"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+ spec.required_ruby_version = '>= 1.9.3'
21
+
22
+ spec.add_dependency "tilt", ">= 1.3", "< 2.0"
23
+ spec.add_dependency "twilio-ruby", "~> 3.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.0"
28
+ spec.add_development_dependency "rack-test"
29
+ spec.add_development_dependency "sinatra", ">= 1.3"
30
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twiml_template
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Phil Nash
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tilt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: twilio-ruby
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.7'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.7'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: minitest
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '5.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '5.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rack-test
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: sinatra
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '1.3'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '1.3'
117
+ description: An easy way to work with TwiML for responding to Twilio webhooks in Rails
118
+ or Sinatra applications using template files.
119
+ email:
120
+ - philnash@gmail.com
121
+ executables: []
122
+ extensions: []
123
+ extra_rdoc_files: []
124
+ files:
125
+ - ".gitignore"
126
+ - ".travis.yml"
127
+ - Gemfile
128
+ - LICENSE.txt
129
+ - README.md
130
+ - Rakefile
131
+ - lib/action_view/template/handlers/twiml.rb
132
+ - lib/sinatra/twiml.rb
133
+ - lib/tilt/twiml.rb
134
+ - lib/twiml_template.rb
135
+ - lib/twiml_template/railtie.rb
136
+ - lib/twiml_template/version.rb
137
+ - spec/sinatra/twiml_spec.rb
138
+ - spec/sinatra/views/hello.twiml
139
+ - spec/spec_helper.rb
140
+ - spec/tilt/twiml_spec.rb
141
+ - twiml_template.gemspec
142
+ homepage: https://github.com/philnash/twiml_template
143
+ licenses:
144
+ - MIT
145
+ metadata: {}
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: 1.9.3
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 2.2.2
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: TwiML templates for Rails and Tilt.
166
+ test_files:
167
+ - spec/sinatra/twiml_spec.rb
168
+ - spec/sinatra/views/hello.twiml
169
+ - spec/spec_helper.rb
170
+ - spec/tilt/twiml_spec.rb
171
+ has_rdoc: