SlideShare ist ein Scribd-Unternehmen logo
1 von 34
Downloaden Sie, um offline zu lesen
RUBY ON THE PHONE
  Mat Schaffer, Ruby Nation 2011
MAT
SCHAFFER
github.com/matschaffer

    @matschaffer
RAILS – JAVASCRIPT
       (for hire)
CALLME
• Provider   comparison

• Making    phone calls

• Handling   Call flow

• SMS

• Testing
XML REST API             JSON REST API

 you generate TwiML      they host a JVM script

 just phone and SMS     IM and Twitter integration

  1¢/2¢ per minute           3¢ per minute

$1 per month per line    $3 per month per line
PLACING CALLS
https://github.com/webficient/twilio
require 'rubygems'
require 'twilio'

Twilio.connect(ENV['TWILIO_SID'], ENV['TWILIO_TOKEN'])
Twilio::Call.make('+12155551213',
                  '+12155551234',
                  'http://myapp.com')


    From*
         To
                      What do to
> curl -d From=+12155551212 
       -d To=+12155551234 
       -d Url=http://myapp.com 
       -u $TWILIO_SID:$TWILIO_TOKEN 
https://api.twilio.com/2010-04-01/Accounts/
$TWILIO_SID/Calls
POST https://twilio/call
Url=http://me/calls




                           POST https://me/calls
                           CallSid=CAc7ca63fb9...
<?xml version="1.0" encoding="UTF-8" ?>
<Response>
  <Say>Hello Ruby Nation</Say>
</Response>
INCOMING CALLS
require 'rubygems'
require 'twilio'

Twilio.connect(ENV['TWILIO_SID'], ENV['TWILIO_TOKEN'])
resp = Twilio::AvailablePhoneNumbers.search_local(:postal_code => '20191')
numbers = resp['TwilioResponse']['AvailablePhoneNumbers']['AvailablePhoneNumber']

my_number = numbers.first
Twilio::IncomingPhoneNumber.create(:PhoneNumber => my_number['PhoneNumber'],
                                   :VoiceUrl => 'http://myapp.com')
For a good time call (540) 318-2266

 post '/' do
   return Twilio::Verb.new do |v|
     v.say "Welcome to the party!"
     v.dial do
       v.conference 'rubynation_partyline'
     end
   end.response
 end




<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say>Welcome to the party!</Say>
  <Dial>
    <Conference>rubynation_partyline</Conference>
  </Dial>
</Response>
TWIML
VERBS
OUTPUT   <Play> <Say>


 INPUT   <Gather> <Record>

         <Dial>
 CALLS   <Number> <Conference>

         <Hangup> <Reject>
OTHERS   <Pause> <Redirect> <Sms>
WHOOPS! RAILS ISSUES

• skip_before_filter   :verify_authenticity_token

• be   careful about response types (use *.xml or filter)

• tough   to fit in RESTful model
CALL FLOW
TWILIO LOOP

1.Post initial call information with new CallSid

2.Perform action

3.Post action’s result with original CallSid

4.Repeat
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Gather action="/calls/response.xml">
  " <Say>Hi there, press a number</Say>
  </Gather>
</Response>




                 <?xml version="1.0" encoding="UTF-8"?>
                 <Response>
                   <Say>Thanks for your input</Say>
                 </Response>
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial action="/calls/complete.xml">
  " <Number url="/calls/prompt.xml?connection_id=1">2155551212</Number>
  </Dial>
</Response>


                           <?xml version="1.0" encoding="UTF-8"?>
                           <Response>
                             <Say>
                               Here’s what the called person hears
                             </Say>
                           </Response>



          <?xml version="1.0" encoding="UTF-8"?>
          <Response>
            <Say>The caller hears this after the call</Say>
          </Response>
SMS
SENDING

require 'rubygems'
require 'twilio'

Twilio.connect(ENV['TWILIO_SID'], ENV['TWILIO_TOKEN'])
Twilio::Sms.message('+12155551213',
                    '+12155551234',
                    "Hello sms")
