tropo-webapi-ruby 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+ doc
21
+
22
+ ## PROJECT::SPECIFIC
23
+
24
+ *.gem
data/.yardoc ADDED
Binary file
data/HISTORY.rdoc ADDED
@@ -0,0 +1,5 @@
1
+ = Tropo Release History
2
+
3
+ == Version 0.1.0
4
+
5
+ - Alpha release
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2010 Voxeo, Corporation
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,192 @@
1
+ = Tropo Web API Ruby Library
2
+
3
+ A Ruby library for interaction with the Tropo Web API using JSON.
4
+
5
+ == Tropo Web API Overview
6
+
7
+ The Tropo Remote API provides a RESTful JSON API for controlling realtime communications applications from
8
+ your own web servers.
9
+
10
+ == Requirements
11
+
12
+ * Ruby v1.8.6+ or JRuby v1.4.0+
13
+ * RubyGems
14
+
15
+ == Installation
16
+
17
+ $ sudo gem install tropo-webapi-ruby
18
+
19
+ Optional, if you would like to use with Sinatra:
20
+
21
+ $ sudo gem install sinatra
22
+
23
+ == Generate Documentation
24
+
25
+ === Developer
26
+
27
+ $ gemserver
28
+
29
+ === Project Developer
30
+
31
+ $ sudo gem install yardoc
32
+
33
+ From within the project:
34
+
35
+ $ yardoc
36
+
37
+ == Usage
38
+
39
+ require 'rubygems'
40
+ require 'tropo-webapi-ruby'
41
+
42
+ # Will return the properly formatted JSON to pass to Tropo
43
+ response = Tropo::Generator.ask({ :say => 'Hello world!' })
44
+
45
+ # Will return a Ruby Hash, with some transformations, from the JSON string received from Tropo
46
+ response = Tropo::Generator.parse(json_string)
47
+
48
+ # Will provide instance variables that will allow you to easily reference session type
49
+ tropo = Tropo::Generator.new
50
+ response = tropo.parse(json_string)
51
+ p 'Hey, this is a voice session!' if tropo.voice_session
52
+ p 'Hey, this is a text messaging session!' if tropo.text_session
53
+
54
+ == Examples
55
+
56
+ === Sinatra
57
+
58
+ Using the great RESTful Web Services framework Sinatra for Ruby.
59
+
60
+ ==== Hello World
61
+
62
+ require 'rubygems'
63
+ require 'sinatra'
64
+ require 'tropo-webapi-ruby'
65
+
66
+ post '/helloworld.json' do
67
+ Tropo::Generator.say 'Hello World!'
68
+ end
69
+
70
+ post '/helloworld_block.json' do
71
+ tropo = Tropo::Generator.new do
72
+ say 'Hello World!'
73
+ end
74
+ tropo.response
75
+ end
76
+
77
+ post '/helloworld_twice.json' do
78
+ tropo = Tropo::Generator.new
79
+ tropo.say 'Hello World!'
80
+ tropo.say 'Hello again.'
81
+ tropo.response
82
+ end
83
+
84
+ ==== Getting Session Information
85
+
86
+ # Produces a Ruby hash:
87
+ #
88
+ # { :session =>
89
+ # { :timestamp => Tue Jan 19 18:27:46 -0500 2010,
90
+ # :user_type =>"HUMAN",
91
+ # :initial_text => nil,
92
+ # :account_id =>"0",
93
+ # :headers => [{ "value" => "70", "key"=>"Max-Forwards" },
94
+ # { "value" => "385", "key"=>"Content-Length" },
95
+ # { "value" => "<sip:127.0.0.1:49152>", "key"=>"Contact" },
96
+ # { "value" => "replaces", "key"=>"Supported" },
97
+ # { "value" => "<sip:sample.json@localhost:5555>", "key"=>"To" },
98
+ # { "value" => "1 INVITE", "key"=>"CSeq" },
99
+ # { "value" => "SJphone-M/1.65.382f (SJ Labs)", "key"=>"User-Agent" },
100
+ # { "value" => "SIP/2.0/UDP 127.0.0.1:49152;branch=z9000000a9;rport=49152", "key"=>"Via" },
101
+ # { "value" => "3FA7C70A1DD211B286B7A583D7B46DDD0xac106207", "key"=>"Call-ID" },
102
+ # { "value" => "application/sdp", "key"=>"Content-Type" },
103
+ # { "value" => "unknown <sip:127.0.0.1:49152>;tag=750b1b1648e9c876", "key"=>"From" }],
104
+ # :id => "3FA7C70A1DD211B286B7A583D7B46DDD0xac106207",
105
+ # :to => { :network => "PSTN",
106
+ # :channel => "VOICE",
107
+ # :name => "unknown",
108
+ # :id => "sample.json"},
109
+ # :from => { :network => "PSTN",
110
+ # :channel => "VOICE",
111
+ # :name => "unknown",
112
+ # :id => "unknown"}}}
113
+ #
114
+ post '/start_session.json' do
115
+ tropo_session = Tropo::Generator.parse request.env["rack.input"].read
116
+ p tropo_session
117
+ end
118
+
119
+ ==== Asking for input and receiving the response, or catching a hangup
120
+
121
+ post '/ask.json' do
122
+ tropo = Tropo::Generator.new do
123
+ on :event => 'hangup', :next => '/hangup.json'
124
+ on :event => 'continue', :next => '/answer.json'
125
+ ask({ :name => 'account_number',
126
+ :bargein => 'true',
127
+ :timeout => 30,
128
+ :require => 'true' }) do
129
+ say :value => 'Please say your account number'
130
+ choices :value => '[5 DIGITS]'
131
+ end
132
+ end
133
+ tropo.response
134
+ end
135
+
136
+ # Produces a Ruby hash, if the user gives a response before hanging up:
137
+ #
138
+ # { :result =>
139
+ # { :actions => { :attempts => 1,
140
+ # :disposition => "SUCCESS",
141
+ # :interpretation => "12345",
142
+ # :confidence => 100,
143
+ # :name => "account_number",
144
+ # :utterance => "1 2 3 4 5" },
145
+ # :session_duration => 3,
146
+ # :error => nil,
147
+ # :sequence => 1,
148
+ # :session_id => "5325C262-1DD2-11B2-8F5B-C16F64C1D62E@127.0.0.1",
149
+ # :state => "ANSWERED",
150
+ # :complete => true } }
151
+ #
152
+ post '/answer.json' do
153
+ tropo_event = Tropo::Generator.parse request.env["rack.input"].read
154
+ p tropo_event
155
+ end
156
+
157
+ # Produces a Ruby hash, if the user hangs up before giving a reponse
158
+ # { :result =>
159
+ # { :actions => {},
160
+ # :session_duration => 1,
161
+ # :error => nil,
162
+ # :sequence => 1,
163
+ # :session_id => "812BEF50-1DD2-11B2-8F5B-C16F64C1D62E@127.0.0.1",
164
+ # :state => "DISCONNECTED",
165
+ # :complete => true } }
166
+ #
167
+ post '/hangup.json' do
168
+ tropo_event = Tropo::Generator.parse request.env["rack.input"].read
169
+ p tropo_event
170
+ end
171
+
172
+ ==== Redirect a call before answering
173
+
174
+ # A redirect method
175
+ post '/redirect.json' do
176
+ Tropo::Generator.redirect({ :to => 'sip:1234', :from => '4155551212' })
177
+ end
178
+
179
+ ==== Reject a call before answering
180
+
181
+ # A reject method
182
+ post '/reject.json' do
183
+ Tropo::Generator.reject
184
+ end
185
+
186
+ === Additional Examples
187
+
188
+ May be found by checking out the project from Github, and then looking in $PROJECT_HOME/examples and $PROJECT_HOME/spec/tropo-webapi-ruby_spec.rb.
189
+
190
+ == Copyright
191
+
192
+ Copyright (c) 2010 Voxeo, Corporation. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "tropo-webapi-ruby"
8
+ gem.summary = "Tropo Web API Ruby Gem"
9
+ gem.description = "Ruby library for interacting with the Tropo Web API via REST & JSON"
10
+ gem.email = "jsgoecke@voxeo.com"
11
+ gem.homepage = "http://tropo.com"
12
+ gem.authors = ["Jason Goecke"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.files.include %w(lib/tropo-webapi-ruby.rb lib/tropo-webapi-ruby/tropo-webapi-ruby.rb lib/tropo-webapi-ruby/tropo-webapi-ruby-helpers.rb LICENSE VERSION README.markdown)
15
+ gem.add_dependency('json', '>= 1.2.0')
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :spec => :check_dependencies
36
+
37
+ task :default => :spec
38
+
39
+ require 'rake/rdoctask'
40
+ Rake::RDocTask.new do |rdoc|
41
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "tropo #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
49
+ # require 'yardoc'
50
+ # YARD::Rake::YardocTask.new do |t|
51
+ # t.files = ['lib/**/*.rb', OTHER_PATHS] # optional
52
+ # t.options = ['--any', '--extra', '--opts'] # optional
53
+ # end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.5
@@ -0,0 +1,193 @@
1
+ require 'rubygems'
2
+ require 'sinatra'
3
+ require '../lib/tropo-webapi-ruby'
4
+
5
+ enable :sessions
6
+
7
+ post '/index.json' do
8
+ tropo = Tropo::Generator.new do
9
+ on :event => 'continue', :next => '/the_answer.json'
10
+ ask({ :name => 'account_number',
11
+ :bargein => 'true',
12
+ :timeout => 30,
13
+ :require => 'true' }) do
14
+ say :value => 'Please enter your account number'
15
+ choices :value => '[5 DIGITS]'
16
+ end
17
+ end
18
+ tropo.response
19
+ end
20
+
21
+ post '/the_answer.json' do
22
+ Tropo::Generator.hangup
23
+ end
24
+
25
+ post '/helloworld.json' do
26
+ json_session = request.env["rack.input"].read
27
+ p json_session
28
+ tropo_session = Tropo::Generator.parse json_session
29
+ Tropo::Generator.say :value => 'Hello World!'
30
+ end
31
+
32
+ # ===
33
+ # Ask answer example
34
+ post '/ask.json' do
35
+ tropo = Tropo::Generator.new do
36
+ on :event => 'hangup', :next => '/hangup.json'
37
+ on :event => 'continue', :next => '/answer.json'
38
+ ask({ :name => 'account_number',
39
+ :bargein => 'true',
40
+ :timeout => 30,
41
+ :require => 'true' }) do
42
+ say :value => 'Please say your account number'
43
+ choices :value => '[5 DIGITS]'
44
+ end
45
+ end
46
+ tropo.response
47
+ end
48
+
49
+ post '/answer.json' do
50
+ tropo_event = Tropo::Generator.parse request.env["rack.input"].read
51
+ p tropo_event
52
+ end
53
+
54
+ post '/hangup.json' do
55
+ tropo_event = Tropo::Generator.parse request.env["rack.input"].read
56
+ p tropo_event
57
+ end
58
+ # ===
59
+ post '/say_goodbye.json' do
60
+ Tropo::Generator.say :value => 'Thank you. Goodbye.'
61
+ end
62
+
63
+ post '/start.json' do
64
+ tropo_session = Tropo::Generator.parse request.env["rack.input"].read
65
+ session[:callid] = tropo_session[:session][:id]
66
+ tropo = Tropo::Generator.new do
67
+ say :value => 'Hello World!'
68
+ on :event => 'hangup', :next => '/hangup.json'
69
+ end
70
+ tropo.response
71
+ end
72
+
73
+ post '/disconnect.json' do
74
+ tropo = Tropo::Generator.hangup
75
+ p tropo
76
+ tropo
77
+ end
78
+
79
+ post '/hangup.json' do
80
+ p 'Received a hangup response!'
81
+ json_string = request.env["rack.input"].read
82
+ tropo_session = Tropo::Generator.parse json_string
83
+ p tropo_session
84
+ end
85
+
86
+ post '/conference.json' do
87
+ tropo = Tropo::Generator.conference({ :name => 'foo',
88
+ :id => '1234',
89
+ :mute => false,
90
+ :send_tones => false,
91
+ :exit_tone => '#' }) do
92
+ on(:event => 'join') { say :value => 'Welcome to the conference' }
93
+ on(:event => 'leave') { say :value => 'Someone has left the conference' }
94
+ end
95
+ tropo
96
+ end
97
+
98
+ post '/result.json' do
99
+ tropo_result = Tropo::Generator.parse request.env["rack.input"].read
100
+ p tropo_result
101
+ end
102
+
103
+ post '/nomatch.json' do
104
+ Tropo::Generator.say :value => 'Something went terribly wrong!'
105
+ end
106
+
107
+ post '/redirect.json' do
108
+ response = Tropo::Generator.redirect(:to => 'sip:9991427589@sip.tropo.com')
109
+ p response
110
+ response
111
+ end
112
+
113
+ post '/reject.json' do
114
+ response = Tropo::Generator.reject
115
+ p response
116
+ response
117
+ end
118
+
119
+ post '/total_recording.json' do
120
+ tropo = Tropo::Generator.new do
121
+ start_recording :name => 'ladeda', :url => 'http://postthis/mofo'
122
+ say :value => 'I am now recording!'
123
+ stop_recording
124
+ end
125
+ p tropo.response
126
+ tropo.response
127
+ end
128
+
129
+ post '/record.json' do
130
+ response = Tropo::Generator.record({ :name => 'foo',
131
+ :url => 'http://sendme.com/tropo',
132
+ :beep => true,
133
+ :send_tones => false,
134
+ :exit_tone => '#' }) do
135
+ say :value => 'Please say your account number'
136
+ choices :value => '[5 DIGITS]'
137
+ end
138
+ p response
139
+ response
140
+ end
141
+
142
+ post '/session_scope.json' do
143
+ session = Hash.new
144
+ session[:foo] = 'bar'
145
+ tropo = Tropo::Generator.new(session) do
146
+ p session
147
+ say 'Do we now see the session?'
148
+ end
149
+ tropo.response
150
+ end
151
+
152
+ post '/transfer_request.json' do
153
+ tropo = Tropo::Generator.new do
154
+ say 'Hello, about to transfer you'
155
+ transfer :to => 'sip:9991427589@sip.tropo.com'
156
+ say 'I like rocks!'*10
157
+ end
158
+ p tropo.response
159
+ tropo.response
160
+ end
161
+
162
+ post '/ons.json' do
163
+ tropo = Tropo::Generator.new
164
+ p tropo.on :event => 'hangup', :next => '/hangup.json'
165
+ p tropo.on :event => 'continue', :next => '/next_resource.json'
166
+ tropo.say 'That was a boat load of ons...'
167
+ tropo.response
168
+ end
169
+
170
+ post '/on_with_block.json' do
171
+ tropo = Tropo::Generator.new
172
+ p tropo.on :event => 'hangup', :next => '/hangup.json'
173
+ p tropo.on :event => 'continue', :next => '/next_resource.json' do
174
+ say 'About to send you to the next menu.'
175
+ end
176
+ tropo.say 'That was a boat load of ons...'
177
+ tropo.response
178
+ end
179
+
180
+ post '/test_json.json' do
181
+ t = Tropo::Generator.new
182
+ t.on :event => 'error', :next => '/error.json' # For fatal programming errors. Log some details so we can fix it
183
+ t.on :event => 'hangup', :next => '/hangup.json' # When a user hangs or call is done. We will want to log some details.
184
+ t.on :event => 'continue', :next => '/next.json'
185
+ t.say "Hello"
186
+ t.start_recording(:name => 'recording', :url => "http://heroku-voip.marksilver.net/post_audio_to_s3?filename=foo.wav&unique_id=bar")
187
+ # [From this point, until stop_recording(), we will record what the caller *and* the IVR say]
188
+ t.say "You are now on the record."
189
+ # Prompt the user to incriminate themselve on-the-record
190
+ t.say "Go ahead, sing-along."
191
+ t.say "http://denalidomain.com/music/keepers/HappyHappyBirthdaytoYou-Disney.mp3"
192
+ t.response
193
+ end