twilio-ruby 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/lib/twilio-ruby.rb +33 -30
  2. data/lib/twilio-ruby/rest/accounts/account.rb +12 -0
  3. data/lib/twilio-ruby/rest/accounts/accounts.rb +5 -0
  4. data/lib/twilio-ruby/rest/available_phone_numbers/available_phone_number.rb +5 -0
  5. data/lib/twilio-ruby/rest/available_phone_numbers/available_phone_numbers.rb +11 -0
  6. data/lib/twilio-ruby/rest/available_phone_numbers/country.rb +10 -0
  7. data/lib/twilio-ruby/rest/available_phone_numbers/local.rb +11 -0
  8. data/lib/twilio-ruby/rest/available_phone_numbers/toll_free.rb +11 -0
  9. data/lib/twilio-ruby/rest/calls/call.rb +22 -0
  10. data/lib/twilio-ruby/rest/calls/calls.rb +10 -0
  11. data/lib/twilio-ruby/rest/client.rb +61 -0
  12. data/lib/twilio-ruby/rest/conferences/conference.rb +10 -0
  13. data/lib/twilio-ruby/rest/conferences/conferences.rb +5 -0
  14. data/lib/twilio-ruby/rest/conferences/participant.rb +17 -0
  15. data/lib/twilio-ruby/rest/conferences/participants.rb +5 -0
  16. data/lib/twilio-ruby/rest/incoming_phone_numbers/incoming_phone_number.rb +5 -0
  17. data/lib/twilio-ruby/rest/incoming_phone_numbers/incoming_phone_numbers.rb +9 -0
  18. data/lib/twilio-ruby/rest/instance_resource.rb +55 -0
  19. data/lib/twilio-ruby/rest/list_resource.rb +35 -0
  20. data/lib/twilio-ruby/rest/notifications/notification.rb +5 -0
  21. data/lib/twilio-ruby/rest/notifications/notifications.rb +5 -0
  22. data/lib/twilio-ruby/rest/outgoing_caller_ids/outgoing_caller_id.rb +5 -0
  23. data/lib/twilio-ruby/rest/outgoing_caller_ids/outgoing_caller_ids.rb +9 -0
  24. data/lib/twilio-ruby/rest/recordings/recording.rb +12 -0
  25. data/lib/twilio-ruby/rest/recordings/recordings.rb +5 -0
  26. data/lib/twilio-ruby/rest/sandbox/sandbox.rb +5 -0
  27. data/lib/twilio-ruby/rest/sms_messages/sms_message.rb +5 -0
  28. data/lib/twilio-ruby/rest/sms_messages/sms_messages.rb +9 -0
  29. data/lib/twilio-ruby/rest/transcriptions/transcription.rb +5 -0
  30. data/lib/twilio-ruby/rest/transcriptions/transcriptions.rb +5 -0
  31. data/lib/twilio-ruby/rest/utils.rb +32 -0
  32. data/lib/twilio-ruby/{verb.rb → twiml/verb.rb} +0 -0
  33. metadata +36 -36
  34. data/lib/twilio-ruby/accounts/account.rb +0 -10
  35. data/lib/twilio-ruby/accounts/accounts.rb +0 -3
  36. data/lib/twilio-ruby/available_phone_numbers/available_phone_number.rb +0 -3
  37. data/lib/twilio-ruby/available_phone_numbers/available_phone_numbers.rb +0 -9
  38. data/lib/twilio-ruby/available_phone_numbers/country.rb +0 -8
  39. data/lib/twilio-ruby/available_phone_numbers/local.rb +0 -9
  40. data/lib/twilio-ruby/available_phone_numbers/toll_free.rb +0 -9
  41. data/lib/twilio-ruby/calls/call.rb +0 -20
  42. data/lib/twilio-ruby/calls/calls.rb +0 -7
  43. data/lib/twilio-ruby/client.rb +0 -59
  44. data/lib/twilio-ruby/conferences/conference.rb +0 -8
  45. data/lib/twilio-ruby/conferences/conferences.rb +0 -3
  46. data/lib/twilio-ruby/conferences/participant.rb +0 -15
  47. data/lib/twilio-ruby/conferences/participants.rb +0 -3
  48. data/lib/twilio-ruby/incoming_phone_numbers/incoming_phone_number.rb +0 -3
  49. data/lib/twilio-ruby/incoming_phone_numbers/incoming_phone_numbers.rb +0 -7
  50. data/lib/twilio-ruby/instance_resource.rb +0 -53
  51. data/lib/twilio-ruby/list_resource.rb +0 -33
  52. data/lib/twilio-ruby/notifications/notification.rb +0 -3
  53. data/lib/twilio-ruby/notifications/notifications.rb +0 -3
  54. data/lib/twilio-ruby/outgoing_caller_ids/outgoing_caller_id.rb +0 -3
  55. data/lib/twilio-ruby/outgoing_caller_ids/outgoing_caller_ids.rb +0 -7
  56. data/lib/twilio-ruby/recordings/recording.rb +0 -10
  57. data/lib/twilio-ruby/recordings/recordings.rb +0 -3
  58. data/lib/twilio-ruby/sandbox/sandbox.rb +0 -3
  59. data/lib/twilio-ruby/sms_messages/sms_message.rb +0 -3
  60. data/lib/twilio-ruby/sms_messages/sms_messages.rb +0 -7
  61. data/lib/twilio-ruby/transcriptions/transcription.rb +0 -3
  62. data/lib/twilio-ruby/transcriptions/transcriptions.rb +0 -3
  63. data/lib/twilio-ruby/utils.rb +0 -23