RECEIVING
  require 'rubygems'
  require 'sinatra'
  require 'twilio'

  post '/sms' do
    return Twilio::Verb.new do |v|
      v.sms "Ohai #{params[:From]}"
    end.response
  end


<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Sms>Ohai +12155551212</Sms>
</Response>
TESTING
REST CALL TESTING


• Mocking: Mocha, Rspec, etc

• HTTP   Mocking: VCR, Artiface, FakeWeb, WebMock
require 'rubygems'
require 'sinatra'

class FakeTwilio < Sinatra::Base
  def fixture(resource)
    resource.gsub!('/', '-')
    Rails.root.join('test', 'support', 'twilio_responses',
                    "#{resource}-GET.xml").read
  end

  get '/2010-04-01/Accounts/:sid/*' do |sid, resource|
    fixture(resource)
  end

  post '/2010-04-01/Accounts/:sid/*' do |sid, resource|
    fixture(resource)
  end
end



            Artifice.activate_with(FakeTwilio)
ARTIFICE + CUCUMBER
    Kinda sucked. got better?
    module Selenium
      Net = ::Net.dup
      module Net
        HTTP = Artifice::NET_HTTP
      end
    end

    class Capybara::Server
      Net = ::Net.dup
      module Net
        HTTP = Artifice::NET_HTTP
      end
    end
TWIML TESTING

  test "new should omit connected connections" do
    @connection.connect!
    post :new, :CallSid => @sid
    assert_match "Number", response.body
    assert_not_match @connection.number, response.body
  end



             Basic match testing
NEED MORE INPUT?
THANK YOU
  questions?

Weitere ähnliche Inhalte

Andere mochten auch

2011 02-08 cucumber
2011 02-08 cucumber2011 02-08 cucumber
2011 02-08 cucumberMat Schaffer
 
chef loves windows
chef loves windowschef loves windows
chef loves windowsMat Schaffer
 
UPenn on Rails pt 2
UPenn on Rails pt 2UPenn on Rails pt 2
UPenn on Rails pt 2Mat Schaffer
 
Knockout vs. angular
Knockout vs. angularKnockout vs. angular
Knockout vs. angularMaslowB
 
Hands On Intro to Node.js
Hands On Intro to Node.jsHands On Intro to Node.js
Hands On Intro to Node.jsChris Cowan
 
Sfd2012Hanoi Nguyễn Hà Dương - Introduction to Node.js
Sfd2012Hanoi Nguyễn Hà Dương - Introduction to Node.jsSfd2012Hanoi Nguyễn Hà Dương - Introduction to Node.js
Sfd2012Hanoi Nguyễn Hà Dương - Introduction to Node.jsVu Hung Nguyen
 
Hadoop a Natural Choice for Data Intensive Log Processing
Hadoop a Natural Choice for Data Intensive Log ProcessingHadoop a Natural Choice for Data Intensive Log Processing
Hadoop a Natural Choice for Data Intensive Log ProcessingHitendra Kumar
 
JS Frameworks - Angular Vs Backbone
JS Frameworks - Angular Vs BackboneJS Frameworks - Angular Vs Backbone
JS Frameworks - Angular Vs BackboneGourav Jain, MCTS®
 

Andere mochten auch (9)

2011 02-08 cucumber
2011 02-08 cucumber2011 02-08 cucumber
2011 02-08 cucumber
 
chef loves windows
chef loves windowschef loves windows
chef loves windows
 
UPenn on Rails pt 2
UPenn on Rails pt 2UPenn on Rails pt 2
UPenn on Rails pt 2
 
Knockout vs. angular
Knockout vs. angularKnockout vs. angular
Knockout vs. angular
 
Hands On Intro to Node.js
Hands On Intro to Node.jsHands On Intro to Node.js
Hands On Intro to Node.js
 
Sfd2012Hanoi Nguyễn Hà Dương - Introduction to Node.js
Sfd2012Hanoi Nguyễn Hà Dương - Introduction to Node.jsSfd2012Hanoi Nguyễn Hà Dương - Introduction to Node.js
Sfd2012Hanoi Nguyễn Hà Dương - Introduction to Node.js
 
