twilio-rb 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/twilio/call.rb +40 -0
- data/lib/twilio/config.rb +14 -0
- data/lib/twilio/resource.rb +100 -0
- data/lib/twilio/sms.rb +10 -0
- data/lib/twilio.rb +17 -0
- metadata +129 -0
data/lib/twilio/call.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
module Twilio
|
2
|
+
class Call
|
3
|
+
include Twilio::Resource
|
4
|
+
|
5
|
+
# Dials the call
|
6
|
+
def save
|
7
|
+
handle_response self.class.post "/Accounts/#{Twilio::ACCOUNT_SID}/Calls.json", :body => attributes
|
8
|
+
end
|
9
|
+
|
10
|
+
# Cancels a call if its state is 'queued' or 'ringing'
|
11
|
+
def cancel!
|
12
|
+
state_guard { modify_call 'Status' => 'cancelled' }
|
13
|
+
end
|
14
|
+
|
15
|
+
def complete!
|
16
|
+
state_guard { modify_call 'Status' => 'completed' }
|
17
|
+
end
|
18
|
+
|
19
|
+
# Update Handler URL
|
20
|
+
def url=(url)
|
21
|
+
# If this attribute exists it is assumed the API call to create a call has been made, so we need to tell Twilio.
|
22
|
+
modify_call "url" => url if self[:status]
|
23
|
+
self[:url] = url
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def state_guard(&blk)
|
29
|
+
if self[:status] # If this attribute exists it is assumed the API call to create a call has been made, and the object is in the correct state to make request.
|
30
|
+
blk.call
|
31
|
+
else
|
32
|
+
raise Twilio::InvalidStateError.new 'Call is in invalid state to perform this action.'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def modify_call(params)
|
37
|
+
handle_response self.class.post "/Accounts/#{Twilio::ACCOUNT_SID}/Calls/#{self[:sid]}.json", :body => params
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Twilio
|
2
|
+
module Config
|
3
|
+
def setup &blk
|
4
|
+
instance_eval &blk
|
5
|
+
end
|
6
|
+
|
7
|
+
def method_missing(meth, *args, &blk)
|
8
|
+
const = meth.to_s.upcase
|
9
|
+
Twilio.const_set(const, args.first) unless Twilio.const_defined? const
|
10
|
+
end
|
11
|
+
|
12
|
+
extend self
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'active_support/core_ext/string/inflections' # Chill! we only use the bits of AS we need!
|
2
|
+
|
3
|
+
module Twilio
|
4
|
+
module Resource
|
5
|
+
def initialize(attrs ={}) #:nodoc:
|
6
|
+
@attributes = Hash[attrs.map { |k,v| [k.to_s.camelize, v.to_s] }]
|
7
|
+
normalize_http_verbs!
|
8
|
+
escape_send_digits! if attributes.include? 'SendDigits'
|
9
|
+
normalize_if_machine_parameter!
|
10
|
+
end
|
11
|
+
|
12
|
+
# Convenience for accessing attributes. Attributes can be accessed either using the
|
13
|
+
# preferred symbol style, e.g. :if_machine or using the Twilio stringified attribute
|
14
|
+
# style, e.g. 'IfMachine'
|
15
|
+
# Kind of like ActiveSupport::HashWithIndifferentAccess on crack.
|
16
|
+
def [](key)
|
17
|
+
accessor = key.is_a?(Symbol) ? key.to_s.camelize : key
|
18
|
+
attributes[accessor]
|
19
|
+
end
|
20
|
+
|
21
|
+
def []=(key,value)
|
22
|
+
accessor = key.is_a?(Symbol) ? key.to_s.camelize : key
|
23
|
+
attributes[accessor] = value
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def handle_response(res) # :nodoc:
|
29
|
+
if res.code.to_s =~ /^(4|5)\d\d/
|
30
|
+
raise Twilio::APIError.new "Error ##{res.parsed_response['code']}: #{res.parsed_response['message']}"
|
31
|
+
else
|
32
|
+
attributes.update Hash[res.parsed_response.map { |k,v| [k.camelize, v] }] # params are camelized in requests, yet underscored in the repsonse. inconsistency FTW!
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def normalize_http_verbs! #:nodoc:
|
37
|
+
# Twilio accepts a HTTP method for use with various callbacks. The API documentation
|
38
|
+
# indicates that the HTTP verbs are to be passed as upcase.
|
39
|
+
attributes.each { |k,v| v.upcase! if k =~ /Method$/ }
|
40
|
+
end
|
41
|
+
|
42
|
+
def escape_send_digits! #:nodoc:
|
43
|
+
# A pound, i.e. "#" has special meaning in a URL so it must be escaped
|
44
|
+
attributes.update 'SendDigits' => CGI.escape(attributes['SendDigits'])
|
45
|
+
end
|
46
|
+
|
47
|
+
def normalize_if_machine_parameter! #:nodoc:
|
48
|
+
attributes['IfMachine'].capitalize! if attributes['IfMachine']
|
49
|
+
end
|
50
|
+
|
51
|
+
def method_missing(meth, *args, &blk) #:nodoc
|
52
|
+
meth = meth.to_s.camelize
|
53
|
+
if meth.to_s =~ /\=$/
|
54
|
+
add_attr_writer meth
|
55
|
+
send meth, args.first
|
56
|
+
elsif attributes.include? meth = meth.gsub('=', '')
|
57
|
+
add_attr_reader meth
|
58
|
+
send meth
|
59
|
+
else
|
60
|
+
super
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def add_attr_writer(attribute) #:nodoc
|
65
|
+
metaclass.class_eval do
|
66
|
+
attribute = attribute.to_s.gsub(/\=$/, '')
|
67
|
+
define_method("#{attribute}=") { |value| attributes[attribute] = value } unless respond_to? "#{attribute}="
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def add_attr_reader(attribute) #:nodoc
|
72
|
+
metaclass.class_eval do
|
73
|
+
define_method(attribute) { attributes[attribute] } unless respond_to? attribute
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def metaclass #:nodoc
|
78
|
+
class << self; self; end
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.included(base)
|
82
|
+
base.instance_eval do
|
83
|
+
include HTTParty
|
84
|
+
attr_reader :attributes
|
85
|
+
format :json
|
86
|
+
base_uri Twilio::API_ENDPOINT
|
87
|
+
end
|
88
|
+
|
89
|
+
class << base
|
90
|
+
def create(attrs={})
|
91
|
+
new(attrs).tap { |c| c.save }
|
92
|
+
end
|
93
|
+
|
94
|
+
def post(url, opts)
|
95
|
+
super url, opts.merge(:basic_auth => { :username => Twilio::ACCOUNT_SID, :password => Twilio::AUTH_TOKEN })
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/lib/twilio/sms.rb
ADDED
data/lib/twilio.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
%w<rubygems active_support CGI yajl yajl/json_gem httparty>.each { |lib| require lib }
|
2
|
+
require File.join(File.dirname(__FILE__), 'twilio', 'resource.rb')
|
3
|
+
|
4
|
+
module Twilio
|
5
|
+
API_ENDPOINT = 'https://api.twilio.com/2010-04-01'
|
6
|
+
APIError = Class.new StandardError
|
7
|
+
ConfigurationError = Class.new StandardError
|
8
|
+
InvalidStateError = Class.new StandardError
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def const_missing(const_name)
|
12
|
+
raise Twilio::ConfigurationError.new "Cannot complete request. Please set #{const_name.to_s.downcase} with Twilio::Config.setup first!"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
Dir[File.join(File.dirname(__FILE__), 'twilio', '*.rb')].each { |lib| require lib }
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: twilio-rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Stevie Graham
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-09-28 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activesupport
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 3
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
version: 3.0.0
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: yajl-ruby
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
- 7
|
46
|
+
- 7
|
47
|
+
version: 0.7.7
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: httparty
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
- 6
|
61
|
+
- 1
|
62
|
+
version: 0.6.1
|
63
|
+
type: :runtime
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: webmock
|
67
|
+
prerelease: false
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 1
|
75
|
+
- 3
|
76
|
+
- 5
|
77
|
+
version: 1.3.5
|
78
|
+
type: :runtime
|
79
|
+
version_requirements: *id004
|
80
|
+
description: A nice Ruby wrapper for the Twilio REST API
|
81
|
+
email: sjtgraham@mac.com
|
82
|
+
executables: []
|
83
|
+
|
84
|
+
extensions: []
|
85
|
+
|
86
|
+
extra_rdoc_files: []
|
87
|
+
|
88
|
+
files:
|
89
|
+
- lib/twilio/call.rb
|
90
|
+
- lib/twilio/config.rb
|
91
|
+
- lib/twilio/resource.rb
|
92
|
+
- lib/twilio/sms.rb
|
93
|
+
- lib/twilio.rb
|
94
|
+
has_rdoc: true
|
95
|
+
homepage: http://github.com/stevegraham/twilio-rb
|
96
|
+
licenses: []
|
97
|
+
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
segments:
|
109
|
+
- 1
|
110
|
+
- 8
|
111
|
+
- 7
|
112
|
+
version: 1.8.7
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
version: "0"
|
121
|
+
requirements: []
|
122
|
+
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 1.3.7
|
125
|
+
signing_key:
|
126
|
+
specification_version: 3
|
127
|
+
summary: Interact with the Twilio API in a nice Ruby way.
|
128
|
+
test_files: []
|
129
|
+
|