Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1 from weyus/master

Updated RSpec tests to work with 2.x and fixed timestamp parsing issue
  • Loading branch information...
commit 5a270ce390c69f5c19f090491d7193ccca15ff1d 2 parents c0120c5 + 8d071dc
@jsgoecke jsgoecke authored
View
10 Rakefile
@@ -24,14 +24,12 @@ rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
end
-require 'spec/rake/spectask'
-Spec::Rake::SpecTask.new(:spec) do |spec|
- spec.libs << 'lib' << 'spec'
- spec.spec_files = FileList['spec/**/*_spec.rb']
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new(:spec) do |spec|
+ spec.pattern = 'spec/**/*_spec.rb'
end
-Spec::Rake::SpecTask.new(:rcov) do |spec|
- spec.libs << 'lib' << 'spec'
+RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
end
View
44 lib/tropo-webapi-ruby/tropo-webapi-ruby-helpers.rb
@@ -1,7 +1,7 @@
module Tropo
module Helpers
private
-
+
##
# Method checks for presence of required elements and then builds the action
#
@@ -10,10 +10,12 @@ module Helpers
# @return [Hash] provides the properply built hash for the action
def build_action(action, params)
raise ArgumentError, 'Action requires parameters' if params.nil?
-
+
case action
+ when 'ask'
+ has_params?(params, 'ask', 'name')
when 'choices'
- if params[:mode]
+ if params[:mode]
if params[:mode] != 'dtmf' && params[:mode] != 'speech'
raise ArgumentError, "If mode is provided, only 'dtmf', 'speech' or 'any' is supported"
end
@@ -26,7 +28,7 @@ def build_action(action, params)
has_params?(params, 'record', ['name', 'url'])
when 'start_recording'
has_params?(params, 'start_recording', ['url'])
-
+
# Camelcase this one to be Java friendly
action = 'startRecording'
when 'redirect'
@@ -38,14 +40,14 @@ def build_action(action, params)
when 'transfer'
has_params?(params, 'transfer', 'to')
end
-
+
if action == 'on'
build_elements(params)
else
{ action.to_sym => build_elements(params) }
end
end
-
+
##
# Checks to see if certain parameters are present, and if not raises an error
#
@@ -64,7 +66,7 @@ def has_params?(params, action, names)
raise ArgumentError, "A '#{names}' must be provided to a '#{action}' action" if params[names.to_sym].nil?
end
end
-
+
# Takes a Ruby underscore string and converts to a Java friendly camelized string
#
# @param [String] the string to be camelized
@@ -75,7 +77,7 @@ def camelize(ruby_string)
return_string = return_string + split_string[2].capitalize if split_string[2]
return_string
end
-
+
##
# Creates a nested hash when we have block within a block
#
@@ -97,7 +99,7 @@ def create_nested_on_hash(params)
@nested_on_hash_cnt ||= 0
@nested_on_hash[:on] << params
end
-
+
##
# Creates an on_hash for the on action
#
@@ -105,7 +107,7 @@ def create_nested_on_hash(params)
def create_on_hash
@on_hash ||= { :on => Array.new }
end
-
+
##
# Method builds the elements for each of the actions
#
@@ -122,9 +124,9 @@ def build_elements(params)
end
end
end
-
+
hash = Hash.new
- params.each_pair do |k,v|
+ params.each_pair do |k,v|
if k.to_s.include? "_"
k = camelize k.to_s
k = k.to_sym if k
@@ -133,7 +135,7 @@ def build_elements(params)
end
hash
end
-
+
##
# Takes a Java Camelized string and converts to an underscore string
#
@@ -142,7 +144,7 @@ def build_elements(params)
def decamelize(camel_string)
camel_string.gsub(/[A-Z]/) { |char| '_' + char.downcase }
end
-
+
##
# Formats the @response instance variable to JSON before making it available to the accessor
#
@@ -150,7 +152,7 @@ def decamelize(camel_string)
def render_response
@response.to_json
end
-
+
##
# Determines if there is a voice or recognizer specified, if not set it to the default specified and if not default leave it alone
# this is for the speech synthesis and speech recognition language to use on a say/ask methods
@@ -162,7 +164,7 @@ def set_language(params)
params.merge!({ :voice => @voice }) if params[:voice].nil? && @voice
params
end
-
+
##
# Returns an hash from a collapsed array, using the values of 'key' or 'name' as the collpassed hash key
#
@@ -175,10 +177,10 @@ def transform_array(array)
# Set the key to the value of the respresentative key
key = ele['key'] if ele['key']
key = ele['name'] if ele['name']
-
+
# Merge this new key into the hash
transformed_to_hash.merge!({ key => Hash.new })
-
+
# Then add the corresponding key/values to this new hash
ele.each_pair do |k, v|
if k != 'key' && k != 'name'
@@ -209,7 +211,7 @@ def transform_hash(hash)
# @param[Hash] the newly created hash that contins the properly formatted key
def transform_pair(key, value)
hash = { decamelize(key) => value }
- hash['timestamp'] = Time.parse(value) if hash['timestamp']
+ hash['timestamp'] = Time.parse(value) if hash['timestamp'] && (! hash['timestamp'].is_a?(Time))
if hash['actions']
if hash['actions']['name']
key_name = hash['actions']['name']
@@ -220,11 +222,11 @@ def transform_pair(key, value)
set_session_type(hash) if hash['channel']
hash
end
-
+
##
# Sets the session type instance variables of voice_session and text_session
#
- # @param[Hash] the key, value pair of the channel
+ # @param[Hash] the key, value pair of the channel
# @return nil
def set_session_type(hash)
case hash['channel']
View
2  spec/spec_helper.rb
@@ -3,5 +3,5 @@
%w(rubygems tropo-webapi-ruby hashie rspec rspec/autorun).each { |lib| require lib }
RSpec.configure do |config|
-
+
end
View
250 spec/tropo-webapi-ruby_spec.rb
@@ -1,19 +1,19 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Tropo" do
-
+
# Ask action tests (and alias Prompt)
it "should generate a complete 'ask' JSON document" do
- response = Tropo::Generator.ask({ :name => 'foo',
- :bargein => 'true',
+ response = Tropo::Generator.ask({ :name => 'foo',
+ :bargein => 'true',
:timeout => 30,
:require => 'true' })
JSON.parse(response).should == { "tropo" => [{ "ask" => { "name" => "foo", "bargein" => "true", "timeout" => 30, "require" => "true" } }] }
end
-
+
it "should generate an 'ask' JSON document when a block is passed" do
- response = Tropo::Generator.ask({ :name => 'foo',
- :bargein => 'true',
+ response = Tropo::Generator.ask({ :name => 'foo',
+ :bargein => 'true',
:timeout => 30,
:require => 'true' }) do
say :value => 'Please say your account number'
@@ -25,8 +25,8 @@
# There is currently a feature request to support an on within an ask
#
# it "should generate an 'ask' JSON document when a block is passed with an 'on' action" do
- # response = Tropo::Generator.ask({ :name => 'foo',
- # :bargein => 'true',
+ # response = Tropo::Generator.ask({ :name => 'foo',
+ # :bargein => 'true',
# :timeout => 30,
# :require => 'true' }) do
# say :value => 'Please say your account number'
@@ -35,7 +35,7 @@
# end
# JSON.parse(response).should == {"tropo"=>[{"ask"=>{"name"=>"foo", "say"=>[{"value"=>"Please say your account number"}], "bargein"=>"true", "timeout"=>30, "require"=>"true", "on"=>[{"event"=>"success", "next"=>"/result.json"}], "choices"=>{"value"=>"[5 DIGITS]"}}}]}
# end
-
+
it "should generate an error if an 'ask' is passed without a 'name' parameter" do
begin
response = Tropo::Generator.ask({ :foo => 'bar' })
@@ -43,19 +43,19 @@
err.to_s.should == "A 'name' must be provided to a 'ask' action"
end
end
-
+
# Prompt
it "should generate a complete 'prompt' JSON document" do
- response = Tropo::Generator.prompt({ :name => 'foo',
- :bargein => 'true',
+ response = Tropo::Generator.prompt({ :name => 'foo',
+ :bargein => 'true',
:timeout => 30,
:require => 'true' })
JSON.parse(response).should == { "tropo" => [{ "ask" => { "name" => "foo", "bargein" => "true", "timeout" => 30, "require" => "true" } }] }
end
-
+
it "should generate an 'prompt' JSON document when a block is passed" do
- response = Tropo::Generator.prompt({ :name => 'foo',
- :bargein => 'true',
+ response = Tropo::Generator.prompt({ :name => 'foo',
+ :bargein => 'true',
:timeout => 30,
:require => 'true' }) do
say :value => 'Please say your account number'
@@ -63,7 +63,7 @@
end
JSON.parse(response).should == {"tropo"=>[{"ask"=>{"name"=>"foo", "say"=>[{"value"=>"Please say your account number"}], "bargein"=>"true", "timeout"=>30, "require"=>"true", "choices"=>{"value"=>"[5 DIGITS]"}}}]}
end
-
+
it "should generate an error if an 'prompt' is passed without a 'name' parameter" do
begin
response = Tropo::Generator.prompt({ :foo => 'bar' })
@@ -71,13 +71,13 @@
err.to_s.should == "A 'name' must be provided to a 'ask' action"
end
end
-
+
# Choices tests
it "should generate a standard 'choices' JSON document" do
response = Tropo::Generator.choices({ :value => '[5 DIGITS]' })
JSON.parse(response).should == { 'tropo' => [{ 'choices' => { 'value' => '[5 DIGITS]' } }] }
end
-
+
it "should raise an error if a 'choices' passes an unspported mode" do
begin
response = Tropo::Generator.choices({ :value => '[5 DIGITS]', :mode => 'frootloops' })
@@ -85,25 +85,25 @@
err.to_s.should == "If mode is provided, only 'dtmf', 'speech' or 'any' is supported"
end
end
-
+
it "should generate a standard 'choices' JSON document with a mode" do
response = Tropo::Generator.choices({ :value => '[5 DIGITS]', :mode => 'dtmf' })
JSON.parse(response).should == { 'tropo' => [{ 'choices' => { 'value' => '[5 DIGITS]', 'mode' => 'dtmf' } }] }
end
-
+
# Conference action tests
it "should generate a complete 'conference' JSON document" do
- response = Tropo::Generator.conference({ :name => 'foo',
- :id => '1234',
+ response = Tropo::Generator.conference({ :name => 'foo',
+ :id => '1234',
:mute => false,
:send_tones => false,
:exit_tone => '#' })
JSON.parse(response).should == {"tropo"=>[{"conference"=>{"name"=>"foo", "mute"=>false, "sendTones"=>false, "id"=>"1234", "exitTone"=>"#"}}]}
end
-
+
it "should generate a complete 'conference' JSON document when a block is passed" do
- response = Tropo::Generator.conference({ :name => 'foo',
- :id => '1234',
+ response = Tropo::Generator.conference({ :name => 'foo',
+ :id => '1234',
:mute => false,
:send_tones => false,
:exit_tone => '#' }) do
@@ -112,7 +112,7 @@
end
JSON.parse(response).should == {"tropo"=>[{"conference"=>{"name"=>"foo", "mute"=>false, "id"=>"1234", "exitTone"=>"#", "sendTones"=>false, "on"=>[{"say"=>[{"value"=>"Welcome to the conference"}], "event"=>"join"}, {"say"=>[{"value"=>"Someone has left the conference"}], "event"=>"leave"}]}}]}
end
-
+
it "should generate an error if an 'conference' is passed without a 'name' parameter" do
begin
response = Tropo::Generator.conference({ :foo => 'bar' })
@@ -120,7 +120,7 @@
err.to_s.should == "A 'name' must be provided to a 'conference' action"
end
end
-
+
it "should generate an error if an 'conference' is passed without an 'id' parameter" do
begin
response = Tropo::Generator.conference({ :name => 'bar' })
@@ -128,23 +128,23 @@
err.to_s.should == "A 'id' must be provided to a 'conference' action"
end
end
-
+
# Hangup action tests and Disconnect alias
it "should generate a JSON document with a 'hangup' action" do
response = Tropo::Generator.hangup
JSON.parse(response).should == {"tropo"=>[{"hangup"=>nil}]}
end
-
+
it "should generate a JSON document with a 'disconnect' action" do
response = Tropo::Generator.disconnect
JSON.parse(response).should == {"tropo"=>[{"hangup"=>nil}]}
end
-
+
it "should generate a standard 'on' JSON document" do
response = Tropo::Generator.on({ :event => 'hangup', :next => 'myresource' })
JSON.parse(response).should == { "tropo" => [{ "on" =>{ "event" => "hangup", "next" => "myresource" } }] }
end
-
+
# On tests
it "should generate a an error of an 'on' document does not pass an event param" do
begin
@@ -153,7 +153,7 @@
err.to_s.should == "A 'event' must be provided to a 'on' action"
end
end
-
+
it "should generate a an error of an 'on' document does not pass an event param" do
begin
response = Tropo::Generator.on({ :event => 'bar' })
@@ -161,20 +161,20 @@
err.to_s.should == "A 'next' resource must be provided"
end
end
-
+
# Record action tests
it "should generate a complete 'record' JSON document" do
- response = Tropo::Generator.record({ :name => 'foo',
- :url => 'http://sendme.com/tropo',
+ response = Tropo::Generator.record({ :name => 'foo',
+ :url => 'http://sendme.com/tropo',
:beep => true,
:send_tones => false,
:exit_tone => '#' })
JSON.parse(response).should == {"tropo"=>[{"record"=>{"name"=>"foo", "beep"=>true, "url"=>"http://sendme.com/tropo", "exitTone"=>"#", "sendTones"=>false}}]}
end
-
+
it "should generate a complete 'record' JSON document when a block is passed" do
- response = Tropo::Generator.record({ :name => 'foo',
- :url => 'http://sendme.com/tropo',
+ response = Tropo::Generator.record({ :name => 'foo',
+ :url => 'http://sendme.com/tropo',
:beep => true,
:send_tones => false,
:exit_tone => '#' }) do
@@ -183,7 +183,7 @@
end
JSON.parse(response).should == {"tropo"=>[{"record"=>{"name"=>"foo", "say"=>[{"value"=>"Please say your account number"}], "beep"=>true, "url"=>"http://sendme.com/tropo", "sendTones"=>false, "exitTone"=>"#", "choices"=>{"value"=>"[5 DIGITS]"}}}]}
end
-
+
it "should generate an error if an 'record' is passed without a 'name' parameter" do
begin
response = Tropo::Generator.record({ :foo => 'bar' })
@@ -191,7 +191,7 @@
err.to_s.should == "A 'name' must be provided to a 'record' action"
end
end
-
+
it "should generate an error if an 'record' is passed without an 'url' parameter" do
begin
response = Tropo::Generator.record({ :name => 'bar' })
@@ -199,7 +199,7 @@
err.to_s.should == "A 'url' must be provided to a 'record' action"
end
end
-
+
it "should generate an error if an 'record' is passed without an invalid 'url' parameter" do
begin
response = Tropo::Generator.record({ :name => 'bar',
@@ -208,19 +208,19 @@
err.to_s.should == "The 'url' paramater must be a valid URL"
end
end
-
+
it "should accept a valid email address when a 'record' action is called" do
response = Tropo::Generator.record({ :name => 'bar',
:url => 'foo@bar.com' })
JSON.parse(response).should == JSON.parse("{\"tropo\":[{\"record\":{\"url\":\"foo@bar.com\",\"name\":\"bar\"}}]}")
end
-
+
# Redirect action tests
it "should generate a JSON document with a 'redirect' action" do
response = Tropo::Generator.redirect({ :to => 'sip:1234', :from => '4155551212' })
JSON.parse(response).should == {"tropo"=>[{"redirect"=>{"from"=>"4155551212", "to"=>"sip:1234"}}]}
end
-
+
it "should generate an error if a 'redirect' action is included in a block" do
begin
response = Tropo::Generator.conference(:name => 'foobar', :id => 1234) do
@@ -230,7 +230,7 @@
err.to_s.should == 'Redirect should only be used alone and before the session is answered, use transfer instead'
end
end
-
+
it "should generate an error when no 'to' is passed to a 'redirect' action" do
begin
response = Tropo::Generator.redirect
@@ -238,18 +238,18 @@
err.to_s.should == "A 'to' must be provided to a 'redirect' action"
end
end
-
+
# Reject action tests
it "should generate a JSON document with a 'reject' action" do
response = Tropo::Generator.reject
JSON.parse(response).should == {"tropo"=>[{"reject"=>nil}]}
end
-
+
# Say action tests
it "should generate a standard 'say' JSON document when a stiring is passed" do
JSON.parse(Tropo::Generator.say('1234')).should == { "tropo" => [{ "say" => [{ "value" => "1234" }] }] }
end
-
+
it "should generate an error if I try to pass an integer to a 'say' action" do
begin
Tropo::Generator.say(1234)
@@ -257,16 +257,16 @@
err.to_s.should == "An invalid paramater type Fixnum has been passed"
end
end
-
+
it "should generate a standard 'say' JSON document" do
JSON.parse(Tropo::Generator.say({ :value => '1234' })).should == { "tropo" => [{ "say" => [{ "value" => "1234" }] }] }
end
-
+
it "should generate a 'say' JSON document when an array of values is passed" do
response = Tropo::Generator.say([{ :value => '1234' }, { :value => 'abcd', :event => 'nomatch:1' }])
JSON.parse(response).should == { "tropo" => [{ "say" => [{ "value" => "1234" }, { "value" => "abcd", "event"=>"nomatch:1" }] }] }
end
-
+
it "should generate an error if no 'value' key is passed to a 'say' request" do
begin
response = Tropo::Generator.say({ :name => 'foo' })
@@ -274,7 +274,7 @@
err.to_s.should == "A 'value' must be provided to a 'say' action"
end
end
-
+
it "should generate a JSON document with a 'say' and an 'on'" do
result = Tropo::Generator.new do
say :value => 'blah'
@@ -282,13 +282,13 @@
end
JSON.parse(result.response).should == {"tropo"=>[{"say"=>[{"value"=>"blah"}]}, {"on"=>{"event"=>"error", "next"=>"error.json"}}]}
end
-
+
# Start & Stop Recording actions tests
it "should generate a JSON document with a 'start_recording' action" do
response = Tropo::Generator.start_recording(:url => 'http://postrecording.com/tropo')
JSON.parse(response).should == {"tropo"=>[{"startRecording"=>{"url"=>"http://postrecording.com/tropo"}}]}
end
-
+
it "should generate a JSON document with a 'start_call_recording' action" do
response = Tropo::Generator.start_call_recording(:url => 'http://postrecording.com/tropo')
JSON.parse(response).should == {"tropo"=>[{"startRecording"=>{"url"=>"http://postrecording.com/tropo"}}]}
@@ -298,18 +298,18 @@
response = Tropo::Generator.stop_call_recording
JSON.parse(response).should == {"tropo"=>[{"stopRecording"=>nil}]}
end
-
+
it "should generate a JSON document with a 'stoprecording' action" do
response = Tropo::Generator.stop_recording
JSON.parse(response).should == {"tropo"=>[{"stopRecording"=>nil}]}
end
-
+
# Transfer action tests
it "should generate a JSON document with a 'transfer' action" do
response = Tropo::Generator.transfer(:to => 'tel:+14157044517')
JSON.parse(response).should == {"tropo"=>[{"transfer"=>{"to"=>"tel:+14157044517"}}]}
end
-
+
# Transfer action tests
it "should generate a JSON document with a 'transfer' action with an 'on' and 'choices' actions" do
response = Tropo::Generator.transfer(:to => 'tel:+14157044517') do
@@ -318,7 +318,7 @@
end
JSON.parse(response).should == {"tropo"=>[{"transfer"=>{"to"=>"tel:+14157044517", "choices"=>{"value"=>"[5 DIGITS]"}, "on"=>[{"event"=>"unbounded", "next"=>"/error.json"}]}}]}
end
-
+
it "should generate an error if no 'to' key is passed to a 'transfer' request" do
begin
response = Tropo::Generator.transfer
@@ -326,7 +326,7 @@
err.to_s.should == "A 'to' must be provided to a 'transfer' action"
end
end
-
+
# General tests
it "should generate a JSON document when a block is passed" do
result = Tropo::Generator.new do
@@ -335,23 +335,23 @@
end
JSON.parse(result.response).should == {"tropo"=>[{"say"=>[{"value"=>"1234"}, {"value"=>"abcd", "event"=>"nomatch:1"}]}, {"say"=>[{"value"=>"0987"}, {"value"=>"zyxw", "event"=>"nomatch:2"}]}]}
end
-
+
it "should build a Ruby hash object when a session arrives in JSON" do
json_session = "{\"session\":{\"id\":\"dih06n\",\"accountId\":\"33932\",\"timestamp\":\"2010-01-19T23:18:48.562Z\",\"userType\":\"HUMAN\",\"to\":{\"id\":\"tropomessaging@bot.im\",\"name\":\"unknown\",\"channel\":\"TEXT\",\"network\":\"JABBER\"},\"from\":{\"id\":\"john_doe@gmail.com\",\"name\":\"unknown\",\"channel\":\"TEXT\",\"network\":\"JABBER\"}}}"
hash = Tropo::Generator.parse(json_session)
hash[:session][:timestamp] == Time.parse('2010-01-19T18:27:46.852-05:00')
end
-
+
it "should build a Ruby hash object when a result arrives in JSON" do
json_result = "{\"result\":{\"sessionId\":\"sessionId\",\"callState\":\"ANSWERED\",\"sessionDuration\":10,\"sequence\":1,\"complete\":true,\"error\":\"error\",\"properties\":[{\"key\":\"foo\",\"value\":\"bar\"},{\"key\":\"charlie\",\"value\":\"foxtrot\"}],\"actions\":{\"name\":\"pin\",\"attempts\":1,\"disposition\":\"SUCCESS\",\"confidence\":100,\"interpretation\":\"12345\",\"utterance\":\"1 2 3 4 5\"}}}"
Tropo::Generator.parse(json_result).should == Hashie::Mash.new({:result=>{:session_id=>"sessionId", :properties=>{:foo=>{:value=>"bar"}, :charlie=>{:value=>"foxtrot"}}, :complete=>true, :call_state=>"ANSWERED", :actions=>{:pin=>{:disposition=>"SUCCESS", :utterance=>"1 2 3 4 5", :attempts=>1, :interpretation=>"12345", :confidence=>100}}, :session_duration=>10, :error=>"error", :sequence=>1}})
end
-
+
it "should build a ruby hash object when a realworld JSON string arrives" do
json_result = "{\"result\":{\"sessionId\":\"CCFD9C86-1DD1-11B2-B76D-B9B253E4B7FB@161.253.55.20\",\"callState\":\"ANSWERED\",\"sessionDuration\":2,\"sequence\":1,\"complete\":true,\"error\":null,\"actions\":[{\"name\":\"zip\",\"attempts\":1,\"disposition\":\"SUCCESS\",\"confidence\":100,\"interpretation\":\"12345\",\"utterance\":\"1 2 3 4 5\"},{\"name\":\"days\",\"attempts\":1,\"disposition\":\"SUCCESS\",\"confidence\":100,\"interpretation\":\"1\",\"utterance\":\"1\"}]}}"
Tropo::Generator.parse(json_result).should == Hashie::Mash.new({:result=>{:call_state=>"ANSWERED", :complete=>true, :actions=>{:zip=>{:disposition=>"SUCCESS", :utterance=>"1 2 3 4 5", :attempts=>1, :interpretation=>"12345", :confidence=>100}, :days=>{:disposition=>"SUCCESS", :utterance=>"1", :attempts=>1, :interpretation=>"1", :confidence=>100}}, :session_duration=>2, :sequence=>1, :session_id=>"CCFD9C86-1DD1-11B2-B76D-B9B253E4B7FB@161.253.55.20", :error=>nil}})
end
-
+
it "should see an object delcared outside of a block" do
@@session = 'foobar'
result = Tropo::Generator.new do
@@ -361,7 +361,7 @@
end
@@new_session.should == 'foobar'
end
-
+
it "should see an object passed into the block" do
session = 'foobar'
result = Tropo::Generator.new(session) do
@@ -370,20 +370,20 @@
on :event => 'error', :next => 'error.json'
end
end
-
+
it "should allow you to create a Tropo::Generator object and build up a JSON request with two says" do
tropo = Tropo::Generator.new
tropo.say('foo')
tropo.say('bar')
tropo.response.should == "{\"tropo\":[{\"say\":[{\"value\":\"foo\"}]},{\"say\":[{\"value\":\"bar\"}]}]}"
end
-
+
it "should allow you to create a Tropo::Generator object and build up a JSON request with: a say, an on and a record" do
tropo = Tropo::Generator.new
tropo.say 'Welcome to the app'
tropo.on :event => 'hangup', :next => '/hangup.json'
- tropo.record({ :name => 'foo',
- :url => 'http://sendme.com/tropo',
+ tropo.record({ :name => 'foo',
+ :url => 'http://sendme.com/tropo',
:beep => true,
:send_tones => false,
:exit_tone => '#' }) do
@@ -392,13 +392,13 @@
end
JSON.parse(tropo.response).should == {"tropo"=>[{"say"=>[{"value"=>"Welcome to the app"}]}, {"on"=>{"event"=>"hangup", "next"=>"/hangup.json"}}, {"record"=>{"name"=>"foo", "say"=>[{"value"=>"Please say your account number"}], "beep"=>true, "url"=>"http://sendme.com/tropo", "sendTones"=>false, "exitTone"=>"#", "choices"=>{"value"=>"[5 DIGITS]"}}}]}
end
-
+
it "should allow you to reset the object to a fresh response after building a response first" do
tropo = Tropo::Generator.new
tropo.say 'Welcome to the app'
tropo.on :event => 'hangup', :next => '/hangup.json'
- tropo.record({ :name => 'foo',
- :url => 'http://sendme.com/tropo',
+ tropo.record({ :name => 'foo',
+ :url => 'http://sendme.com/tropo',
:beep => true,
:send_tones => false,
:exit_tone => '#' }) do
@@ -409,19 +409,19 @@
tropo.reset
tropo.response.should == "{\"tropo\":[]}"
end
-
+
it "should build a Ruby hash object when a session arrives in JSON with a proper Ruby Time object" do
json_session = "{\"session\":{\"id\":\"dih06n\",\"accountId\":\"33932\",\"timestamp\":\"2010-01-19T23:18:48.562Z\",\"userType\":\"HUMAN\",\"to\":{\"id\":\"tropomessaging@bot.im\",\"name\":\"unknown\",\"channel\":\"TEXT\",\"network\":\"JABBER\"},\"from\":{\"id\":\"john_doe@gmail.com\",\"name\":\"unknown\",\"channel\":\"TEXT\",\"network\":\"JABBER\"}}}"
hash = Tropo::Generator.parse(json_session)
hash[:session][:timestamp].should == Time.parse("2010-01-19T23:18:48.562Z")
end
-
+
it "should build a Ruby hash object when a result arrives in JSON with one action returned in an array" do
json_result = "{\"result\":{\"sessionId\":\"CCFD9C86-1DD1-11B2-B76D-B9B253E4B7FB@161.253.55.20\",\"callState\":\"ANSWERED\",\"sessionDuration\":2,\"sequence\":1,\"complete\":true,\"error\":null,\"actions\":{\"name\":\"zip\",\"attempts\":1,\"disposition\":\"SUCCESS\",\"confidence\":100,\"interpretation\":\"12345\",\"utterance\":\"1 2 3 4 5\"}}}"
hash = Tropo::Generator.parse(json_result)
hash.should == Hashie::Mash.new({:result=>{:call_state=>"ANSWERED", :complete=>true, :actions=>{:zip=>{:utterance=>"1 2 3 4 5", :attempts=>1, :interpretation=>"12345", :confidence=>100, :disposition=>"SUCCESS"}}, :session_id=>"CCFD9C86-1DD1-11B2-B76D-B9B253E4B7FB@161.253.55.20", :session_duration=>2, :error=>nil, :sequence=>1}})
end
-
+
it "should build a Hashie object when a result arrives in JSON" do
json_result = "{\"result\":{\"sessionId\":\"CCFD9C86-1DD1-11B2-B76D-B9B253E4B7FB@161.253.55.20\",\"callState\":\"ANSWERED\",\"sessionDuration\":2,\"sequence\":1,\"complete\":true,\"error\":null,\"actions\":{\"name\":\"zip\",\"attempts\":1,\"disposition\":\"SUCCESS\",\"confidence\":100,\"interpretation\":\"12345\",\"utterance\":\"1 2 3 4 5\"}}}"
hash = Tropo::Generator.parse(json_result)
@@ -429,7 +429,7 @@
hash[:result][:call_state].should == 'ANSWERED'
hash['result']['call_state'].should == 'ANSWERED'
end
-
+
it "should generate valid JSON when a startRecording is used" do
t = Tropo::Generator.new
t.on :event => 'error', :next => '/error.json' # For fatal programming errors. Log some details so we can fix it
@@ -444,14 +444,14 @@
t.say "http://denalidomain.com/music/keepers/HappyHappyBirthdaytoYou-Disney.mp3"
JSON.parse(t.response).should == {"tropo"=>[{"on"=>{"event"=>"error", "next"=>"/error.json"}}, {"on"=>{"event"=>"hangup", "next"=>"/hangup.json"}}, {"on"=>{"event"=>"continue", "next"=>"/next.json"}}, {"say"=>[{"value"=>"Hello"}]}, {"startRecording"=>{"url"=>"http://heroku-voip.marksilver.net/post_audio_to_s3?filename=foo.wav&unique_id=bar"}}, {"say"=>[{"value"=>"You are now on the record."}]}, {"say"=>[{"value"=>"Go ahead, sing-along."}]}, {"say"=>[{"value"=>"http://denalidomain.com/music/keepers/HappyHappyBirthdaytoYou-Disney.mp3"}]}]}
end
-
+
it "should generate a voice_session true if a JSON session is received that is a channel of 'VOICE'" do
tropo = Tropo::Generator.new
tropo.parse "{\"session\":{\"id\":\"0-13c4-4b563da3-7aecefda-46af-1d10bdd0\",\"accountId\":\"33932\",\"timestamp\":\"2010-01-19T23:18:00.854Z\",\"userType\":\"HUMAN\",\"to\":{\"id\":\"9991427589\",\"name\":\"unknown\",\"channel\":\"VOICE\",\"network\":\"PSTN\"},\"from\":{\"id\":\"jsgoecke\",\"name\":\"unknown\",\"channel\":\"VOICE\",\"network\":\"PSTN\"}}}"
tropo.voice_session.should == true
tropo.text_session.should == false
end
-
+
it "should generate a text_session true if a JSON session is received that is a channel of 'TEXT'" do
tropo = Tropo::Generator.new
tropo.parse "{\"session\":{\"id\":\"dih06n\",\"accountId\":\"33932\",\"timestamp\":\"2010-01-19T23:18:48.562Z\",\"userType\":\"HUMAN\",\"to\":{\"id\":\"tropomessaging@bot.im\",\"name\":\"unknown\",\"channel\":\"TEXT\",\"network\":\"JABBER\"},\"from\":{\"id\":\"john_doe@gmail.com\",\"name\":\"unknown\",\"channel\":\"TEXT\",\"network\":\"JABBER\"}}}"
@@ -461,7 +461,7 @@
it "should generate a valid JSON string for a call method" do
json_result = "{\"tropo\":[{\"call\":{\"recording\":{\"password\":\"passwd\",\"username\":\"jose\",\"method\":\"POST\",\"url\":\"http://foobar\",\"format\":\"audio/mp3\"},\"timeout\":10,\"network\":\"SMS\",\"channel\":\"TEXT\",\"to\":\"foo\",\"from\":\"bar\",\"headers\":{\"foo\":\"foo\",\"bar\":\"bar\"},\"answerOnMedia\":false}}]}"
- tropo = Tropo::Generator.call({ :to => 'foo',
+ tropo = Tropo::Generator.call({ :to => 'foo',
:from => 'bar',
:network => 'SMS',
:channel => 'TEXT',
@@ -475,10 +475,10 @@
:password => 'passwd' } })
JSON.parse(tropo).should == JSON.parse(json_result)
end
-
+
it "should generate a valid JSON string for a message method" do
hash_result = {"tropo"=>[{"message"=>{"say"=>[{"value"=>"Please say your account number"}], "from"=>"bar", "timeout"=>10, "to"=>"foo", "network"=>"SMS", "answerOnMedia"=>false, "channel"=>"TEXT", "recording"=>{"format"=>"audio/mp3", "method"=>"POST", "url"=>"http://foobar", "username"=>"jose", "password"=>"passwd"}, "headers"=>{"foo"=>"foo", "bar"=>"bar"}}}]}
- tropo = Tropo::Generator.message({ :to => 'foo',
+ tropo = Tropo::Generator.message({ :to => 'foo',
:from => 'bar',
:network => 'SMS',
:channel => 'TEXT',
@@ -491,14 +491,14 @@
:username => 'jose',
:password => 'passwd' } }) do
say :value => 'Please say your account number'
- end
+ end
JSON.parse(tropo).should == hash_result
end
-
+
it "should generate a valid JSON string for a record method with a transcription request" do
hash_result = {"tropo"=>[{"record"=>{"name"=>"foo", "transcription"=>{"email_format"=>"encoded", "url"=>"mailto:jose@voxeo.com", "id"=>"bling"}, "say"=>[{"value"=>"Please say your account number"}], "beep"=>true, "url"=>"http://sendme.com/tropo", "exitTone"=>"#", "sendTones"=>false, "choices"=>{"value"=>"[5 DIGITS]"}}}]}
- tropo = Tropo::Generator.record({ :name => 'foo',
- :url => 'http://sendme.com/tropo',
+ tropo = Tropo::Generator.record({ :name => 'foo',
+ :url => 'http://sendme.com/tropo',
:beep => true,
:send_tones => false,
:transcription => { :id => 'bling',
@@ -507,20 +507,20 @@
:exit_tone => '#' }) do
say :value => 'Please say your account number'
choices :value => '[5 DIGITS]'
- end
+ end
JSON.parse(tropo).should == hash_result
end
-
+
it "should properly generate a JSON document when calling an ask with says as hash elements rather than as methods" do
hash_result = {"tropo"=>[{"ask"=>{"name"=>"donate_to_id", "say"=>[{"event"=>"timeout", "value"=>"Sorry, I did not hear anything."}, {"event"=>"nomatch:1 nomatch:2 nomatch:3", "value"=>"Sorry, that wasn't a valid answer. You can press or say 1 for 'yes', or 2 for 'no'."}, {"value"=>"You chose organization foobar. Are you ready to donate to them? If you say no, I will tell you a little more about the organization."}, {"event"=>"nomatch:3", "value"=>"This is your last attempt."}], "bargein"=>true, "silenceTimeout"=>10, "timeout"=>10, "attempts"=>4, "choices"=>{"value"=>"true(1,yes,sure,affirmative), false(2,no,no thank you,negative), 0(0,help,i do not know, agent, operator, assistance, representative, real person, human), 9(9,quit,stop,shut up)"}}}]}
help_stop_choices = "0(0,help,i do not know, agent, operator, assistance, representative, real person, human), 9(9,quit,stop,shut up)"
yes_no_choices = "true(1,yes,sure,affirmative), false(2,no,no thank you,negative), " + help_stop_choices
-
+
t = Tropo::Generator.new
- t.ask :name => 'donate_to_id',
- :bargein => true,
- :timeout => 10,
- :silence_timeout => 10,
+ t.ask :name => 'donate_to_id',
+ :bargein => true,
+ :timeout => 10,
+ :silence_timeout => 10,
:attempts => 4,
:say => [{:event => "timeout", :value => "Sorry, I did not hear anything."},
{:event => "nomatch:1 nomatch:2 nomatch:3", :value => "Sorry, that wasn't a valid answer. You can press or say 1 for 'yes', or 2 for 'no'."},
@@ -529,28 +529,28 @@
:choices => { :value => yes_no_choices}
JSON.parse(t.response).should == hash_result
end
-
+
it "should set the voice variable when called" do
t = Tropo::Generator.new
t.voice.should == nil
-
+
t = Tropo::Generator.new(:voice => 'barnie')
t.voice.should == 'barnie'
-
+
t = Tropo::Generator.new
t.voice = 'barnie'
t.voice.should == 'barnie'
end
-
+
it "should handle the setting of the voice parameter based on defaults" do
t = Tropo::Generator.new
t.say 'Hi there!'
JSON.parse(t.response)['tropo'][0]['say'][0]['voice'].should == nil
-
+
t = Tropo::Generator.new
t.say 'Hi there!', :voice => 'barnie'
JSON.parse(t.response)['tropo'][0]['say'][0]['voice'].should == 'barnie'
-
+
t = Tropo::Generator.new(:voice => 'barnie')
t.say 'Hi there!'
t.say 'Wow!'
@@ -562,7 +562,7 @@
t.say 'Wow!', :voice => 'jack'
JSON.parse(t.response)['tropo'][0]['say'][0]['voice'].should == 'barnie'
JSON.parse(t.response)['tropo'][1]['say'][0]['voice'].should == 'jack'
-
+
t = Tropo::Generator.new
t.voice = 'barnie'
t.say 'Hi there!'
@@ -570,82 +570,74 @@
JSON.parse(t.response)['tropo'][0]['say'][0]['voice'].should == 'barnie'
JSON.parse(t.response)['tropo'][1]['say'][0]['voice'].should == 'jack'
end
-
+
it "should set the recognizer variable when called" do
t = Tropo::Generator.new
t.recognizer.should == nil
-
+
t = Tropo::Generator.new(:recognizer => 'fr-fr')
t.recognizer.should == 'fr-fr'
-
+
t = Tropo::Generator.new
t.recognizer = 'fr-fr'
t.recognizer.should == 'fr-fr'
end
-
+
it "should handle the setting of the recognizer parameter based on defaults" do
t = Tropo::Generator.new
- t.ask({ :name => 'foo',
- :bargein => 'true',
+ t.ask({ :name => 'foo',
+ :bargein => 'true',
:timeout => 30,
:require => 'true' })
JSON.parse(t.response)['tropo'][0]['ask']['recognizer'].should == nil
-
+
t = Tropo::Generator.new(:recognizer => 'fr-fr')
- t.ask({ :name => 'foo',
- :bargein => 'true',
+ t.ask({ :name => 'foo',
+ :bargein => 'true',
:timeout => 30,
:require => 'true' })
JSON.parse(t.response)['tropo'][0]['ask']['recognizer'].should == 'fr-fr'
-
+
t = Tropo::Generator.new
t.recognizer = 'fr-fr'
- t.ask({ :name => 'foo',
- :bargein => 'true',
+ t.ask({ :name => 'foo',
+ :bargein => 'true',
:timeout => 30,
:require => 'true' })
JSON.parse(t.response)['tropo'][0]['ask']['recognizer'].should == 'fr-fr'
-
+
t = Tropo::Generator.new
t.recognizer = 'fr-fr'
- t.ask({ :name => 'foo',
- :bargein => 'true',
+ t.ask({ :name => 'foo',
+ :bargein => 'true',
:timeout => 30,
:require => 'true' })
- t.ask({ :name => 'foo',
- :bargein => 'true',
+ t.ask({ :name => 'foo',
+ :bargein => 'true',
:timeout => 30,
:require => 'true',
:recognizer => 'de-de' })
JSON.parse(t.response)['tropo'][0]['ask']['recognizer'].should == 'fr-fr'
JSON.parse(t.response)['tropo'][1]['ask']['recognizer'].should == 'de-de'
end
-
+
it "should parse a JSON string or a Ruby Hash the same" do
json_session = "{\"session\":{\"id\":\"dih06n\",\"accountId\":\"33932\",\"timestamp\":\"2010-01-19T23:18:48.562Z\",\"userType\":\"HUMAN\",\"to\":{\"id\":\"tropomessaging@bot.im\",\"name\":\"unknown\",\"channel\":\"TEXT\",\"network\":\"JABBER\"},\"from\":{\"id\":\"john_doe@gmail.com\",\"name\":\"unknown\",\"channel\":\"TEXT\",\"network\":\"JABBER\"}}}"
tropo = Tropo::Generator.parse json_session
tropo.session.user_type.should == 'HUMAN'
-
+
tropo = Tropo::Generator.parse(JSON.parse(json_session))
tropo.session.user_type.should == 'HUMAN'
end
-
+
it "should return a hash of the response object" do
result = Tropo::Generator.new do
say [{ :value => '1234' }, { :value => 'abcd', :event => "nomatch:1" }]
say [{ :value => '0987' }, { :value => 'zyxw', :event => "nomatch:2" }]
end
- result.to_hash.should == { :tropo => [{ :say => [{ :value => "1234" },
- { :event => "nomatch:1", :value => "abcd" }] },
- { :say => [{ :value => "0987" },
+ result.to_hash.should == { :tropo => [{ :say => [{ :value => "1234" },
+ { :event => "nomatch:1", :value => "abcd" }] },
+ { :say => [{ :value => "0987" },
{ :event => "nomatch:2", :value => "zyxw" }] }] }
end
-
- it "should not require a name in an ask" do
- t = Tropo::Generator.new
- t.ask({ :bargein => 'true',
- :timeout => 30,
- :require => 'true' })
- JSON.parse(t.response)['tropo'][0]['ask']['timeout'].should == 30
- end
end
Please sign in to comment.
Something went wrong with that request. Please try again.