Hadoop a Natural Choice for Data Intensive Log Processing
Hadoop a Natural Choice for Data Intensive Log ProcessingHadoop a Natural Choice for Data Intensive Log Processing
Hadoop a Natural Choice for Data Intensive Log Processing
 
JS Frameworks - Angular Vs Backbone
JS Frameworks - Angular Vs BackboneJS Frameworks - Angular Vs Backbone
JS Frameworks - Angular Vs Backbone
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 

Ähnlich wie Ruby on the Phone

Phone calls and sms from php
Phone calls and sms from phpPhone calls and sms from php
Phone calls and sms from phpDavid Stockton
 
Real time voice call integration - Confoo 2012
Real time voice call integration - Confoo 2012Real time voice call integration - Confoo 2012
Real time voice call integration - Confoo 2012Michael Peacock
 
Lets have some fun with twilio open tok
Lets have some fun with   twilio open tokLets have some fun with   twilio open tok
Lets have some fun with twilio open tokmirahman
 
Si pp introduction_2
Si pp introduction_2Si pp introduction_2
Si pp introduction_2kamrandb2
 
East Bay Ruby Tropo presentation
East Bay Ruby Tropo presentationEast Bay Ruby Tropo presentation
East Bay Ruby Tropo presentationAdam Kalsey
 
Building a Better Call Center with Telephony APIs, SugarCRM, and WebRTC
Building a Better Call Center with Telephony APIs, SugarCRM, and WebRTCBuilding a Better Call Center with Telephony APIs, SugarCRM, and WebRTC
Building a Better Call Center with Telephony APIs, SugarCRM, and WebRTCSugarCRM
 
Swing when you're winning - an introduction to Ruby and Sinatra
Swing when you're winning - an introduction to Ruby and SinatraSwing when you're winning - an introduction to Ruby and Sinatra
Swing when you're winning - an introduction to Ruby and SinatraMatt Gifford
 
Creating Voice Powered Apps with Ribbit
Creating Voice Powered Apps with RibbitCreating Voice Powered Apps with Ribbit
Creating Voice Powered Apps with RibbitJames Williams
 
Twilio at the Google App Engine Meetup 2009-10
Twilio at the Google App Engine Meetup 2009-10Twilio at the Google App Engine Meetup 2009-10
Twilio at the Google App Engine Meetup 2009-10Twilio Inc
 
Interface de Voz con Rails
Interface de Voz con RailsInterface de Voz con Rails
Interface de Voz con RailsSvet Ivantchev
 
2015 ZendCon - Do you queue
2015 ZendCon - Do you queue2015 ZendCon - Do you queue
2015 ZendCon - Do you queueMike Willbanks
 
Using Sinatra to Build REST APIs in Ruby
Using Sinatra to Build REST APIs in RubyUsing Sinatra to Build REST APIs in Ruby
Using Sinatra to Build REST APIs in RubyLaunchAny
 
Volt ruby framework
Volt ruby frameworkVolt ruby framework
Volt ruby frameworkthomasfl
 
Asynchronous programming patterns in Perl
Asynchronous programming patterns in PerlAsynchronous programming patterns in Perl
Asynchronous programming patterns in Perldeepfountainconsulting
 
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)Dotan Dimet
 
VoIP - Cisco CME &amp; IP Communicator
VoIP - Cisco CME &amp; IP CommunicatorVoIP - Cisco CME &amp; IP Communicator
VoIP - Cisco CME &amp; IP Communicatorchinmaypadhye1985
 

Ähnlich wie Ruby on the Phone (20)

Phone calls and sms from php
Phone calls and sms from phpPhone calls and sms from php
Phone calls and sms from php
 
Real time voice call integration - Confoo 2012
Real time voice call integration - Confoo 2012Real time voice call integration - Confoo 2012
Real time voice call integration - Confoo 2012
 
Lets have some fun with twilio open tok
Lets have some fun with   twilio open tokLets have some fun with   twilio open tok
Lets have some fun with twilio open tok
 
