cielo24 0.0.16 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e334329bde7311af931b6b69fd8bb2fbf3f9642c
4
- data.tar.gz: e321a51097a5d236d07ebb6f134203a495aeddef
3
+ metadata.gz: ed8f9b9f2fbd20edca9c999d8d3be9504304547d
4
+ data.tar.gz: 2d6127f5367b2778438c016c613de13d1ac6a8cf
5
5
  SHA512:
6
- metadata.gz: 3b653d28dc45ee1c37eeac09c6b3559c2cfaceda39864eadbde955b6ac73e821c7e4972e0071b61a8a1bf73060c2ef7abadde8e42e502f1788ec4b0aca8680e4
7
- data.tar.gz: 58f5b34fe7b3f74015dc2bededd03c6b59bfa87e069256bfaf272b871b50f9548609cc158b81c1f075988fb7f9176d92243f0fe5ff4b4d56e3fa2935e437a9e0
6
+ metadata.gz: cbce8d5f218e768aa47f2aa4a21f10baef1870cf09153f68175db86ed37b8afbab9f177c8c8234d0605eeec8e17efce24c34464e17dfa39a8dc2dc0ffc4a643e
7
+ data.tar.gz: 00c9011893639c32fe7c90717e6f9beab87422ab9b086c37aa37a596c3ac47fba129753992f00b54e0457206f5d6549316f2741cc70ad2eb4b250b1d79224764
@@ -1,10 +1,10 @@
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
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
10
  end