data/lib/twilio-ruby.rb CHANGED
@@ -3,34 +3,37 @@ require 'net/https'
3
3
  require 'builder'
4
4
  require 'crack'
5
5
  require 'cgi'
6
+ require 'openssl'
7
+ require 'base64'
6
8
 
7
- require 'twilio-ruby/utils'
8
- require 'twilio-ruby/list_resource'
9
- require 'twilio-ruby/instance_resource'
10
- require 'twilio-ruby/sandbox/sandbox'
11
- require 'twilio-ruby/accounts/account'
12
- require 'twilio-ruby/accounts/accounts'
13
- require 'twilio-ruby/calls/call'
14
- require 'twilio-ruby/calls/calls'
15
- require 'twilio-ruby/sms_messages/sms_message'
16
- require 'twilio-ruby/sms_messages/sms_messages'
17
- require 'twilio-ruby/outgoing_caller_ids/outgoing_caller_id'
18
- require 'twilio-ruby/outgoing_caller_ids/outgoing_caller_ids'
19
- require 'twilio-ruby/incoming_phone_numbers/incoming_phone_number'
20
- require 'twilio-ruby/incoming_phone_numbers/incoming_phone_numbers'
21
- require 'twilio-ruby/available_phone_numbers/available_phone_number'
22
- require 'twilio-ruby/available_phone_numbers/available_phone_numbers'
23
- require 'twilio-ruby/available_phone_numbers/country'
24
- require 'twilio-ruby/available_phone_numbers/local'
25
- require 'twilio-ruby/available_phone_numbers/toll_free'
26
- require 'twilio-ruby/conferences/conference'
27
- require 'twilio-ruby/conferences/conferences'
28
- require 'twilio-ruby/conferences/participant'
29
- require 'twilio-ruby/conferences/participants'
30
- require 'twilio-ruby/recordings/recording'
31
- require 'twilio-ruby/recordings/recordings'
32
- require 'twilio-ruby/transcriptions/transcription'
33
- require 'twilio-ruby/transcriptions/transcriptions'
34
- require 'twilio-ruby/notifications/notification'
35
- require 'twilio-ruby/notifications/notifications'
36
- require 'twilio-ruby/client'
9
+
10
+ require 'twilio-ruby/rest/utils'
11
+ require 'twilio-ruby/rest/list_resource'
12
+ require 'twilio-ruby/rest/instance_resource'
13
+ require 'twilio-ruby/rest/sandbox/sandbox'
14
+ require 'twilio-ruby/rest/accounts/account'
15
+ require 'twilio-ruby/rest/accounts/accounts'
16
+ require 'twilio-ruby/rest/calls/call'
17
+ require 'twilio-ruby/rest/calls/calls'
18
+ require 'twilio-ruby/rest/sms_messages/sms_message'
19
+ require 'twilio-ruby/rest/sms_messages/sms_messages'
20
+ require 'twilio-ruby/rest/outgoing_caller_ids/outgoing_caller_id'
21
+ require 'twilio-ruby/rest/outgoing_caller_ids/outgoing_caller_ids'
22
+ require 'twilio-ruby/rest/incoming_phone_numbers/incoming_phone_number'
23
+ require 'twilio-ruby/rest/incoming_phone_numbers/incoming_phone_numbers'
24
+ require 'twilio-ruby/rest/available_phone_numbers/available_phone_number'
25
+ require 'twilio-ruby/rest/available_phone_numbers/available_phone_numbers'
26
+ require 'twilio-ruby/rest/available_phone_numbers/country'
27
+ require 'twilio-ruby/rest/available_phone_numbers/local'
28
+ require 'twilio-ruby/rest/available_phone_numbers/toll_free'
29
+ require 'twilio-ruby/rest/conferences/conference'
30
+ require 'twilio-ruby/rest/conferences/conferences'
31
+ require 'twilio-ruby/rest/conferences/participant'
32
+ require 'twilio-ruby/rest/conferences/participants'
33
+ require 'twilio-ruby/rest/recordings/recording'
34
+ require 'twilio-ruby/rest/recordings/recordings'
35
+ require 'twilio-ruby/rest/transcriptions/transcription'
36
+ require 'twilio-ruby/rest/transcriptions/transcriptions'
37
+ require 'twilio-ruby/rest/notifications/notification'
38
+ require 'twilio-ruby/rest/notifications/notifications'
39
+ require 'twilio-ruby/rest/client'
@@ -0,0 +1,12 @@
1
+ module Twilio
2
+ module REST
3
+ class Account < InstanceResource
4
+ def initialize(uri, client, params={})
5
+ super uri, client, params
6
+ resource :sandbox, :available_phone_numbers, :incoming_phone_numbers,
7
+ :calls, :outgoing_caller_ids, :conferences, :sms_messages, :recordings,
8
+ :transcriptions, :notifications
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Twilio
2
+ module REST
3
+ class Accounts < ListResource; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Twilio
2
+ module REST
3
+ class AvailablePhoneNumber < InstanceResource; end
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ module Twilio
2
+ module REST
3
+ class AvailablePhoneNumbers < ListResource
4
+ def initialize(uri, client)
5
+ @resource_name = 'countries'
6
+ @instance_class = Twilio::REST.const_get 'Country'
7
+ @uri, @client = uri, client
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ module Twilio
2
+ module REST
3
+ class Country < InstanceResource
4
+ def initialize(uri, client, params={})
5
+ super uri, client, params
6
+ resource :local, :toll_free
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ module Twilio
2
+ module REST
3
+ class Local < ListResource
4
+ def initialize(uri, client)
5
+ @resource_name = 'available_phone_numbers'
6
+ @instance_class = Twilio.const_get 'AvailablePhoneNumber'
7
+ @uri, @client = uri, client
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Twilio
2
+ module REST
3
+ class TollFree < ListResource
4
+ def initialize(uri, client)
5
+ @resource_name = 'available_phone_numbers'
6
+ @instance_class = Twilio.const_get 'AvailablePhoneNumber'
7
+ @uri, @client = uri, client
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ module Twilio
2
+ module REST
3
+ class Call < InstanceResource
4
+ def initialize(uri, client, params={})
5
+ super uri, client, params
6
+ resource :recordings, :transcriptions
7
+ end
8
+
9
+ def redirect_to(url)
10
+ update :url => url
11
+ end
12
+
13
+ def cancel
14
+ update :status => 'canceled'
15
+ end
16
+
17
+ def hangup
18
+ update :status => 'completed'
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,10 @@
1
+ module Twilio
2
+ module REST
3
+ class Calls < ListResource
4
+ def make(from, to, url)
5
+ create :from => from, :to => to, :url => url
6
+ end
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,61 @@
1
+ module Twilio
2
+ module REST
3
+ class Client
4
+ include Utils
5
+
6
+ attr_reader :api_version, :domain, :account_sid, :account, :accounts
7
+
8
+ def initialize(account_sid, auth_token, api_version='2010-04-01',
9
+ domain='api.twilio.com', proxy=nil)
10
+ @account_sid = account_sid
11
+ @auth_token = auth_token
12
+ @api_version = api_version
13
+ set_up_connection_to domain, proxy
14
+ set_up_subresources
15
+ end
16
+
17
+ # Define some helper methods for sending HTTP requests
18
+ [:get, :put, :post, :delete].each do |method|
19
+ method_class = Net::HTTP.const_get method.to_s.capitalize
20
+ define_method method do |uri, *args|
21
+ uri += '.json'
22
+ params = (args[0] && args[0] != {}) ? twilify(args[0]) : nil
23
+ uri += "?#{url_encode(params)}" if params && method == :get
24
+ request = method_class.new uri
25
+ request.basic_auth @account_sid, @auth_token
26
+ request.form_data = params if params && [:post, :put].include?(method)
27
+ http_response = @connection.request request
28
+ object = Crack::JSON.parse http_response.body if http_response.body
29
+ raise object['message'] unless http_response.kind_of? Net::HTTPSuccess
30
+ object
31
+ end
32
+ end
33
+
34
+ def request(uri, method, params={})
35
+ send method.downcase.to_sym, uri, params
36
+ end
37
+
38
+ private
39
+
40
+ def set_up_connection_to(domain, proxy)
41
+ @connection = Net::HTTP.new domain, 443
42
+ @connection.use_ssl = true
43
+ # Don't check the server cert. Ideally this is configurable, in case an
44
+ # app wants to verify that it is actually talking to the real Twilio.
45
+ @connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
46
+ end
47
+
48
+ def set_up_subresources
49
+ accounts_uri = "/#{@api_version}/Accounts"
50
+ # Set up a special handle to grab the account.
51
+ @account = Twilio::REST::Account.new "#{accounts_uri}/#{@account_sid}", self
52
+ # Set up the accounts subresource.
53
+ @accounts = Twilio::REST::Accounts.new accounts_uri, self
54
+ end
55
+
56
+ def url_encode(hash)
57
+ hash.to_a.map {|pair| pair.map {|e| CGI.escape e.to_s}.join '='}.join '&'
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,10 @@
1
+ module Twilio
2
+ module REST
3
+ class Conference < InstanceResource
4
+ def initialize(uri, client, params={})
5
+ super uri, client, params
6
+ resource :participants
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ module Twilio
2
+ module REST
3
+ class Conferences < ListResource; end
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ module Twilio
2
+ module REST
3
+ class Participant < InstanceResource
4
+
5
+ def mute
6
+ update :muted => 'true'
7
+ end
8
+
9
+ def unmute
10
+ update :muted => 'false'
11
+ end
12
+
13
+ alias :kick :delete
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module Twilio
2
+ module REST
3
+ class Participants < ListResource; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Twilio
2
+ module REST
3
+ class IncomingPhoneNumber < InstanceResource; end
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module Twilio
2
+ module REST
3
+ class IncomingPhoneNumbers < ListResource
4
+ def buy(phone_number)
5
+ create :phone_number => phone_number
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,55 @@
1
+ module Twilio
2
+ module REST
3
+ class InstanceResource
4
+ include Utils
5
+
6
+ def initialize(uri, client, params={})
7
+ @uri, @client = uri, client
8
+ set_up_properties_from params
9
+ end
10
+
11
+ def update(params={})
12
+ raise "Can't update a resource without a Twilio::Client" unless @client
13
+ response = @client.post @uri, params
14
+ set_up_properties_from response
15
+ self
16
+ end
17
+
18
+ def delete
19
+ raise "Can't delete a resource without a Twilio::Client" unless @client
20
+ @client.delete @uri
21
+ end
22
+
23
+ def method_missing(method, *args)
24
+ super if @updated
25
+ response = @client.get @uri
26
+ set_up_properties_from response
27
+ self.send method, *args
28
+ end
29
+
30
+ protected
31
+
32
+ def set_up_properties_from(hash)
33
+ eigenclass = class << self; self; end
34
+ hash.each do |p,v|
35
+ property = detwilify p
36
+ unless ['uri', 'client', 'updated'].include? property
37
+ eigenclass.send :define_method, property.to_sym, &lambda {v}
38
+ end
39
+ end
40
+ @updated = !hash.keys.empty?
41
+ end
42
+
43
+ def resource(*resources)
44
+ resources.each do |r|
45
+ resource = twilify r
46
+ relative_uri = r == :sms_messages ? 'SMS/Messages' : resource
47
+ instance_variable_set("@#{r}",
48
+ Twilio::REST.const_get(resource).new("#{@uri}/#{relative_uri}", @client))
49
+ end
50
+ self.class.instance_eval {attr_reader *resources}
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,35 @@
1
+ module Twilio
2
+ module REST
3
+ class ListResource
4
+ include Utils
5
+
6
+ def initialize(uri, client)
7
+ @resource_name = self.class.name.split('::')[-1]
8
+ @instance_class = Twilio::REST.const_get @resource_name.chop
9
+ @uri, @client = uri, client
10
+ end
11
+
12
+ # Grab a list of this kind of resource and return it as an array.
13
+ def list(params={})
14
+ raise "Can't get a resource list without a Twilio::Client" unless @client
15
+ response = @client.get @uri, params
16
+ resources = response[detwilify(@resource_name)]
17
+ resources.map do |resource|
18
+ @instance_class.new "#{@uri}/#{resource['sid']}", @client, resource
19
+ end
20
+ end
21
+
22
+ # Return an empty instance resource object with the proper URI.
23
+ def get(sid)
24
+ @instance_class.new "#{@uri}/#{sid}", @client
25
+ end
26
+
27
+ # Return a newly created resource.
28
+ def create(params={})
29
+ raise "Can't create a resource without a Twilio::Client" unless @client
30
+ response = @client.post @uri, params
31
+ @instance_class.new "#{@uri}/#{response['sid']}", @client, response
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ module Twilio
2
+ module REST
3
+ class Notification < InstanceResource; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Twilio
2
+ module REST
3
+ class Notifications < ListResource; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Twilio
2
+ module REST
3
+ class OutgoingCallerId < InstanceResource; end
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module Twilio
2
+ module REST
3
+ class OutgoingCallerIds < ListResource
4
+ def add(phone_number)
5
+ create :phone_number => phone_number
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module Twilio
2
+ module REST
3
+ class Recording < InstanceResource
4
+
5
+ def initialize(uri, client, params={})
6
+ super uri, client, params
7
+ resource :transcriptions
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Twilio
2
+ module REST
3
+ class Recordings < ListResource; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Twilio
2
+ module REST
3
+ class Sandbox < InstanceResource; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Twilio
2
+ module REST
3
+ class SmsMessage < InstanceResource; end
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module Twilio
2
+ module REST
3
+ class SmsMessages < ListResource
4
+ def send(from, to, body)
5
+ create :from => from, :to => to, :body => body
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Twilio
2
+ module REST
3
+ class Transcription < InstanceResource; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Twilio
2
+ module REST
3
+ class Transcriptions < ListResource; end
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+ module Twilio
2
+ module REST
3
+ module Utils
4
+
5
+ def twilify(something)
6
+ if something.is_a? Hash
7
+ Hash[*something.to_a.map {|a| [twilify(a[0]).to_sym, a[1]]}.flatten]
8
+ else
9
+ something.to_s.split('_').map do |s|
10
+ [s[0,1].capitalize, s[1..-1]].join
11
+ end.join
12
+ end
13
+ end
14
+
15
+ def detwilify(something)
16
+ if something.is_a? Hash
17
+ Hash[*something.to_a.map {|pair| [detwilify(pair[0]).to_sym, pair[1]]}.flatten]
18
+ else
19
+ something.to_s.gsub(/[A-Z][a-z]*/) {|s| "_#{s.downcase}"}.gsub(/^_/, '')
20
+ end
21
+ end
22
+
23
+ def self.validate(auth_token, signature, url, params={})
24
+ data = url + params.sort.to_s
25
+ digest = OpenSSL::Digest::Digest.new('sha1')
26
+ expected = Base64.encode64(OpenSSL::HMAC.digest(digest, auth_token, data)).strip
27
+ return expected == signature
28
+ end
29
+
30
+ end
31
+ end
32
+ end
File without changes
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twilio-ruby
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 4
10
- version: 0.0.4
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Benton
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-31 00:00:00 -08:00
18
+ date: 2011-02-15 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -60,37 +60,37 @@ extra_rdoc_files: []
60
60
 
