-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathservice.rb
More file actions
257 lines (228 loc) · 9.08 KB
/
service.rb
File metadata and controls
257 lines (228 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# Licensed by AT&T under 'Software Development Kit Tools Agreement.' 2014 TERMS
# AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION:
# http://developer.att.com/sdk_agreement/ Copyright 2014 AT&T Intellectual
# Property. All rights reserved. http://developer.att.com For more information
# contact developer.support@att.com
#@author kh455g
module Att
module Codekit
module Service
class ServiceException < Exception; end
# Helper class which contains common code for use with the AT&T Cloud API.
class CloudService
include Att::Codekit::Auth
attr_reader :fqdn, :token
# Creates a base service
#
# @param token [OAuthToken] the oauth token used to authenticate
# @param fqdn [String] url that points to where the cloud api exists
def initialize(fqdn, token, client=nil)
@token = token
@fqdn = fqdn
@client = client
end
# Send a post request with standard headers
#
# @param url [String] The url to send the request to
# @param payload [String] The body of the post request
# @param custom_headers [Hash] A hash of headers that override/extend
# the defaults
#
# @return [RestClient::Response] http response object
def post(url, payload, custom_headers={})
headers = {
:Accept => "application/json",
:Content_Type => "application/json",
:Authorization => "Bearer #{@token.access_token}"
}
headers.merge!(custom_headers)
Transport.post url, payload, headers
end
# Send a get request with standard headers
#
# @param url [String] The url to send the request to
# @param custom_headers [Hash] A hash of headers that override/extend
# the defaults
#
# @return [RestClient::Response] http response object
def get(url, custom_headers={})
headers = {
:Accept => "application/json",
:Authorization => "Bearer #{@token.access_token}",
}
headers.merge!(custom_headers)
Transport.get url, headers
end
# Send a Http put request with standard headers
#
# @param url [String] The url to send the request to
# @param payload [String] The data to send to the url
# @param custom_headers [Hash] A hash of headers that override/extend
# the defaults
#
# @return [RestClient::Response] http response object
def patch(url, payload, custom_headers={})
headers = {
:Accept => "application/json",
:Content_Type => 'application/json',
:Authorization => "Bearer #{@token.access_token}",
}
headers.merge!(custom_headers)
Transport.patch url, payload, headers
end
# Send a Http put request with standard headers
#
# @param url [String] The url to send the request to
# @param payload [String] The data to send to the url
# @param custom_headers [Hash] A hash of headers that override/extend
# the defaults
#
# @return [RestClient::Response] http response object
def put(url, payload, custom_headers={})
headers = {
:Accept => "application/json",
:Content_Type => 'application/json',
:Authorization => "Bearer #{@token.access_token}",
}
headers.merge!(custom_headers)
Transport.put url, payload, headers
end
# Send a Http delete request with standard headers
#
# @param url [String] The url to send the request to
# @param custom_headers [Hash] A hash of headers that override/extend
# the defaults
#
# @return [RestClient::Response] http response object
def delete(url, custom_headers={})
headers = {
:Accept => "application/json",
:Authorization => "Bearer #{@token.access_token}",
}
headers.merge!(custom_headers)
Transport.delete url, headers
end
# Transform a list of addresses into a format acceptable by the api
#
# @note If the prefix tel: or short: is not present, then a best guess
# based on the length is made.
#
# @param addresses [String, Array<String>] Comma seperated string or
# array of phone numbers
#
# @return [Array<String>] an array of acceptable addresses
def self.format_addresses(addresses)
# convert any input to an array of strings
addresses = Array(addresses).join(",").split(",")
parsed_addresses = Array.new
addresses.each do |a|
a = a.gsub("-","").strip
unless a.empty?
if a.include?("@") || a.include?("tel:") || a.include?("short:")
parsed_addresses << a
elsif a.length <= 8
parsed_addresses << "short:#{a}"
else
parsed_addresses << "tel:#{a}"
end
end
end
parsed_addresses
end
# Transform a Hash of key, value pairs into a query string
#
# @param query_params [Hash<String>]
#
# @return [String] a query string in the form of
# "key1=value1&key2=value2"
def self.query_param_string(query_params)
parameters = Array.new
query_params.each do |key,value|
unless value.nil?
parameters << %(#{key}=#{CGI.escape(Array(value).join(","))})
end
end
parameters.join("&")
end
# Internal function used to generate random boundary
def self.generateBoundary
"----#{(rand*10_000_000) + 10_000_000}.#{Time.now.to_i}"
end
# Construct a multipart body that's compatible with the AT&T Cloud API
#
# @param boundary [String] a random string to split chunks on
# @param data [Array<Hash>,Hash] An array of hashes or a single hash, each
# hash represents a seperate chunk of data.
#
# @option data [Hash] :headers key value pairs, where key is the header
# and value is the headers value
# @option data [String] :data the actual object (payload) to send
#
# @return [String] A payload that can be sent to the AT&T Cloud API
def self.generateMultiPart(boundary, data)
body = ""
tmp_data = data.is_a?(Hash) ? [data] : Array(data)
tmp_data.each do |part|
body += "--#{boundary}\r\n"
part[:headers].each do |key, value|
body += "#{key}: #{value}\r\n"
end
body += "\r\n#{part[:data].force_encoding("UTF-8")}\r\n\r\n"
end
body += "--#{boundary}--\r\n"
end
# Simple file extension checking to obtain filetype
#
# @note This method should not be used outside of the services as it
# will eventually be removed and updated with libmagic.
# The current speech api does not accept all the current values
# returned from libmagic so a custom function is present here.
#
# @param file [String] the path or name of the file to check type
#
# @return [String] the mime type of the file
def self.getMimeType(file)
extension = file.split(".").last
case extension
when "txt" then "text/plain"
when "html" then "text/html"
when "xml" then "application/xml"
when "ssml" then "application/ssml+xml"
when "jpg", "jpeg", "jpe" then "image/jpeg"
when "png" then "image/png"
when "gif" then "image/gif"
when "bmp" then "image/bmp"
when "tif", "tiff" then "image/tiff"
when "mov", "qt" then "video/quicktime"
when "mpa", "mpe", "mpeg", "mpg", "mpv2" then "video/mpeg"
when "mp4" then "video/mp4"
when "avi" then "video/x-msvideo"
when "asf", "asr", "asx" then "video/x-ms-asf"
when "amr" then "audio/amr"
when "awb" then "audio/amr-wb"
when "aif", "aifc", "aiff" then "audio/x-aiff"
when "au", "snd" then "audio/basic"
when "m3u" then "audio/x-mpegurl"
when "mid","midi", "rmi" then "audio/midi"
when "mp3" then "audio/mpeg"
when "wav" then "audio/x-wav"
when "ogg", "oga" then "audio/ogg"
when "spx" then "audio/x-speex"
when "raw" then "audio/raw"
when "3gp" then "audio/3gpp"
else raise "Unsupported media type extension, file: #{file}"
end
end
end
require_relative "service/ads"
require_relative "service/speech"
require_relative "service/tts"
require_relative "service/dc"
require_relative "service/mim"
require_relative "service/immn"
require_relative "service/sms"
require_relative "service/mms"
require_relative "service/payment"
end
end
end