twilio-rb 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/README.md +95 -0
  2. data/lib/twilio/twiml.rb +20 -0
  3. data/lib/twilio.rb +1 -1
  4. metadata +20 -3
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+
2
+ # Twilio.rb
3
+
4
+ Interact with the Twilio API in a nice Ruby way
5
+
6
+ ## Configuration
7
+
8
+ Configuration for this library is encapsulated within `Twilio::Config`. One needs to setup with an Account SID and an Auth Token, e.g.
9
+
10
+ <pre># This should be in an initializer or similar
11
+ Twilio::Config.setup do
12
+ account_sid 'AC0000000000000000000000000000'
13
+ auth_token '000000000000000000000000000000'
14
+ end</pre>
15
+
16
+ Any method that calls the Twilio API will raise `Twilio::ConfigurationError` if either Account SID or Auth Token are not configured.
17
+
18
+ ## Making a telephone call
19
+
20
+ The API used to make a telephone call is similar to interacting with an ActiveRecord model object.
21
+ <pre>Twilio::Call.create :to => '+16465551234', :from => '+19175550000',
22
+ :url => "http://example.com/call_handler"</pre>
23
+ or
24
+ <pre>call = Twilio::Call.new :to => '+16465551234', :from => '+19175550000',
25
+ :url => "http://example.com/call_handler"
26
+
27
+ call.save</pre>
28
+
29
+ The parameter keys should be given as underscored symbols. They will be converted internally to camelized strings prior to an API call being made.
30
+
31
+ Please see the Twilio REST API documentation for an up to date list of supported parameters.
32
+
33
+ ### Modifying a live telephone call
34
+
35
+ Once a call has been been created it can be modified with the following methods:
36
+
37
+ `Twilio::Call#cancel!` will terminate the call if its state is `queued` or `ringing`
38
+ `Twilio::Call#complete!` will terminate the call even if its state is `in-progress`
39
+ `Twilio::Call#url=` will immediately redirect the call to a new handler URL
40
+
41
+ `Twilio::Call#cancel!` and `Twilio::Call#complete!` will raise `Twilio::InvalidStateError` if the call has not been "saved".
42
+ `Twilio::Call#url=` will updated its state with the new URL ready for when `Twilio::Call#save` is called.
43
+
44
+ ## Sending an SMS message
45
+
46
+ The API used to send an SMS message is similar to interacting with an ActiveRecord model object.
47
+ <pre>Twilio::SMS.create :to => '+16465551234', :from => '+19175550000',
48
+ :body => "Hey baby, how was your day? x"</pre>
49
+ or
50
+ <pre>sms = Twilio::SMS.new :to => '+16465551234', :from => '+19175550000',
51
+ :body => "Hey baby, how was your day? x"
52
+
53
+ sms.save</pre>
54
+
55
+ The parameter keys should be given as underscored symbols. They will be converted internally to camelized strings prior to an API call being made.
56
+
57
+ Please see the Twilio REST API documentation for an up to date list of supported parameters.
58
+
59
+ ## Building TwiML documents
60
+
61
+ A TwiML document is an XML document. The best way to build XML in Ruby is with Builder, and so it follows that we should use builder for TwiML. `Twilio::TwiML.build` behaves like builder except element names are capitalised for you and attributes are camelized for you as well. This is so you may continue to write beautiful code.
62
+
63
+ The following Ruby code:
64
+
65
+ <pre>Twilio::TwiML.build do |res|
66
+ res.say 'Hey man! Listen to this!', :voice => 'man'
67
+ res.play 'http://foo.com/cowbell.mp3'
68
+ res.say 'What did you think of that?!', :voice => 'man'
69
+ res.record :action => "http://foo.com/handleRecording.php",
70
+ :method => "GET", :max_length => "20",
71
+ :finish_on_Key => "*"
72
+ res.gather :action => "/process_gather.php", :method => "GET" do |g|
73
+ g.say 'Now hit some buttons!'
74
+ end
75
+ res.say 'Awesome! Thanks!', :voice => 'man'
76
+ res.hangup
77
+ end</pre>
78
+
79
+ Therefore emits the following TwiML document:
80
+
81
+ <pre><?xml version="1.0" encoding="UTF-8"?>
82
+ &lt;Response&gt;
83
+ &lt;Say voice="man"&gt;Hey man! Listen to this!&lt;/Say&gt;
84
+ &lt;Play&gt;http://foo.com/cowbell.mp3&lt;/Play&gt;
85
+ &lt;Say voice="man"&gt;What did you think of that?!&lt;/Say&gt;
86
+ &lt;Record maxLength="20" method="GET" action="http://foo.com/handleRecording.php" finishOnKey="*"/&gt;
87
+ &lt;Gather method="GET" action="/process_gather.php"&gt;
88
+ &lt;Say&gt;Now hit some buttons!&lt;/Say&gt;
89
+ &lt;/Gather&gt;
90
+ &lt;Say voice="man"&gt;Awesome! Thanks!&lt;/Say&gt;
91
+ &lt;Hangup/&gt;
92
+ &lt;/Response&gt;
93
+ </pre>
94
+
95
+ This specialised behaviour only affects `Twilio::TwiML.build` and does not affect Builder in general.
@@ -0,0 +1,20 @@
1
+ module Twilio
2
+ module TwiML
3
+ def build &blk
4
+ xm = Builder::XmlMarkup.new(:indent => 2)
5
+ xm.instance_eval do
6
+ def method_missing(meth, *args, &blk)
7
+ # camelize options
8
+ if args.last.kind_of? Hash
9
+ args[-1] = Hash[args.last.map { |k,v| [k.to_s.camelize(:lower), v]}]
10
+ end
11
+ # let builder do the heavy lifting
12
+ super(meth.to_s.capitalize, *args, &blk)
13
+ end
14
+ end
15
+ xm.instruct!
16
+ xm.response &blk
17
+ end
18
+ extend self
19
+ end
20
+ end
data/lib/twilio.rb CHANGED
@@ -1,4 +1,4 @@
1
- %w<rubygems active_support CGI yajl yajl/json_gem httparty>.each { |lib| require lib }
1
+ %w<rubygems active_support CGI yajl yajl/json_gem httparty builder>.each { |lib| require lib }
2
2
  require File.join(File.dirname(__FILE__), 'twilio', 'resource.rb')
3
3
 
4
4
  module Twilio
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Stevie Graham
@@ -77,6 +77,21 @@ dependencies:
77
77
  version: 1.3.5
78
78
  type: :runtime
79
79
  version_requirements: *id004
80
+ - !ruby/object:Gem::Dependency
81
+ name: builder
82
+ prerelease: false
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 2
90
+ - 1
91
+ - 2
92
+ version: 2.1.2
93
+ type: :runtime
94
+ version_requirements: *id005
80
95
  description: A nice Ruby wrapper for the Twilio REST API
81
96
  email: sjtgraham@mac.com
82
97
  executables: []
@@ -86,10 +101,12 @@ extensions: []
86
101
  extra_rdoc_files: []
87
102
 
88
103
  files:
104
+ - README.md
89
105
  - lib/twilio/call.rb
90
106
  - lib/twilio/config.rb
91
107
  - lib/twilio/resource.rb
92
108
  - lib/twilio/sms.rb
109
+ - lib/twilio/twiml.rb
93
110
  - lib/twilio.rb
94
111
  has_rdoc: true
95
112
  homepage: http://github.com/stevegraham/twilio-rb