@@ -1,275 +1,288 @@
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
- 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
- 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
- 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
- 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
- fix_job_info_offsets 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
- fix_job_list_offsets 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
- response['TaskId']
146
- end
147
-
148
- def add_media_to_job_url(api_token, job_id, media_url)
149
- 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
- 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
- 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
- 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
- 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
- JSON.parse(response)['CaptionUrl']
199
- else
200
- 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
- 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
- WebUtils.get_json(@base_url + GET_LIST_OF_ELEMENT_LISTS_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
214
- end
215
-
216
- ##############################
217
- ### PRIVATE HELPER METHODS ###
218
- ##############################
219
- private
220
-
221
- def send_media_url(api_token, job_id, media_url, path)
222
- assert_argument(media_url, 'Media URL')
223
- query_hash = init_job_req_dict(api_token, job_id)
224
- query_hash[:media_url] = media_url
225
- response = WebUtils.get_json(@base_url + path, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
226
- response['TaskId']
227
- end
228
-
229
- def init_job_req_dict(api_token, job_id)
230
- assert_argument(job_id, 'Job ID')
231
- init_access_req_dict(api_token).merge({job_id: job_id})
232
- end
233
-
234
- def init_access_req_dict(api_token)
235
- assert_argument(api_token, 'API Token')
236
- init_version_dict.merge({api_token: api_token})
237
- end
238
-
239
- def init_version_dict
240
- {v: API_VERSION}
241
- end
242
-
243
- def assert_argument(arg, arg_name)
244
- if arg.nil?
245
- raise ArgumentError.new('Invalid argument - ' + arg_name)
246
- end
247
- end
248
-
249
- def fix_job_info_offsets(job_info)
250
- top_keys = %w(CreationDate StartDate DueDate CompletedDate ReturnDate AuthorizationDate)
251
- task_keys = %w(TaskRequestTime)
252
- job_info = fix_offsets(job_info, top_keys)
253
- job_info.Tasks = job_info.Tasks.map do |task_item|
254
- fix_offsets(task_item, task_keys)
255
- end
256
- job_info
257
- end
258
-
259
- def fix_job_list_offsets(job_list)
260
- keys = %w(CreationDate CreationTime StartDate StartTime DueDate CompletedDate
261
- ReturnDate CompletedTime AuthorizationDate)
262
- job_list.ActiveJobs = job_list.ActiveJobs.map do |job_item|
263
- fix_offsets(job_item, keys)
264
- end
265
- job_list
266
- end
267
-
268
- def fix_offsets(hash, keys)
269
- keys.each do |key|
270
- hash[key] = WebUtils.to_utc(hash[key])
271
- end
272
- hash
273
- end
274
- 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
+ AGGREGATE_STATISTICS_PATH = '/api/job/aggregate_statistics'
35
+
36
+ def initialize(base_url='https://api.cielo24.com')
37
+ @base_url = base_url
38
+ end
39
+
40
+ ######################
41
+ ### ACCESS CONTROL ###
42
+ ######################
43
+
44
+ def login(username, password=nil, api_securekey=nil, use_headers=false)
45
+ assert_argument(username, 'Username')
46
+ raise ArgumentError.new('Password or API Secure Key must be supplied for login.') if (password.nil? and api_securekey.nil?)
47
+
48
+ query_hash = init_version_dict
49
+ headers = Hash.new
50
+
51
+ if use_headers
52
+ headers[:'x-auth-user'] = username
53
+ headers[:'x-auth-password'] = password unless password.nil?
54
+ headers[:'x-auth-securekey'] = api_securekey unless api_securekey.nil?
55
+ else
56
+ query_hash[:username] = username
57
+ query_hash[:password] = password unless password.nil?
58
+ query_hash[:securekey] = api_securekey unless api_securekey.nil?
59
+ end
60
+
61
+ response = WebUtils.get_json(@base_url + LOGIN_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash, headers)
62
+ response['ApiToken']
63
+ end
64
+
65
+ def logout(api_token)
66
+ query_hash = init_access_req_dict(api_token)
67
+ # Nothing returned
68
+ WebUtils.http_request(@base_url + LOGOUT_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
69
+ end
70
+
71
+ def update_password(api_token, new_password, sub_account=nil)
72
+ assert_argument(new_password, 'New Password')
73
+ query_hash = init_access_req_dict(api_token)
74
+ query_hash[:new_password] = new_password
75
+ # username parameter named sub_account for clarity
76
+ query_hash[:username] = sub_account unless sub_account.nil?
77
+ # Nothing returned
78
+ WebUtils.http_request(@base_url + UPDATE_PASSWORD_PATH, 'POST', WebUtils::BASIC_TIMEOUT, nil, nil, query_hash)
79
+ end
80
+
81
+ def generate_api_key(api_token, sub_account=nil, force_new=false)
82
+ query_hash = init_access_req_dict(api_token)
83
+ # account_id parameter named sub_account for clarity
84
+ query_hash[:account_id] = sub_account unless sub_account.nil?
85
+ query_hash[:force_new] = force_new
86
+ response = WebUtils.get_json(@base_url + GENERATE_API_KEY_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
87
+ response['ApiKey']
88
+ end
89
+
90
+ def remove_api_key(api_token, api_securekey)
91
+ assert_argument(api_securekey, 'API Secure Key')
92
+ query_hash = init_access_req_dict(api_token)
93
+ query_hash[:api_securekey] = api_securekey
94
+ # Nothing returned
95
+ WebUtils.http_request(@base_url + REMOVE_API_KEY_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
96
+ end
97
+
98
+ ###################
99
+ ### JOB CONTROL ###
100
+ ###################
101
+
102
+ def create_job(api_token, job_name=nil, language=Language::ENGLISH, external_id=nil, sub_account=nil)
103
+ query_hash = init_access_req_dict(api_token)
104
+ query_hash[:job_name] = job_name unless job_name.nil?
105
+ query_hash[:language] = language unless language.nil?
106
+ query_hash[:external_id] = external_id unless external_id.nil?
107
+ # username parameter named sub_account for clarity
108
+ query_hash[:username] = sub_account unless sub_account.nil?
109
+
110
+ response = WebUtils.get_json(@base_url + CREATE_JOB_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
111
+ # Return a hash with JobId and TaskId
112
+ Mash.new(response)
113
+ end
114
+
115
+ def authorize_job(api_token, job_id)
116
+ query_hash = init_job_req_dict(api_token, job_id)
117
+ # Nothing returned
118
+ WebUtils.http_request(@base_url + AUTHORIZE_JOB_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
119
+ end
120
+
121
+ def delete_job(api_token, job_id)
122
+ query_hash = init_job_req_dict(api_token, job_id)
123
+ response = WebUtils.get_json(@base_url + DELETE_JOB_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
124
+ response['TaskId']
125
+ end
126
+
127
+ def get_job_info(api_token, job_id)
128
+ query_hash = init_job_req_dict(api_token, job_id)
129
+ response = WebUtils.get_json(@base_url + GET_JOB_INFO_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
130
+ fix_job_info_offsets Mash.new(response)
131
+ end
132
+
133
+ def get_job_list(api_token, options=nil)
134
+ query_hash = init_access_req_dict(api_token)
135
+ query_hash.merge!(options.get_hash) unless options.nil?
136
+ response = WebUtils.get_json(@base_url + GET_JOB_LIST_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
137
+ fix_job_list_offsets Mash.new(response)
138
+ end
139
+
140
+ def add_media_to_job_file(api_token, job_id, media_file)
141
+ assert_argument(media_file, 'Media File')
142
+ query_hash = init_job_req_dict(api_token, job_id)
143
+ file_size = File.size(media_file.path)
144
+ response = WebUtils.get_json(@base_url + ADD_MEDIA_TO_JOB_PATH, 'POST', nil, query_hash,
145
+ {'Content-Type' => 'video/mp4', 'Content-Length' => file_size}, media_file)
146
+ response['TaskId']
147
+ end
148
+
149
+ def add_media_to_job_url(api_token, job_id, media_url)
150
+ send_media_url(api_token, job_id, media_url, ADD_MEDIA_TO_JOB_PATH)
151
+ end
152
+
153
+ def add_media_to_job_embedded(api_token, job_id, media_url)
154
+ send_media_url(api_token, job_id, media_url, ADD_EMBEDDED_MEDIA_TO_JOB_PATH)
155
+ end
156
+
157
+ def get_media(api_token, job_id)
158
+ query_hash = init_job_req_dict(api_token, job_id)
159
+ response = WebUtils.get_json(@base_url + GET_MEDIA_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
160
+ response['MediaUrl']
161
+ end
162
+
163
+ def perform_transcription(api_token,
164
+ job_id,
165
+ fidelity,
166
+ priority=nil,
167
+ callback_uri=nil,
168
+ turnaround_hours=nil,
169
+ target_language=nil,
170
+ options=nil)
171
+ assert_argument(fidelity, 'Fidelity')
172
+ query_hash = init_job_req_dict(api_token, job_id)
173
+ query_hash[:transcription_fidelity] = fidelity
174
+ query_hash[:priority] = priority unless priority.nil?
175
+ query_hash[:callback_url] = callback_uri unless callback_uri.nil?
176
+ query_hash[:turnaround_hours] = turnaround_hours unless turnaround_hours.nil?
177
+ query_hash[:target_language] = target_language unless target_language.nil?
178
+ query_hash[:options] = options.get_hash.to_json unless options.nil?
179
+
180
+ response = WebUtils.get_json(@base_url + PERFORM_TRANSCRIPTION, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
181
+ response['TaskId']
182
+ end
183
+
184
+ def get_transcript(api_token, job_id, transcript_options=nil)
185
+ query_hash = init_job_req_dict(api_token, job_id)
186
+ query_hash.merge!(transcript_options.get_hash) unless transcript_options.nil?
187
+ # Returns raw transcript text
188
+ WebUtils.http_request(@base_url + GET_TRANSCRIPT_PATH, 'GET', WebUtils::DOWNLOAD_TIMEOUT, query_hash)
189
+ end
190
+
191
+ def get_caption(api_token, job_id, caption_format, caption_options=nil)
192
+ assert_argument(caption_format, 'Caption Format')
193
+ query_hash = init_job_req_dict(api_token, job_id)
194
+ query_hash[:caption_format] = caption_format
195
+ query_hash.merge!(caption_options.get_hash) unless caption_options.nil?
196
+
197
+ response = WebUtils.http_request(@base_url + GET_CAPTION_PATH, 'GET', WebUtils::DOWNLOAD_TIMEOUT, query_hash)
198
+ if not caption_options.nil? and caption_options.build_url # If build_url is true
199
+ JSON.parse(response)['CaptionUrl']
200
+ else
201
+ response # Else return raw caption text
202
+ end
203
+ end
204
+
205
+ def get_element_list(api_token, job_id, elementlist_version=nil)
206
+ query_hash = init_job_req_dict(api_token, job_id)
207
+ query_hash[:elementlist_version] = elementlist_version unless elementlist_version.nil?
208
+ response = WebUtils.get_json(@base_url + GET_ELEMENT_LIST_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
209
+ Mash.new(response)
210
+ end
211
+
212
+ def get_list_of_element_lists(api_token, job_id)
213
+ query_hash = init_job_req_dict(api_token, job_id)
214
+ WebUtils.get_json(@base_url + GET_LIST_OF_ELEMENT_LISTS_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
215
+ end
216
+
217
+ def aggregate_statistics(api_token, metrics=nil, group_by=nil, start_date=nil, end_date=nil, sub_account=nil)
218
+ query_hash = init_access_req_dict(api_token)
219
+ query_hash[:metrics] = metrics.to_json unless metrics.nil?
220
+ query_hash[:group_by] = group_by unless group_by.nil?
221
+ query_hash[:start_date] = start_date unless start_date.nil?
222
+ query_hash[:end_date] = end_date unless end_date.nil?
223
+ # account_id parameter named sub_account for clarity
224
+ query_hash[:account_id] = sub_account unless sub_account.nil?
225
+ response = WebUtils.get_json(@base_url + AGGREGATE_STATISTICS_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
226
+ Mash.new(response)
227
+ end
228
+
229
+ ##############################
230
+ ### PRIVATE HELPER METHODS ###
231
+ ##############################
232
+ private
233
+
234
+ def send_media_url(api_token, job_id, media_url, path)
235
+ assert_argument(media_url, 'Media URL')
236
+ query_hash = init_job_req_dict(api_token, job_id)
237
+ query_hash[:media_url] = media_url
238
+ response = WebUtils.get_json(@base_url + path, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
239
+ response['TaskId']
240
+ end
241
+
242
+ def init_job_req_dict(api_token, job_id)
243
+ assert_argument(job_id, 'Job ID')
244
+ init_access_req_dict(api_token).merge({job_id: job_id})
245
+ end
246
+
247
+ def init_access_req_dict(api_token)
248
+ assert_argument(api_token, 'API Token')
249
+ init_version_dict.merge({api_token: api_token})
250
+ end
251
+
252
+ def init_version_dict
253
+ {v: API_VERSION}
254
+ end
255
+
256
+ def assert_argument(arg, arg_name)
257
+ if arg.nil?
258
+ raise ArgumentError.new('Invalid argument - ' + arg_name)
259
+ end
260
+ end
261
+
262
+ def fix_job_info_offsets(job_info)
263
+ top_keys = %w(CreationDate StartDate DueDate CompletedDate ReturnDate AuthorizationDate)
264
+ task_keys = %w(TaskRequestTime)
265
+ job_info = fix_offsets(job_info, top_keys)
266
+ job_info.Tasks = job_info.Tasks.map do |task_item|
267
+ fix_offsets(task_item, task_keys)
268
+ end
269
+ job_info
270
+ end
271
+
272
+ def fix_job_list_offsets(job_list)
273
+ keys = %w(CreationDate CreationTime StartDate StartTime DueDate CompletedDate
274
+ ReturnDate CompletedTime AuthorizationDate)
275
+ job_list.ActiveJobs = job_list.ActiveJobs.map do |job_item|
276
+ fix_offsets(job_item, keys)
277
+ end
278
+ job_list
279
+ end
280
+
281
+ def fix_offsets(hash, keys)
282
+ keys.each do |key|
283
+ hash[key] = WebUtils.to_utc(hash[key])
284
+ end
285
+ hash
286
+ end
287
+ end
275
288
  end