Swoole Overview
Swoole OverviewSwoole Overview
Swoole Overview
 
Si pp introduction_2
Si pp introduction_2Si pp introduction_2
Si pp introduction_2
 
East Bay Ruby Tropo presentation
East Bay Ruby Tropo presentationEast Bay Ruby Tropo presentation
East Bay Ruby Tropo presentation
 
Building a Better Call Center with Telephony APIs, SugarCRM, and WebRTC
Building a Better Call Center with Telephony APIs, SugarCRM, and WebRTCBuilding a Better Call Center with Telephony APIs, SugarCRM, and WebRTC
Building a Better Call Center with Telephony APIs, SugarCRM, and WebRTC
 
Swing when you're winning - an introduction to Ruby and Sinatra
Swing when you're winning - an introduction to Ruby and SinatraSwing when you're winning - an introduction to Ruby and Sinatra
Swing when you're winning - an introduction to Ruby and Sinatra
 
Creating Voice Powered Apps with Ribbit
Creating Voice Powered Apps with RibbitCreating Voice Powered Apps with Ribbit
Creating Voice Powered Apps with Ribbit
 
Twilio at the Google App Engine Meetup 2009-10
Twilio at the Google App Engine Meetup 2009-10Twilio at the Google App Engine Meetup 2009-10
Twilio at the Google App Engine Meetup 2009-10
 
Interface de Voz con Rails
Interface de Voz con RailsInterface de Voz con Rails
Interface de Voz con Rails
 
2015 ZendCon - Do you queue
2015 ZendCon - Do you queue2015 ZendCon - Do you queue
2015 ZendCon - Do you queue
 
Using Sinatra to Build REST APIs in Ruby
Using Sinatra to Build REST APIs in RubyUsing Sinatra to Build REST APIs in Ruby
Using Sinatra to Build REST APIs in Ruby
 
web3j Overview
web3j Overviewweb3j Overview
web3j Overview
 
Rack Middleware
Rack MiddlewareRack Middleware
Rack Middleware
 
Volt ruby framework
Volt ruby frameworkVolt ruby framework
Volt ruby framework
 
Tdd iPhone For Dummies
Tdd iPhone For DummiesTdd iPhone For Dummies
Tdd iPhone For Dummies
 
Asynchronous programming patterns in Perl
Asynchronous programming patterns in PerlAsynchronous programming patterns in Perl
Asynchronous programming patterns in Perl
 
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
 
VoIP - Cisco CME &amp; IP Communicator
VoIP - Cisco CME &amp; IP CommunicatorVoIP - Cisco CME &amp; IP Communicator
VoIP - Cisco CME &amp; IP Communicator
 

Kürzlich hochgeladen

Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 

Kürzlich hochgeladen (20)

Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 