61
61
  files:
62
62
  - lib/twilio-ruby.rb
63
- - lib/twilio-ruby/sms_messages/sms_message.rb
64
- - lib/twilio-ruby/sms_messages/sms_messages.rb
65
- - lib/twilio-ruby/client.rb
66
- - lib/twilio-ruby/verb.rb
67
- - lib/twilio-ruby/list_resource.rb
68
- - lib/twilio-ruby/incoming_phone_numbers/incoming_phone_number.rb
69
- - lib/twilio-ruby/incoming_phone_numbers/incoming_phone_numbers.rb
70
- - lib/twilio-ruby/conferences/participant.rb
71
- - lib/twilio-ruby/conferences/conference.rb
72
- - lib/twilio-ruby/conferences/conferences.rb
73
- - lib/twilio-ruby/conferences/participants.rb
74
- - lib/twilio-ruby/transcriptions/transcriptions.rb
75
- - lib/twilio-ruby/transcriptions/transcription.rb
76
- - lib/twilio-ruby/calls/call.rb
77
- - lib/twilio-ruby/calls/calls.rb
78
- - lib/twilio-ruby/utils.rb
79
- - lib/twilio-ruby/accounts/accounts.rb
80
- - lib/twilio-ruby/accounts/account.rb
81
- - lib/twilio-ruby/recordings/recording.rb
82
- - lib/twilio-ruby/recordings/recordings.rb
83
- - lib/twilio-ruby/notifications/notification.rb
84
- - lib/twilio-ruby/notifications/notifications.rb
85
- - lib/twilio-ruby/outgoing_caller_ids/outgoing_caller_id.rb
86
- - lib/twilio-ruby/outgoing_caller_ids/outgoing_caller_ids.rb
87
- - lib/twilio-ruby/available_phone_numbers/country.rb
88
- - lib/twilio-ruby/available_phone_numbers/local.rb
89
- - lib/twilio-ruby/available_phone_numbers/available_phone_number.rb
90
- - lib/twilio-ruby/available_phone_numbers/available_phone_numbers.rb
91
- - lib/twilio-ruby/available_phone_numbers/toll_free.rb
92
- - lib/twilio-ruby/sandbox/sandbox.rb
93
- - lib/twilio-ruby/instance_resource.rb
63
+ - lib/twilio-ruby/rest/sms_messages/sms_message.rb
64
+ - lib/twilio-ruby/rest/sms_messages/sms_messages.rb
65
+ - lib/twilio-ruby/rest/client.rb
66
+ - lib/twilio-ruby/rest/list_resource.rb
67
+ - lib/twilio-ruby/rest/incoming_phone_numbers/incoming_phone_number.rb
68
+ - lib/twilio-ruby/rest/incoming_phone_numbers/incoming_phone_numbers.rb
69
+ - lib/twilio-ruby/rest/conferences/participant.rb
70
+ - lib/twilio-ruby/rest/conferences/conference.rb
71
+ - lib/twilio-ruby/rest/conferences/conferences.rb
72
+ - lib/twilio-ruby/rest/conferences/participants.rb
73
+ - lib/twilio-ruby/rest/transcriptions/transcriptions.rb
74
+ - lib/twilio-ruby/rest/transcriptions/transcription.rb
75
+ - lib/twilio-ruby/rest/calls/call.rb
76
+ - lib/twilio-ruby/rest/calls/calls.rb
77
+ - lib/twilio-ruby/rest/utils.rb
78
+ - lib/twilio-ruby/rest/accounts/accounts.rb
79
+ - lib/twilio-ruby/rest/accounts/account.rb
80
+ - lib/twilio-ruby/rest/recordings/recording.rb
81
+ - lib/twilio-ruby/rest/recordings/recordings.rb
82
+ - lib/twilio-ruby/rest/notifications/notification.rb
83
+ - lib/twilio-ruby/rest/notifications/notifications.rb
84
+ - lib/twilio-ruby/rest/outgoing_caller_ids/outgoing_caller_id.rb
85
+ - lib/twilio-ruby/rest/outgoing_caller_ids/outgoing_caller_ids.rb
86
+ - lib/twilio-ruby/rest/available_phone_numbers/country.rb
87
+ - lib/twilio-ruby/rest/available_phone_numbers/local.rb
88
+ - lib/twilio-ruby/rest/available_phone_numbers/available_phone_number.rb
89
+ - lib/twilio-ruby/rest/available_phone_numbers/available_phone_numbers.rb
90
+ - lib/twilio-ruby/rest/available_phone_numbers/toll_free.rb
91
+ - lib/twilio-ruby/rest/sandbox/sandbox.rb
92
+ - lib/twilio-ruby/rest/instance_resource.rb
93
+ - lib/twilio-ruby/twiml/verb.rb
94
94
  - test/twilio_spec.rb
