cielo24 0.0.13 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,10 @@
1
- require 'cielo24/version'
2
- require 'cielo24/actions'
3
- require 'cielo24/enums'
4
- require 'cielo24/options'
5
- require 'cielo24/web_utils'
6
-
7
- require 'hashie'
8
- require 'httpclient'
9
-
10
- module Cielo24
11
- end
1
+ require 'cielo24/actions'
2
+ require 'cielo24/enums'
3
+ require 'cielo24/options'
4
+ require 'cielo24/web_utils'
5
+
6
+ require 'hashie'
7
+ require 'httpclient'
8
+
9
+ module Cielo24
10
+ end
@@ -1,246 +1,250 @@
1
- module Cielo24
2
- class Actions
3
-
4
- require 'uri'
5
- require 'hashie'
6
- require_relative 'web_utils'
7
- require_relative 'enums'
8
- include Errno
9
- include Hashie
10
- include Cielo24
11
-
12
- attr_accessor :base_url
13
-
14
- API_VERSION = 1
15
-
16
- LOGIN_PATH = "/api/account/login"
17
- LOGOUT_PATH = "/api/account/logout"
18
- UPDATE_PASSWORD_PATH = "/api/account/update_password"
19
- GENERATE_API_KEY_PATH = "/api/account/generate_api_key"
20
- REMOVE_API_KEY_PATH = "/api/account/remove_api_key"
21
- CREATE_JOB_PATH = "/api/job/new"
22
- AUTHORIZE_JOB_PATH = "/api/job/authorize"
23
- DELETE_JOB_PATH = "/api/job/del"
24
- GET_JOB_INFO_PATH = "/api/job/info"
25
- GET_JOB_LIST_PATH = "/api/job/list"
26
- ADD_MEDIA_TO_JOB_PATH = "/api/job/add_media"
27
- ADD_EMBEDDED_MEDIA_TO_JOB_PATH = "/api/job/add_media_url"
28
- GET_MEDIA_PATH = "/api/job/media"
29
- PERFORM_TRANSCRIPTION = "/api/job/perform_transcription"
30
- GET_TRANSCRIPTION_PATH = "/api/job/get_transcript"
31
- GET_CAPTION_PATH = "/api/job/get_caption"
32
- GET_ELEMENT_LIST_PATH = "/api/job/get_elementlist"
33
- GET_LIST_OF_ELEMENT_LISTS_PATH = "/api/job/list_elementlists"
34
-
35
- def initialize(base_url="https://api.cielo24.com")
36
- @base_url = base_url
37
- end
38
-
39
- ### ACCESS CONTROL ###
40
-
41
- def login(username, password=nil, api_securekey=nil, use_headers=false)
42
- assert_argument(username, "Username")
43
- raise ArgumentError.new("Password or API Secure Key must be supplied for login.") if (password.nil? and api_securekey.nil?)
44
-
45
- query_hash = init_version_dict
46
- headers = Hash.new
47
-
48
- if not use_headers
49
- query_hash[:username] = username
50
- query_hash[:password] = password if (!password.nil?)
51
- query_hash[:securekey] = api_securekey if (!api_securekey.nil?)
52
- else
53
- headers[:'x-auth-user'] = username
54
- headers[:'x-auth-key'] = password if (!password.nil?)
55
- headers[:'x-auth-securekey'] = api_securekey if (!api_securekey.nil?)
56
- end
57
-
58
- json = WebUtils.get_json(@base_url + LOGIN_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash, headers)
59
- return json["ApiToken"]
60
- end
61
-
62
- def logout(api_token)
63
- query_hash = init_access_req_dict(api_token)
64
- # Nothing returned
65
- WebUtils.http_request(@base_url + LOGOUT_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
66
- end
67
-
68
- def update_password(api_token, new_password)
69
- assert_argument(new_password, "New Password")
70
- query_hash = init_access_req_dict(api_token)
71
- query_hash[:new_password] = new_password
72
-
73
- # Nothing returned
74
- WebUtils.http_request(@base_url + UPDATE_PASSWORD_PATH, 'POST', WebUtils::BASIC_TIMEOUT, nil, nil, query_hash)
75
- end
76
-
77
- def generate_api_key(api_token, username, force_new=false)
78
- assert_argument(username, "Username")
79
- assert_argument(api_token, "API Token")
80
- query_hash = init_access_req_dict(api_token)
81
- query_hash[:account_id] = username
82
- query_hash[:force_new] = force_new
83
-
84
- json = WebUtils.get_json(@base_url + GENERATE_API_KEY_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
85
- return json["ApiKey"]
86
- end
87
-
88
- def remove_api_key(api_token, api_securekey)
89
- query_hash = init_access_req_dict(api_token)
90
- query_hash[:api_securekey] = api_securekey
91
-
92
- # Nothing returned
93
- WebUtils.http_request(@base_url + REMOVE_API_KEY_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
94
- end
95
-
96
- ### JOB CONTROL ###
97
-
98
- def create_job(api_token, job_name=nil, language="en")
99
- query_hash = init_access_req_dict(api_token)
100
- query_hash[:job_name] = job_name if !(job_name.nil?)
101
- query_hash[:language] = language
102
-
103
- json = WebUtils.get_json(@base_url + CREATE_JOB_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
104
- # Return a hash with JobId and TaskId
105
- return Mash.new(json)
106
- end
107
-
108
- def authorize_job(api_token, job_id)
109
- query_hash = init_job_req_dict(api_token, job_id)
110
- # Nothing returned
111
- WebUtils.http_request(@base_url + AUTHORIZE_JOB_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
112
- end
113
-
114
- def delete_job(api_token, job_id)
115
- query_hash = init_job_req_dict(api_token, job_id)
116
-
117
- json = WebUtils.get_json(@base_url + DELETE_JOB_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
118
- return json["TaskId"]
119
- end
120
-
121
- def get_job_info(api_token, job_id)
122
- query_hash = init_job_req_dict(api_token, job_id)
123
-
124
- json = WebUtils.get_json(@base_url + GET_JOB_INFO_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
125
- return Mash.new(json)
126
- end
127
-
128
- def get_job_list(api_token)
129
- query_hash = init_access_req_dict(api_token)
130
-
131
- json = WebUtils.get_json(@base_url + GET_JOB_LIST_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
132
- return Mash.new(json)
133
- end
134
-
135
- def add_media_to_job_file(api_token, job_id, media_file)
136
- assert_argument(media_file, "Media File")
137
- query_hash = init_job_req_dict(api_token, job_id)
138
- file_size = File.size(media_file.path)
139
-
140
- json = WebUtils.get_json(@base_url + ADD_MEDIA_TO_JOB_PATH, 'POST', nil, query_hash, {'Content-Type' => 'video/mp4', 'Content-Length' => file_size}, media_file)
141
- return json["TaskId"]
142
- end
143
-
144
- def add_media_to_job_url(api_token, job_id, media_url)
145
- return send_media_url(api_token, job_id, media_url, ADD_MEDIA_TO_JOB_PATH)
146
- end
147
-
148
- def add_media_to_job_embedded(api_token, job_id, media_url)
149
- return send_media_url(api_token, job_id, media_url, ADD_EMBEDDED_MEDIA_TO_JOB_PATH)
150
- end
151
-
152
- def send_media_url(api_token, job_id, media_url, path)
153
- assert_argument(media_url, "Media URL")
154
- query_hash = init_job_req_dict(api_token, job_id)
155
- query_hash[:media_url] = URI.escape(media_url)
156
-
157
- json = WebUtils.get_json(@base_url + path, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
158
- return json["TaskId"]
159
- end
160
- private :send_media_url
161
-
162
- def get_media(api_token, job_id)
163
- query_hash = init_job_req_dict(api_token, job_id)
164
- json = WebUtils.get_json(@base_url + GET_MEDIA_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
165
- return json["MediaUrl"]
166
- end
167
-
168
- def perform_transcription(api_token,
169
- job_id,
170
- fidelity,
171
- priority,
172
- callback_uri=nil,
173
- turnaround_hours=nil,
174
- target_language=nil,
175
- options = nil)
176
- assert_argument(fidelity, "Fidelity")
177
- assert_argument(priority, "Priority")
178
- query_hash = init_job_req_dict(api_token, job_id)
179
- query_hash[:transcription_fidelity] = fidelity
180
- query_hash[:priority] = priority
181
- query_hash[:callback_uri] = URI.escape(callback_uri) if !(callback_uri.nil?)
182
- query_hash[:turnaround_hours] = turnaround_hours if !(turnaround_hours.nil?)
183
- query_hash[:target_language] = target_language if !(target_language.nil?)
184
- query_hash.merge!(options.get_hash) if !(options.nil?)
185
-
186
- json = WebUtils.get_json(@base_url + PERFORM_TRANSCRIPTION, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
187
- return json["TaskId"]
188
- end
189
-
190
- def get_transcript(api_token, job_id, transcript_options=nil)
191
- query_hash = init_job_req_dict(api_token, job_id)
192
- query_hash.merge!(transcript_options.get_hash) if !(transcript_options.nil?)
193
- # Return raw transcript text
194
- return WebUtils.http_request(@base_url + GET_TRANSCRIPTION_PATH, 'GET', WebUtils::DOWNLOAD_TIMEOUT, query_hash)
195
- end
196
-
197
- def get_caption(api_token, job_id, caption_format, caption_options=nil)
198
- query_hash = init_job_req_dict(api_token, job_id)
199
- query_hash[:caption_format] = caption_format
200
- query_hash.merge!(caption_options.get_hash) if !(caption_options.nil?)
201
-
202
- response = WebUtils.http_request(@base_url + GET_CAPTION_PATH, 'GET', WebUtils::DOWNLOAD_TIMEOUT, query_hash)
203
- if(!caption_options.nil? and caption_options.build_url) # If build_url is true
204
- return JSON.parse(response)["CaptionUrl"]
205
- else
206
- return response # Else return raw caption text
207
- end
208
- end
209
-
210
- def get_element_list(api_token, job_id, elementlist_version=nil)
211
- query_hash = init_job_req_dict(api_token, job_id)
212
- query_hash[:elementlist_version] = elementlist_version if !(elementlist_version.nil?)
213
- json = WebUtils.get_json(@base_url + GET_ELEMENT_LIST_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
214
- return Mash.new(json)
215
- end
216
-
217
- def get_list_of_element_lists(api_token, job_id)
218
- query_hash = init_job_req_dict(api_token, job_id)
219
- json = WebUtils.get_json(@base_url + GET_LIST_OF_ELEMENT_LISTS_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
220
- return json
221
- end
222
-
223
- ### PRIVATE HELPER METHODS ###
224
- private
225
-
226
- def init_job_req_dict(api_token, job_id)
227
- assert_argument(job_id, "Job ID")
228
- init_access_req_dict(api_token).merge({job_id: job_id})
229
- end
230
-
231
- def init_access_req_dict(api_token)
232
- assert_argument(api_token, "API Token")
233
- init_version_dict().merge({api_token: api_token})
234
- end
235
-
236
- def init_version_dict()
237
- {v: API_VERSION}
238
- end
239
-
240
- def assert_argument(arg, arg_name)
241
- if (arg.nil?)
242
- raise ArgumentError.new("Invalid argument - " + arg_name)
243
- end
244
- end
245
- end
1
+ module Cielo24
2
+ class Actions
3
+
4
+ require 'uri'
5
+ require 'hashie'
6
+ require_relative 'web_utils'
7
+ require_relative 'enums'
8
+ require 'json'
9
+ include Errno
10
+ include Hashie
11
+ include Cielo24
12
+
13
+ attr_accessor :base_url
14
+
15
+ API_VERSION = 1
16
+ LOGIN_PATH = '/api/account/login'
17
+ LOGOUT_PATH = '/api/account/logout'
18
+ UPDATE_PASSWORD_PATH = '/api/account/update_password'
19
+ GENERATE_API_KEY_PATH = '/api/account/generate_api_key'
20
+ REMOVE_API_KEY_PATH = '/api/account/remove_api_key'
21
+ CREATE_JOB_PATH = '/api/job/new'
22
+ AUTHORIZE_JOB_PATH = '/api/job/authorize'
23
+ DELETE_JOB_PATH = '/api/job/del'
24
+ GET_JOB_INFO_PATH = '/api/job/info'
25
+ GET_JOB_LIST_PATH = '/api/job/list'
26
+ ADD_MEDIA_TO_JOB_PATH = '/api/job/add_media'
27
+ ADD_EMBEDDED_MEDIA_TO_JOB_PATH = '/api/job/add_media_url'
28
+ GET_MEDIA_PATH = '/api/job/media'
29
+ PERFORM_TRANSCRIPTION = '/api/job/perform_transcription'
30
+ GET_TRANSCRIPT_PATH = '/api/job/get_transcript'
31
+ GET_CAPTION_PATH = '/api/job/get_caption'
32
+ GET_ELEMENT_LIST_PATH = '/api/job/get_elementlist'
33
+ GET_LIST_OF_ELEMENT_LISTS_PATH = '/api/job/list_elementlists'
34
+
35
+ def initialize(base_url='https://api.cielo24.com')
36
+ @base_url = base_url
37
+ end
38
+
39
+ ######################
40
+ ### ACCESS CONTROL ###
41
+ ######################
42
+
43
+ def login(username, password=nil, api_securekey=nil, use_headers=false)
44
+ assert_argument(username, 'Username')
45
+ raise ArgumentError.new('Password or API Secure Key must be supplied for login.') if (password.nil? and api_securekey.nil?)
46
+
47
+ query_hash = init_version_dict
48
+ headers = Hash.new
49
+
50
+ if use_headers
51
+ headers[:'x-auth-user'] = username
52
+ headers[:'x-auth-password'] = password unless password.nil?
53
+ headers[:'x-auth-securekey'] = api_securekey unless api_securekey.nil?
54
+ else
55
+ query_hash[:username] = username
56
+ query_hash[:password] = password unless password.nil?
57
+ query_hash[:securekey] = api_securekey unless api_securekey.nil?
58
+ end
59
+
60
+ response = WebUtils.get_json(@base_url + LOGIN_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash, headers)
61
+ return response['ApiToken']
62
+ end
63
+
64
+ def logout(api_token)
65
+ query_hash = init_access_req_dict(api_token)
66
+ # Nothing returned
67
+ WebUtils.http_request(@base_url + LOGOUT_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
68
+ end
69
+
70
+ def update_password(api_token, new_password, sub_account=nil)
71
+ assert_argument(new_password, 'New Password')
72
+ query_hash = init_access_req_dict(api_token)
73
+ query_hash[:new_password] = new_password
74
+ # username parameter named sub_account for clarity
75
+ query_hash[:username] = sub_account unless sub_account.nil?
76
+ # Nothing returned
77
+ WebUtils.http_request(@base_url + UPDATE_PASSWORD_PATH, 'POST', WebUtils::BASIC_TIMEOUT, nil, nil, query_hash)
78
+ end
79
+
80
+ def generate_api_key(api_token, username, force_new=false)
81
+ assert_argument(username, 'Username')
82
+ query_hash = init_access_req_dict(api_token)
83
+ query_hash[:account_id] = username
84
+ query_hash[:force_new] = force_new
85
+ response = WebUtils.get_json(@base_url + GENERATE_API_KEY_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
86
+ return response['ApiKey']
87
+ end
88
+
89
+ def remove_api_key(api_token, api_securekey)
90
+ assert_argument(api_securekey, 'API Secure Key')
91
+ query_hash = init_access_req_dict(api_token)
92
+ query_hash[:api_securekey] = api_securekey
93
+ # Nothing returned
94
+ WebUtils.http_request(@base_url + REMOVE_API_KEY_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
95
+ end
96
+
97
+ ###################
98
+ ### JOB CONTROL ###
99
+ ###################
100
+
101
+ def create_job(api_token, job_name=nil, language=Language::ENGLISH, external_id=nil, sub_account=nil)
102
+ query_hash = init_access_req_dict(api_token)
103
+ query_hash[:job_name] = job_name unless job_name.nil?
104
+ query_hash[:language] = language unless language.nil?
105
+ query_hash[:external_id] = external_id unless external_id.nil?
106
+ # username parameter named sub_account for clarity
107
+ query_hash[:username] = sub_account unless sub_account.nil?
108
+
109
+ response = WebUtils.get_json(@base_url + CREATE_JOB_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
110
+ # Return a hash with JobId and TaskId
111
+ return Mash.new(response)
112
+ end
113
+
114
+ def authorize_job(api_token, job_id)
115
+ query_hash = init_job_req_dict(api_token, job_id)
116
+ # Nothing returned
117
+ WebUtils.http_request(@base_url + AUTHORIZE_JOB_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
118
+ end
119
+
120
+ def delete_job(api_token, job_id)
121
+ query_hash = init_job_req_dict(api_token, job_id)
122
+ response = WebUtils.get_json(@base_url + DELETE_JOB_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
123
+ return response['TaskId']
124
+ end
125
+
126
+ def get_job_info(api_token, job_id)
127
+ query_hash = init_job_req_dict(api_token, job_id)
128
+ response = WebUtils.get_json(@base_url + GET_JOB_INFO_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
129
+ return Mash.new(response)
130
+ end
131
+
132
+ def get_job_list(api_token, options=nil)
133
+ query_hash = init_access_req_dict(api_token)
134
+ query_hash.merge!(options.get_hash) unless options.nil?
135
+ response = WebUtils.get_json(@base_url + GET_JOB_LIST_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
136
+ return Mash.new(response)
137
+ end
138
+
139
+ def add_media_to_job_file(api_token, job_id, media_file)
140
+ assert_argument(media_file, 'Media File')
141
+ query_hash = init_job_req_dict(api_token, job_id)
142
+ file_size = File.size(media_file.path)
143
+ response = WebUtils.get_json(@base_url + ADD_MEDIA_TO_JOB_PATH, 'POST', nil, query_hash,
144
+ {'Content-Type' => 'video/mp4', 'Content-Length' => file_size}, media_file)
145
+ return response['TaskId']
146
+ end
147
+
148
+ def add_media_to_job_url(api_token, job_id, media_url)
149
+ return send_media_url(api_token, job_id, media_url, ADD_MEDIA_TO_JOB_PATH)
150
+ end
151
+
152
+ def add_media_to_job_embedded(api_token, job_id, media_url)
153
+ return send_media_url(api_token, job_id, media_url, ADD_EMBEDDED_MEDIA_TO_JOB_PATH)
154
+ end
155
+
156
+ def get_media(api_token, job_id)
157
+ query_hash = init_job_req_dict(api_token, job_id)
158
+ response = WebUtils.get_json(@base_url + GET_MEDIA_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
159
+ return response['MediaUrl']
160
+ end
161
+
162
+ def perform_transcription(api_token,
163
+ job_id,
164
+ fidelity,
165
+ priority=nil,
166
+ callback_uri=nil,
167
+ turnaround_hours=nil,
168
+ target_language=nil,
169
+ options=nil)
170
+ assert_argument(fidelity, 'Fidelity')
171
+ query_hash = init_job_req_dict(api_token, job_id)
172
+ query_hash[:transcription_fidelity] = fidelity
173
+ query_hash[:priority] = priority unless priority.nil?
174
+ query_hash[:callback_url] = callback_uri unless callback_uri.nil?
175
+ query_hash[:turnaround_hours] = turnaround_hours unless turnaround_hours.nil?
176
+ query_hash[:target_language] = target_language unless target_language.nil?
177
+ query_hash[:options] = options.get_hash.to_json unless options.nil?
178
+
179
+ response = WebUtils.get_json(@base_url + PERFORM_TRANSCRIPTION, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
180
+ return response['TaskId']
181
+ end
182
+
183
+ def get_transcript(api_token, job_id, transcript_options=nil)
184
+ query_hash = init_job_req_dict(api_token, job_id)
185
+ query_hash.merge!(transcript_options.get_hash) unless transcript_options.nil?
186
+ # Returns raw transcript text
187
+ return WebUtils.http_request(@base_url + GET_TRANSCRIPT_PATH, 'GET', WebUtils::DOWNLOAD_TIMEOUT, query_hash)
188
+ end
189
+
190
+ def get_caption(api_token, job_id, caption_format, caption_options=nil)
191
+ assert_argument(caption_format, 'Caption Format')
192
+ query_hash = init_job_req_dict(api_token, job_id)
193
+ query_hash[:caption_format] = caption_format
194
+ query_hash.merge!(caption_options.get_hash) unless caption_options.nil?
195
+
196
+ response = WebUtils.http_request(@base_url + GET_CAPTION_PATH, 'GET', WebUtils::DOWNLOAD_TIMEOUT, query_hash)
197
+ if not caption_options.nil? and caption_options.build_url # If build_url is true
198
+ return JSON.parse(response)['CaptionUrl']
199
+ else
200
+ return response # Else return raw caption text
201
+ end
202
+ end
203
+
204
+ def get_element_list(api_token, job_id, elementlist_version=nil)
205
+ query_hash = init_job_req_dict(api_token, job_id)
206
+ query_hash[:elementlist_version] = elementlist_version unless elementlist_version.nil?
207
+ response = WebUtils.get_json(@base_url + GET_ELEMENT_LIST_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
208
+ return Mash.new(response)
209
+ end
210
+
211
+ def get_list_of_element_lists(api_token, job_id)
212
+ query_hash = init_job_req_dict(api_token, job_id)
213
+ response = WebUtils.get_json(@base_url + GET_LIST_OF_ELEMENT_LISTS_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
214
+ return response
215
+ end
216
+
217
+ ##############################
218
+ ### PRIVATE HELPER METHODS ###
219
+ ##############################
220
+ private
221
+
222
+ def send_media_url(api_token, job_id, media_url, path)
223
+ assert_argument(media_url, 'Media URL')
224
+ query_hash = init_job_req_dict(api_token, job_id)
225
+ query_hash[:media_url] = media_url
226
+ response = WebUtils.get_json(@base_url + path, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
227
+ return response['TaskId']
228
+ end
229
+
230
+ def init_job_req_dict(api_token, job_id)
231
+ assert_argument(job_id, 'Job ID')
232
+ init_access_req_dict(api_token).merge({job_id: job_id})
233
+ end
234
+
235
+ def init_access_req_dict(api_token)
236
+ assert_argument(api_token, 'API Token')
237
+ init_version_dict.merge({api_token: api_token})
238
+ end
239
+
240
+ def init_version_dict()
241
+ {v: API_VERSION}
242
+ end
243
+
244
+ def assert_argument(arg, arg_name)
245
+ if arg.nil?
246
+ raise ArgumentError.new('Invalid argument - ' + arg_name)
247
+ end
248
+ end
249
+ end
246
250
  end