Ruby on the Phone

  • 1. RUBY ON THE PHONE Mat Schaffer, Ruby Nation 2011
  • 3. RAILS – JAVASCRIPT (for hire)
  • 5. • Provider comparison • Making phone calls • Handling Call flow • SMS • Testing
  • 6. XML REST API JSON REST API you generate TwiML they host a JVM script just phone and SMS IM and Twitter integration 1¢/2¢ per minute 3¢ per minute $1 per month per line $3 per month per line
  • 8. https://github.com/webficient/twilio require 'rubygems' require 'twilio' Twilio.connect(ENV['TWILIO_SID'], ENV['TWILIO_TOKEN']) Twilio::Call.make('+12155551213',                   '+12155551234',                   'http://myapp.com') From* To What do to
  • 9. > curl -d From=+12155551212        -d To=+12155551234        -d Url=http://myapp.com        -u $TWILIO_SID:$TWILIO_TOKEN https://api.twilio.com/2010-04-01/Accounts/ $TWILIO_SID/Calls
  • 10. POST https://twilio/call Url=http://me/calls POST https://me/calls CallSid=CAc7ca63fb9...
  • 11. <?xml version="1.0" encoding="UTF-8" ?> <Response>   <Say>Hello Ruby Nation</Say> </Response>
  • 13.
  • 14. require 'rubygems' require 'twilio' Twilio.connect(ENV['TWILIO_SID'], ENV['TWILIO_TOKEN']) resp = Twilio::AvailablePhoneNumbers.search_local(:postal_code => '20191') numbers = resp['TwilioResponse']['AvailablePhoneNumbers']['AvailablePhoneNumber'] my_number = numbers.first Twilio::IncomingPhoneNumber.create(:PhoneNumber => my_number['PhoneNumber'],                                    :VoiceUrl => 'http://myapp.com')
  • 15.
  • 16. For a good time call (540) 318-2266 post '/' do   return Twilio::Verb.new do |v|     v.say "Welcome to the party!"     v.dial do       v.conference 'rubynation_partyline'     end   end.response end <?xml version="1.0" encoding="UTF-8"?> <Response>   <Say>Welcome to the party!</Say>   <Dial>     <Conference>rubynation_partyline</Conference>   </Dial> </Response>
  • 17.
  • 18. TWIML
  • 19. VERBS OUTPUT <Play> <Say> INPUT <Gather> <Record> <Dial> CALLS <Number> <Conference> <Hangup> <Reject> OTHERS <Pause> <Redirect> <Sms>
  • 20. WHOOPS! RAILS ISSUES • skip_before_filter :verify_authenticity_token • be careful about response types (use *.xml or filter) • tough to fit in RESTful model
  • 22. TWILIO LOOP 1.Post initial call information with new CallSid 2.Perform action 3.Post action’s result with original CallSid 4.Repeat
  • 23. <?xml version="1.0" encoding="UTF-8"?> <Response> <Gather action="/calls/response.xml"> " <Say>Hi there, press a number</Say> </Gather> </Response> <?xml version="1.0" encoding="UTF-8"?> <Response> <Say>Thanks for your input</Say> </Response>
  • 24. <?xml version="1.0" encoding="UTF-8"?> <Response> <Dial action="/calls/complete.xml"> " <Number url="/calls/prompt.xml?connection_id=1">2155551212</Number> </Dial> </Response> <?xml version="1.0" encoding="UTF-8"?> <Response> <Say> Here’s what the called person hears </Say> </Response> <?xml version="1.0" encoding="UTF-8"?> <Response> <Say>The caller hears this after the call</Say> </Response>
  • 25. SMS
  • 26. SENDING require 'rubygems' require 'twilio' Twilio.connect(ENV['TWILIO_SID'], ENV['TWILIO_TOKEN']) Twilio::Sms.message('+12155551213',                     '+12155551234',                     "Hello sms")
  • 27. RECEIVING require 'rubygems' require 'sinatra' require 'twilio' post '/sms' do   return Twilio::Verb.new do |v|     v.sms "Ohai #{params[:From]}"   end.response end <?xml version="1.0" encoding="UTF-8"?> <Response>   <Sms>Ohai +12155551212</Sms> </Response>
  • 29. REST CALL TESTING • Mocking: Mocha, Rspec, etc • HTTP Mocking: VCR, Artiface, FakeWeb, WebMock
  • 30. require 'rubygems' require 'sinatra' class FakeTwilio < Sinatra::Base   def fixture(resource)     resource.gsub!('/', '-')     Rails.root.join('test', 'support', 'twilio_responses', "#{resource}-GET.xml").read   end   get '/2010-04-01/Accounts/:sid/*' do |sid, resource|     fixture(resource)   end   post '/2010-04-01/Accounts/:sid/*' do |sid, resource|     fixture(resource)   end end Artifice.activate_with(FakeTwilio)
  • 31. ARTIFICE + CUCUMBER Kinda sucked. got better? module Selenium   Net = ::Net.dup   module Net     HTTP = Artifice::NET_HTTP   end end class Capybara::Server   Net = ::Net.dup   module Net     HTTP = Artifice::NET_HTTP   end end
  • 32. TWIML TESTING   test "new should omit connected connections" do     @connection.connect!     post :new, :CallSid => @sid     assert_match "Number", response.body     assert_not_match @connection.number, response.body   end Basic match testing
  • 34. THANK YOU questions?