telapi 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/.gitignore +20 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +22 -0
  4. data/README.md +162 -0
  5. data/Rakefile +1 -0
  6. data/lib/telapi.rb +24 -0
  7. data/lib/telapi/account.rb +14 -0
  8. data/lib/telapi/application.rb +88 -0
  9. data/lib/telapi/available_phone_number.rb +24 -0
  10. data/lib/telapi/call.rb +214 -0
  11. data/lib/telapi/caller_id.rb +16 -0
  12. data/lib/telapi/carrier.rb +16 -0
  13. data/lib/telapi/conference.rb +206 -0
  14. data/lib/telapi/configuration.rb +40 -0
  15. data/lib/telapi/error.rb +22 -0
  16. data/lib/telapi/fraud.rb +79 -0
  17. data/lib/telapi/inbound_xml.rb +36 -0
  18. data/lib/telapi/incoming_phone_number.rb +86 -0
  19. data/lib/telapi/message.rb +47 -0
  20. data/lib/telapi/network.rb +50 -0
  21. data/lib/telapi/notification.rb +29 -0
  22. data/lib/telapi/recording.rb +69 -0
  23. data/lib/telapi/resource.rb +18 -0
  24. data/lib/telapi/resource_collection.rb +38 -0
  25. data/lib/telapi/transcription.rb +44 -0
  26. data/lib/telapi/version.rb +3 -0
  27. data/spec/spec_helper.rb +17 -0
  28. data/spec/support/telapi_helpers.rb +38 -0
  29. data/spec/telapi/account_spec.rb +17 -0
  30. data/spec/telapi/application_spec.rb +69 -0
  31. data/spec/telapi/available_phone_number_spec.rb +27 -0
  32. data/spec/telapi/call_spec.rb +173 -0
  33. data/spec/telapi/caller_id_spec.rb +17 -0
  34. data/spec/telapi/carrier_spec.rb +17 -0
  35. data/spec/telapi/conference_spec.rb +174 -0
  36. data/spec/telapi/configuration_spec.rb +38 -0
  37. data/spec/telapi/error_spec.rb +32 -0
  38. data/spec/telapi/fraud_spec.rb +55 -0
  39. data/spec/telapi/inbound_xml_spec.rb +49 -0
  40. data/spec/telapi/incoming_phone_number_spec.rb +69 -0
  41. data/spec/telapi/message_spec.rb +41 -0
  42. data/spec/telapi/network_spec.rb +80 -0
  43. data/spec/telapi/notification_spec.rb +34 -0
  44. data/spec/telapi/recording_spec.rb +72 -0
  45. data/spec/telapi/resource_collection_spec.rb +64 -0
  46. data/spec/telapi/resource_spec.rb +25 -0
  47. data/spec/telapi/transcription_spec.rb +41 -0
  48. data/telapi-ruby.gemspec +32 -0
  49. metadata +227 -0