95
95
  - test/test_twilio.rb
96
96
  - examples.rb
@@ -125,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  requirements: []
126
126
 
127
127
  rubyforge_project:
128
- rubygems_version: 1.4.2
128
+ rubygems_version: 1.5.0
129
129
  signing_key:
130
130
  specification_version: 3
131
131
  summary: A simple library for communicating with the Twilio REST API
@@ -1,10 +0,0 @@
1
- module Twilio
2
- class Account < InstanceResource
3
- def initialize(uri, client, params={})
4
- super uri, client, params
5
- resource :sandbox, :available_phone_numbers, :incoming_phone_numbers,
6
- :calls, :outgoing_caller_ids, :conferences, :sms_messages, :recordings,
7
- :transcriptions, :notifications
8
- end
9
- end
10
- end
@@ -1,3 +0,0 @@
1
- module Twilio
2
- class Accounts < ListResource; end
3
- end
@@ -1,3 +0,0 @@
1
- module Twilio
2
- class AvailablePhoneNumber < InstanceResource; end
3
- end
@@ -1,9 +0,0 @@
1
- module Twilio
2
- class AvailablePhoneNumbers < ListResource
3
- def initialize(uri, client)
4
- @resource_name = 'countries'
5
- @instance_class = Twilio.const_get 'Country'
6
- @uri, @client = uri, client
7
- end
8
- end
9
- end
@@ -1,8 +0,0 @@
1
- module Twilio
2
- class Country < InstanceResource
3
- def initialize(uri, client, params={})
4
- super uri, client, params
5
- resource :local, :toll_free
6
- end
7
- end
8
- end
@@ -1,9 +0,0 @@
1
- module Twilio
2
- class Local < ListResource
3
- def initialize(uri, client)
4
- @resource_name = 'available_phone_numbers'
5
- @instance_class = Twilio.const_get 'AvailablePhoneNumber'
6
- @uri, @client = uri, client
7
- end
8
- end
9
- end
@@ -1,9 +0,0 @@
1
- module Twilio
2
- class TollFree < ListResource
3
- def initialize(uri, client)
4
- @resource_name = 'available_phone_numbers'
5
- @instance_class = Twilio.const_get 'AvailablePhoneNumber'
6
- @uri, @client = uri, client
7
- end
8
- end
9
- end
@@ -1,20 +0,0 @@
1
- module Twilio
2
- class Call < InstanceResource
3
- def initialize(uri, client, params={})
4
- super uri, client, params
5
- resource :recordings, :transcriptions
6
- end
7
-
8
- def redirect_to(url)
9
- update :url => url
10
- end
11
-
12
- def cancel
13
- update :status => 'canceled'
14
- end
15
-
16
- def hangup
17
- update :status => 'completed'
18
- end
19
- end
20
- end
@@ -1,7 +0,0 @@
1
- module Twilio
2
- class Calls < ListResource
3
- def make(from, to, url)
4
- create :from => from, :to => to, :url => url
5
- end
6
- end
7
- end
@@ -1,59 +0,0 @@
1
- module Twilio
2
- class Client
3
- include Utils
4
-
5
- attr_reader :api_version, :domain, :account_sid, :account, :accounts
6
-
7
- def initialize(account_sid, auth_token, api_version='2010-04-01',
8
- domain='api.twilio.com', proxy=nil)
9
- @account_sid = account_sid
10
- @auth_token = auth_token
11
- @api_version = api_version
12
- set_up_connection_to domain, proxy
13
- set_up_subresources
14
- end
15
-
16
- # Define some helper methods for sending HTTP requests
17
- [:get, :put, :post, :delete].each do |method|
18
- method_class = Net::HTTP.const_get method.to_s.capitalize
19
- define_method method do |uri, *args|
20
- uri += '.json'
21
- params = (args[0] && args[0] != {}) ? twilify(args[0]) : nil
22
- uri += "?#{url_encode(params)}" if params && method == :get
23
- request = method_class.new uri
24
- request.basic_auth @account_sid, @auth_token
25
- request.form_data = params if params && [:post, :put].include?(method)
26
- http_response = @connection.request request
27
- object = Crack::JSON.parse http_response.body if http_response.body
28
- raise object['message'] unless http_response.kind_of? Net::HTTPSuccess
29
- object
30
- end
31
- end
32
-
33
- def request(uri, method, params={})
34
- send method.downcase.to_sym, uri, params
35
- end
36
-
37
- private
38
-
39
- def set_up_connection_to(domain, proxy)
40
- @connection = Net::HTTP.new domain, 443
41
- @connection.use_ssl = true
42
- # Don't check the server cert. Ideally this is configurable, in case an
43
- # app wants to verify that it is actually talking to the real Twilio.
44
- @connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
45
- end
46
-
47
- def set_up_subresources
48
- accounts_uri = "/#{@api_version}/Accounts"
49
- # Set up a special handle to grab the account.
50
- @account = Twilio::Account.new "#{accounts_uri}/#{@account_sid}", self
51
- # Set up the accounts subresource.
52
- @accounts = Twilio::Accounts.new accounts_uri, self
53
- end
54
-
55
- def url_encode(hash)
56
- hash.to_a.map {|pair| pair.map {|e| CGI.escape e.to_s}.join '='}.join '&'
57
- end
58
- end
59
- end
@@ -1,8 +0,0 @@
1
- module Twilio
2
- class Conference < InstanceResource
3
- def initialize(uri, client, params={})
4
- super uri, client, params
5
- resource :participants
6
- end
7
- end
8
- end
@@ -1,3 +0,0 @@
1
- module Twilio
2
- class Conferences < ListResource; end
3
- end
@@ -1,15 +0,0 @@
1
- module Twilio
2
- class Participant < InstanceResource
3
-
4
- def mute
5
- update :muted => 'true'
6
- end
7
-
8
- def unmute
9
- update :muted => 'false'
10
- end
11
-
12
- alias :kick :delete
13
-
14
- end
15
- end
@@ -1,3 +0,0 @@
1
- module Twilio
2
- class Participants < ListResource; end
3
- end
@@ -1,3 +0,0 @@
1
- module Twilio
2
- class IncomingPhoneNumber < InstanceResource; end
3
- end
@@ -1,7 +0,0 @@
1
- module Twilio
2
- class IncomingPhoneNumbers < ListResource
3
- def buy(phone_number)
4
- create :phone_number => phone_number
5
- end
6
- end
7
- end
@@ -1,53 +0,0 @@
1
- module Twilio
2
- class InstanceResource
3
- include Utils
4
-
5
- def initialize(uri, client, params={})
6
- @uri, @client = uri, client
7
- set_up_properties_from params
8
- end
9
-
10
- def update(params={})
11
- raise "Can't update a resource without a Twilio::Client" unless @client
12
- response = @client.post @uri, params
13
- set_up_properties_from response
14
- self
15
- end
16
-
17
- def delete
18
- raise "Can't delete a resource without a Twilio::Client" unless @client
19
- @client.delete @uri
20
- end
21
-
22
- def method_missing(method, *args)
23
- super if @updated
24
- response = @client.get @uri
25
- set_up_properties_from response
26
- self.send method, *args
27
- end
28
-
29
- protected
30
-
31
- def set_up_properties_from(hash)
32
- eigenclass = class << self; self; end
33
- hash.each do |p,v|
34
- property = detwilify p
35
- unless ['uri', 'client', 'updated'].include? property
36
- eigenclass.send :define_method, property.to_sym, &lambda {v}
37
- end
38
- end
39
- @updated = !hash.keys.empty?
40
- end
41
-
42
- def resource(*resources)
43
- resources.each do |r|
44
- resource = twilify r
45
- relative_uri = r == :sms_messages ? 'SMS/Messages' : resource
46
- instance_variable_set("@#{r}",
47
- Twilio.const_get(resource).new("#{@uri}/#{relative_uri}", @client))
48
- end
49
- self.class.instance_eval {attr_reader *resources}
50
- end
51
-
52
- end
53
- end
@@ -1,33 +0,0 @@
1
- module Twilio
2
- class ListResource
3
- include Utils
4
-
5
- def initialize(uri, client)
6
- @resource_name = self.class.name.split('::')[-1]
7
- @instance_class = Twilio.const_get @resource_name.chop
8
- @uri, @client = uri, client
9
- end
10
-
11
- # Grab a list of this kind of resource and return it as an array.
12
- def list(params={})
13
- raise "Can't get a resource list without a Twilio::Client" unless @client
14
- response = @client.get @uri, params
15
- resources = response[detwilify(@resource_name)]
16
- resources.map do |resource|
17
- @instance_class.new "#{@uri}/#{resource['sid']}", @client, resource
18
- end
19
- end
20
-
21
- # Return an empty instance resource object with the proper URI.
22
- def get(sid)
23
- @instance_class.new "#{@uri}/#{sid}", @client
24
- end
25
-
26
- # Return a newly created resource.
27
- def create(params={})
28
- raise "Can't create a resource without a Twilio::Client" unless @client
29
- response = @client.post @uri, params
30
- @instance_class.new "#{@uri}/#{response['sid']}", @client, response
31
- end
32
- end
33
- end
@@ -1,3 +0,0 @@
1
- module Twilio
2
- class Notification < InstanceResource; end
3
- end
@@ -1,3 +0,0 @@
1
- module Twilio
2
- class Notifications < ListResource; end
3
- end
@@ -1,3 +0,0 @@
1
- module Twilio
2
- class OutgoingCallerId < InstanceResource; end
3
- end
@@ -1,7 +0,0 @@
1
- module Twilio
2
- class OutgoingCallerIds < ListResource
3
- def add(phone_number)
4
- create :phone_number => phone_number
5
- end
6
- end
7
- end
@@ -1,10 +0,0 @@
1
- module Twilio
2
- class Recording < InstanceResource
3
-
4
- def initialize(uri, client, params={})
5
- super uri, client, params
6
- resource :transcriptions
7
- end
8
-
9
- end
10
- end
@@ -1,3 +0,0 @@
1
- module Twilio
2
- class Recordings < ListResource; end
3
- end
@@ -1,3 +0,0 @@
1
- module Twilio
2
- class Sandbox < InstanceResource; end
3
- end
@@ -1,3 +0,0 @@
1
- module Twilio
2
- class SmsMessage < InstanceResource; end
3
- end
@@ -1,7 +0,0 @@
1
- module Twilio
2
- class SmsMessages < ListResource
3
- def send(from, to, body)
4
- create :from => from, :to => to, :body => body
5
- end
6
- end
7
- end
@@ -1,3 +0,0 @@
1
- module Twilio
2
- class Transcription < InstanceResource; end
3
- end
@@ -1,3 +0,0 @@
1
- module Twilio
2
- class Transcriptions < ListResource; end
3
- end
@@ -1,23 +0,0 @@
1
- module Twilio
2
- module Utils
3
-
4
- def twilify(something)
5
- if something.is_a? Hash
6
- Hash[*something.to_a.map {|a| [twilify(a[0]).to_sym, a[1]]}.flatten]
7
- else
8
- something.to_s.split('_').map do |s|
9
- [s[0,1].capitalize, s[1..-1]].join
10
- end.join
11
- end
12
- end
13
-
14
- def detwilify(something)
15
- if something.is_a? Hash
16
- Hash[*something.to_a.map {|pair| [detwilify(pair[0]).to_sym, pair[1]]}.flatten]
17
- else
18
- something.to_s.gsub(/[A-Z][a-z]*/) {|s| "_#{s.downcase}"}.gsub(/^_/, '')
19
- end
20
- end
21
-
22
- end
23
- end