@@ -0,0 +1,16 @@
1
+ module Telapi
2
+ # Wraps TelAPI CNAM Caller ID functionality
3
+ class CallerId < Resource
4
+ class << self
5
+
6
+ # Returns a Telapi::CallerId object given a phone number
7
+ # See http://www.telapi.com/docs/api/rest/carrier-services/cnam-lookup/
8
+ def lookup(phone_number)
9
+ opts = { :PhoneNumber => phone_number }
10
+ response = Network.get(['CNAM'], opts)
11
+ CallerId.new(response)
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module Telapi
2
+ # Wraps TelAPI Carrier functionality
3
+ class Carrier < Resource
4
+ class << self
5
+
6
+ # Returns a Telapi::Carrier object given a phone number
7
+ # See http://www.telapi.com/docs/api/rest/carrier-services/carrier-lookup/
8
+ def lookup(phone_number)
9
+ opts = { :PhoneNumber => phone_number }
10
+ response = Network.get(['Carrier'], opts)
11
+ Carrier.new(response)
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,206 @@
1
+ module Telapi
2
+ # Wraps TelAPI Conference functionality
3
+ class Conference < Resource
4
+ class << self
5
+
6
+ # Returns a resource collection containing Telapi::Conference objects
7
+ # See http://www.telapi.com/docs/api/rest/conferences/list/
8
+ #
9
+ # Optional params is a hash containing:
10
+ # +MemberID+:: string
11
+ # +Muted+:: true or false
12
+ # +Deafed+:: true or false
13
+ # +Page+:: integer greater than 0
14
+ # +PageSize+:: integer greater than 0
15
+ def list(optional_params = {})
16
+ response = Network.get(['Conferences'], optional_params)
17
+ ResourceCollection.new(response, 'conferences', self)
18
+ end
19
+
20
+ # Returns a Telapi::Conference object given its name
21
+ # See http://www.telapi.com/docs/api/rest/conferences/view/
22
+ #
23
+ # Required params:
24
+ # +conference_name+:: conference name
25
+ #
26
+ # Optional params is a hash containing:
27
+ # +MemberID+:: string
28
+ # +Muted+:: true or (false)
29
+ # +Deafed+:: true or (false)
30
+ def get(conference_name, optional_params = {})
31
+ response = Network.get(['Conferences', conference_name], optional_params)
32
+ Conference.new(response)
33
+ end
34
+
35
+ # Mute a member of a conference call, returning a Telapi::Conference object
36
+ # See http://www.telapi.com/docs/api/rest/conferences/mute/
37
+ #
38
+ # Required params:
39
+ # +conference_name+:: conference name
40
+ # +member_id+:: string
41
+ def mute_member(conference_name, member_id)
42
+ opts = { :MemberID => member_id }
43
+ response = Network.post(['Conferences', conference_name, 'Mute'], opts)
44
+ Conference.new(response)
45
+ end
46
+
47
+ # Unmute a member of a conference call, returning a Telapi::Conference object
48
+ # See http://www.telapi.com/docs/api/rest/conferences/unmute/
49
+ #
50
+ # Required params:
51
+ # +conference_name+:: conference name
52
+ # +member_id+:: string
53
+ def unmute_member(conference_name, member_id)
54
+ opts = { :MemberID => member_id }
55
+ response = Network.post(['Conferences', conference_name, 'UnMute'], opts)
56
+ Conference.new(response)
57
+ end
58
+
59
+ # Deaf audio for a specific member of a conference call, returning a Telapi::Conference object
60
+ # See http://www.telapi.com/docs/api/rest/conferences/deaf/
61
+ #
62
+ # Required params:
63
+ # +conference_name+:: conference name
64
+ # +member_id+:: string
65
+ def deaf_member(conference_name, member_id)
66
+ opts = { :MemberID => member_id }
67
+ response = Network.post(['Conferences', conference_name, 'Deaf'], opts)
68
+ Conference.new(response)
69
+ end
70
+
71
+ # Restore audio for a specific member of a conference call, returning a Telapi::Conference object
72
+ # See http://www.telapi.com/docs/api/rest/conferences/undeaf/
73
+ #
74
+ # Required params:
75
+ # +conference_name+:: conference name
76
+ # +member_id+:: string
77
+ def undeaf_member(conference_name, member_id)
78
+ opts = { :MemberID => member_id }
79
+ response = Network.post(['Conferences', conference_name, 'UnDeaf'], opts)
80
+ Conference.new(response)
81
+ end
82
+
83
+ # Hangup a specific member of a conference call, returning a Telapi::Conference object
84
+ # See http://www.telapi.com/docs/api/rest/conferences/hangup/
85
+ #
86
+ # Required params:
87
+ # +conference_name+:: conference name
88
+ # +member_id+:: string
89
+ def hangup_member(conference_name, member_id)
90
+ opts = { :MemberID => member_id }
91
+ response = Network.post(['Conferences', conference_name, 'Hangup'], opts)
92
+ Conference.new(response)
93
+ end
94
+
95
+ # Remove a specific member of a conference call, returning a Telapi::Conference object
96
+ # See http://www.telapi.com/docs/api/rest/conferences/kick/
97
+ #
98
+ # Required params:
99
+ # +conference_name+:: conference name
100
+ # +member_id+:: string
101
+ def kick_member(conference_name, member_id)
102
+ opts = { :MemberID => member_id }
103
+ response = Network.post(['Conferences', conference_name, 'Kick'], opts)
104
+ Conference.new(response)
105
+ end
106
+
107
+ # Send message that will be read in an audible voice to conference members, returning a Telapi::Conference object
108
+ # See http://www.telapi.com/docs/api/rest/conferences/speak-text/
109
+ #
110
+ # Required params:
111
+ # +conference_name+:: conference name
112
+ # +member_id+:: string
113
+ # +text+:: string
114
+ def speak_text(conference_name, member_id, text)
115
+ opts = { :MemberID => member_id, :Text => text }
116
+ response = Network.post(['Conferences', conference_name, 'Say'], opts)
117
+ Conference.new(response)
118
+ end
119
+
120
+ # Play pre-recorded sound from a file to conference members, returning a Telapi::Conference object
121
+ # See http://www.telapi.com/docs/api/rest/conferences/play-audio/
122
+ #
123
+ # Required params:
124
+ # +conference_name+:: conference name
125
+ # +member_id+:: string
126
+ # +sound_url+:: valid URL
127
+ def play_audio(conference_name, member_id, sound_url)
128
+ opts = { :MemberID => member_id, :Url => sound_url }
129
+ response = Network.post(['Conferences', conference_name, 'Play'], opts)
130
+ Conference.new(response)
131
+ end
132
+
133
+ # Record conference, returning a Telapi::Conference object
134
+ # See http://www.telapi.com/docs/api/rest/conferences/start-recording/
135
+ #
136
+ # Required params:
137
+ # +conference_name+:: conference name
138
+ def start_recording(conference_name)
139
+ response = Network.post(['Conferences', conference_name, 'RecordStart'])
140
+ Conference.new(response)
141
+ end
142
+
143
+ # End recording of conference, returning a Telapi::Conference object
144
+ # See http://www.telapi.com/docs/api/rest/conferences/stop-recording/
145
+ #
146
+ # Required params:
147
+ # +conference_name+:: conference name
148
+ def stop_recording(conference_name)
149
+ response = Network.post(['Conferences', conference_name, 'RecordStop'])
150
+ Conference.new(response)
151
+ end
152
+
153
+ end
154
+
155
+ # See ::mute_member
156
+ def mute_member(member_id)
157
+ self.class.mute_member(self.name, member_id)
158
+ end
159
+
160
+ # See ::unmute_member
161
+ def unmute_member(member_id)
162
+ self.class.unmute_member(self.name, member_id)
163
+ end
164
+
165
+ # See ::deaf_member
166
+ def deaf_member(member_id)
167
+ self.class.deaf_member(self.name, member_id)
168
+ end
169
+
170
+ # See ::undeaf_member
171
+ def undeaf_member(member_id)
172
+ self.class.undeaf_member(self.name, member_id)
173
+ end
174
+
175
+ # See ::hangup_member
176
+ def hangup_member(member_id)
177
+ self.class.hangup_member(self.name, member_id)
178
+ end
179
+
180
+ # See ::kick_member
181
+ def kick_member(member_id)
182
+ self.class.kick_member(self.name, member_id)
183
+ end
184
+
185
+ # See ::speak_text
186
+ def speak_text(member_id, text)
187
+ self.class.speak_text(self.name, member_id, text)
188
+ end
189
+
190
+ # See ::play_audio
191
+ def play_audio(member_id, sound_url)
192
+ self.class.play_audio(self.name, member_id, sound_url)
193
+ end
194
+
195
+ # See ::start_recording
196
+ def start_recording
197
+ self.class.start_recording(self.name)
198
+ end
199
+
200
+ # See ::stop_recording
201
+ def stop_recording
202
+ self.class.stop_recording(self.name)
203
+ end
204
+
205
+ end
206
+ end
@@ -0,0 +1,40 @@
1
+ require 'ostruct'
2
+
3
+ module Telapi
4
+ class Configuration < OpenStruct #:nodoc:
5
+ def initialize
6
+ super(
7
+ :base_uri => 'https://api.telapi.com/2011-07-01',
8
+ :ssl_ca_path => '/etc/ssl/certs',
9
+ :account_sid => nil,
10
+ :auth_token => nil
11
+ )
12
+ end
13
+
14
+ def inspect
15
+ marshal_dump
16
+ end
17
+ alias_method :to_s, :inspect
18
+ end
19
+
20
+ # Change the default Telapi configuration by setting an individual key or passing in a configuration block.
21
+ #
22
+ # # set individual keys
23
+ # Telapi.config.ssl_ca_path = '/some/path'
24
+ #
25
+ # # or use a block
26
+ # Telapi.config do |config|
27
+ # config.ssl_ca_path = '/some/path'
28
+ # config.account_sid = 'abc123'
29
+ # config.auth_token = 'xyz567'
30
+ # end
31
+ #
32
+ # To get the current configuration as a hash:
33
+ #
34
+ # p Telapi.config
35
+ # # => {:base_uri=>"https://api.telapi.com/2011-07-01/", :ssl_ca_path=>"/etc/ssl/certs", :account_sid=>"abc123", :auth_token=>"xyz567"}
36
+ def self.config
37
+ @config ||= Configuration.new
38
+ block_given? ? yield(@config) : @config
39
+ end
40
+ end
@@ -0,0 +1,22 @@
1
+ module Telapi
2
+ class ApiError < StandardError
3
+ attr_accessor :status, :code, :help_uri
4
+
5
+ def initialize(attributes)
6
+ super attributes['message']
7
+ @status = attributes['status']
8
+ @code = attributes['code']
9
+ @help_uri = attributes['more_info']
10
+ end
11
+ end
12
+
13
+ class InvalidConfiguration < StandardError
14
+ def initialize(attribute)
15
+ @attribute = attribute
16
+ end
17
+
18
+ def message
19
+ "The following configuration setting is not properly set: #{@attribute}"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,79 @@
1
+ module Telapi
2
+ # Wraps TelAPI Fraud control functionality
3
+ class Fraud < Resource
4
+ class << self
5
+
6
+ # Returns a resource collection containing Telapi::Fraud objects
7
+ # See http://www.telapi.com/docs/api/rest/fraud-control/list/
8
+ #
9
+ # Optional params is a hash containing:
10
+ # +Page+:: integer greater than 0
11
+ # +PageSize+:: integer greater than 0
12
+ def list(optional_params = {})
13
+ response = Network.get(['Fraud'], optional_params)
14
+ ResourceCollection.new(response, 'frauds', self)
15
+ end
16
+
17
+ # Authorizes destination for outbound calls and sms messages, returns a Telapi::Fraud object
18
+ # See http://www.telapi.com/docs/api/rest/fraud-control/authorize/
19
+ #
20
+ # Required params:
21
+ # +country_code_iso+:: country code (ISO), see http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
22
+ #
23
+ # Optional params is a hash containing:
24
+ # +MobileBreakout+:: (true) or false
25
+ # +LandlineBreakout+:: (true) or false
26
+ # +SmsEnabled+:: (true) or false
27
+ def authorize(country_code_iso, optional_params = {})
28
+ response = Network.post(['Fraud', 'Authorize', country_code_iso], optional_params)
29
+ Fraud.new(response)
30
+ end
31
+
32
+ # Extend authorized destination for outbound calls and sms messages, returns a Telapi::Fraud object
33
+ # See http://www.telapi.com/docs/api/rest/fraud-control/extend/
34
+ #
35
+ # Required params:
36
+ # +country_code_iso+:: country code (ISO), see http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
37
+ #
38
+ # Optional params is a hash containing:
39
+ # +MobileBreakout+:: (true) or false
40
+ # +LandlineBreakout+:: (true) or false
41
+ # +SmsEnabled+:: (true) or false
42
+ def extend_authorization(country_code_iso, optional_params = {})
43
+ response = Network.post(['Fraud', 'Extend', country_code_iso], optional_params)
44
+ Fraud.new(response)
45
+ end
46
+
47
+ # Blocks destination for outbound calls and sms messages, returns a Telapi::Fraud object
48
+ # See http://www.telapi.com/docs/api/rest/fraud-control/block/
49
+ #
50
+ # Required params:
51
+ # +country_code_iso+:: country code (ISO), see http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
52
+ #
53
+ # Optional params is a hash containing:
54
+ # +MobileBreakout+:: (true) or false
55
+ # +LandlineBreakout+:: (true) or false
56
+ # +SmsEnabled+:: (true) or false
57
+ def block(country_code_iso, optional_params = {})
58
+ response = Network.post(['Fraud', 'Block', country_code_iso], optional_params)
59
+ Fraud.new(response)
60
+ end
61
+
62
+ # Whitelist destination for outbound calls and sms messages, returns a Telapi::Fraud object
63
+ # See http://www.telapi.com/docs/api/rest/fraud-control/whitelist/
64
+ #
65
+ # Required params:
66
+ # +country_code_iso+:: country code (ISO), see http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
67
+ #
68
+ # Optional params is a hash containing:
69
+ # +MobileBreakout+:: (true) or false
70
+ # +LandlineBreakout+:: (true) or false
71
+ # +SmsEnabled+:: (true) or false
72
+ def whitelist(country_code_iso, optional_params = {})
73
+ response = Network.post(['Fraud', 'Whitelist', country_code_iso], optional_params)
74
+ Fraud.new(response)
75
+ end
76
+
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,36 @@
1
+ require 'nokogiri'
2
+
3
+ module Telapi
4
+ # Wraps TelAPI InboundXML functionality
5
+ # See http://www.telapi.com/docs/api/inboundxml/voice/introduction/
6
+ class InboundXml
7
+
8
+ # Internally Nokogiri builder is being used to construct responses.
9
+ # See http://nokogiri.org/Nokogiri/XML/Builder.html
10
+ #
11
+ # Pass in a block of options:
12
+ #
13
+ # Telapi::InboundXml.new do
14
+ # Say('Hello.', :loop => 3, :voice => 'man')
15
+ # Say('Hello, my name is Jane.', :voice => 'woman')
16
+ # Say('Now I will not stop talking.', :loop => 0)
17
+ # end
18
+ #
19
+ # Then calling #response produces the following output:
20
+ #
21
+ # <Response>
22
+ # <Say loop='3' voice='man'>Hello.</Say>
23
+ # <Say voice='woman'>Hello, my name is Jane.</Say>
24
+ # <Say loop='0'>Now I will not stop talking.</Say>
25
+ # </Response>
26
+ def initialize(&block)
27
+ @xml = Nokogiri::XML::Builder.new
28
+ @xml.Response &block
29
+ end
30
+
31
+ # Returns xml
32
+ def response
33
+ @xml.to_xml
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,86 @@
1
+ module Telapi
2
+ # Wraps TelAPI Incoming Phone Number functionality
3
+ class IncomingPhoneNumber < Resource
4
+ class << self
5
+
6
+ # Returns a resource collection containing Telapi::IncomingPhoneNumber objects
7
+ # See http://www.telapi.com/docs/api/rest/incoming-phone-numbers/list/
8
+ #
9
+ # Optional params is a hash containing:
10
+ # +PhoneNumber+:: valid TelAPI phone number, e.g. 7325551234
11
+ # +FriendlyName+:: valid domestic format phone number, e.g. 7325551234
12
+ # +Page+:: integer greater than 0
13
+ # +PageSize+:: integer greater than 0
14
+ def list(optional_params = {})
15
+ response = Network.get(['IncomingPhoneNumbers'], optional_params)
16
+ ResourceCollection.new(response, 'incoming_phone_numbers', self)
17
+ end
18
+
19
+ # Returns a Telapi::IncomingPhoneNumber object given its id
20
+ # See http://www.telapi.com/docs/api/rest/incoming-phone-numbers/view/
21
+ def get(id)
22
+ response = Network.get(['IncomingPhoneNumbers', id])
23
+ IncomingPhoneNumber.new(response)
24
+ end
25
+
26
+ # Creates a new incoming phone number, returning a Telapi::IncomingPhoneNumber object
27
+ # See http://www.telapi.com/docs/api/rest/incoming-phone-numbers/add-or-delete/
28
+ #
29
+ # Required params:
30
+ # +phone_number+:: area code, e.g. 18052585701
31
+ def create(phone_number)
32
+ opts = { :PhoneNumber => phone_number }
33
+ response = Network.post(['IncomingPhoneNumbers'], opts)
34
+ IncomingPhoneNumber.new(response)
35
+ end
36
+
37
+ # Delete an incoming phone number, returning a Telapi::IncomingPhoneNumber object
38
+ # See http://www.telapi.com/docs/api/rest/incoming-phone-numbers/add-or-delete/
39
+ #
40
+ # Required params:
41
+ # +id+:: incoming phone number id
42
+ def delete(id)
43
+ response = Network.delete(['IncomingPhoneNumbers', id])
44
+ IncomingPhoneNumber.new(response)
45
+ end
46
+
47
+ # Updates an incoming phone number, returning a Telapi::IncomingPhoneNumber object
48
+ # See http://www.telapi.com/docs/api/rest/incoming-phone-numbers/update/
49
+ #
50
+ # Required params:
51
+ # +id+:: incoming phone number id
52
+ #
53
+ # Optional params is a hash containing:
54
+ # +FriendlyName+:: string
55
+ # +VoiceUrl+:: valid URL
56
+ # +VoiceMethod+:: (POST) or GET
57
+ # +VoiceFallbackUrl+:: valid URL
58
+ # +VoiceFallbackMethod+:: (POST) or GET
59
+ # +VoiceCallerIdLookup+:: true or (false)
60
+ # +SmsUrl+:: valid URL
61
+ # +SmsMethod+:: (POST) or GET
62
+ # +SmsFallbackUrl+:: valid URL
63
+ # +SmsFallbackMethod+:: (POST) or GET
64
+ # +HeartbeatUrl+:: valid URL
65
+ # +HeartbeatMethod+:: (POST) or GET
66
+ # +StatusCallback+:: valid URL
67
+ # +StatusCallbackMethod+:: (POST) or GET
68
+ def update(id, optional_params = {})
69
+ response = Network.post(['IncomingPhoneNumbers', id], optional_params)
70
+ IncomingPhoneNumber.new(response)
71
+ end
72
+
73
+ end
74
+
75
+ # See ::delete
76
+ def delete
77
+ self.class.delete(self.sid)
78
+ end
79
+
80
+ # See ::update
81
+ def update(optional_params = {})
82
+ self.class.update(self.sid, optional_params)
83
+ end
84
+
85
+ end
86
+ end