rev-api 2.2.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +22 -21
  3. data/.ruby-gemset +1 -1
  4. data/.ruby-version +1 -1
  5. data/.travis.yml +8 -8
  6. data/Gemfile +3 -3
  7. data/LICENSE +191 -191
  8. data/README.md +131 -132
  9. data/Rakefile +13 -13
  10. data/examples/cli.rb +270 -270
  11. data/lib/rev-api.rb +26 -26
  12. data/lib/rev-api/api.rb +326 -326
  13. data/lib/rev-api/api_serializable.rb +30 -30
  14. data/lib/rev-api/exceptions.rb +97 -100
  15. data/lib/rev-api/http_client.rb +97 -97
  16. data/lib/rev-api/models/order.rb +129 -138
  17. data/lib/rev-api/models/order_request.rb +273 -222
  18. data/lib/rev-api/version.rb +3 -3
  19. data/rev-api.gemspec +33 -34
  20. data/spec/fixtures/api_cassettes/cancel_order.yml +38 -38
  21. data/spec/fixtures/api_cassettes/cancel_order_not_allowed.yml +40 -40
  22. data/spec/fixtures/api_cassettes/get_attachment_content.yml +399 -399
  23. data/spec/fixtures/api_cassettes/get_attachment_content_as_pdf.yml +399 -399
  24. data/spec/fixtures/api_cassettes/get_attachment_content_as_text.yml +65 -65
  25. data/spec/fixtures/api_cassettes/get_attachment_content_as_youtube_transcript.yml +66 -66
  26. data/spec/fixtures/api_cassettes/get_attachment_content_unacceptable_representation.yml +42 -42
  27. data/spec/fixtures/api_cassettes/get_attachment_content_with_invalid_id.yml +42 -42
  28. data/spec/fixtures/api_cassettes/get_attachment_metadata.yml +42 -42
  29. data/spec/fixtures/api_cassettes/get_attachment_with_invalid_id.yml +40 -40
  30. data/spec/fixtures/api_cassettes/get_orders.yml +122 -122
  31. data/spec/fixtures/api_cassettes/get_orders_with_clientRef.yml +41 -41
  32. data/spec/fixtures/api_cassettes/get_tc_order.yml +44 -44
  33. data/spec/fixtures/api_cassettes/get_third_page_of_orders.yml +52 -58
  34. data/spec/fixtures/api_cassettes/link_input.yml +44 -44
  35. data/spec/fixtures/api_cassettes/link_input_with_all_attributes.yml +44 -44
  36. data/spec/fixtures/api_cassettes/link_input_with_spaces_in_filename.yml +45 -45
  37. data/spec/fixtures/api_cassettes/not_found_order.yml +42 -42
  38. data/spec/fixtures/api_cassettes/submit_cp_order.yml +44 -45
  39. data/spec/fixtures/api_cassettes/submit_su_order.yml +44 -45
  40. data/spec/fixtures/api_cassettes/submit_tc_order_with_account_balance.yml +44 -45
  41. data/spec/fixtures/api_cassettes/submit_tc_order_with_invalid_request.yml +45 -45
  42. data/spec/fixtures/api_cassettes/submit_tc_order_without_specifying_payment.yml +44 -45
  43. data/spec/fixtures/api_cassettes/unauthorized.yml +42 -42
  44. data/spec/fixtures/api_cassettes/upload_input.yml +90 -90
  45. data/spec/fixtures/api_cassettes/upload_input_with_invalid_content_type.yml +91 -91
  46. data/spec/lib/rev/api_spec.rb +30 -24
  47. data/spec/lib/rev/cancel_order_spec.rb +24 -24
  48. data/spec/lib/rev/exceptions_spec.rb +8 -8
  49. data/spec/lib/rev/get_attachment_content_spec.rb +79 -79
  50. data/spec/lib/rev/get_attachment_metadata_spec.rb +33 -33
  51. data/spec/lib/rev/get_order_spec.rb +52 -68
  52. data/spec/lib/rev/get_orders_spec.rb +62 -62
  53. data/spec/lib/rev/http_client_spec.rb +32 -32
  54. data/spec/lib/rev/models/order_request_spec.rb +79 -10
  55. data/spec/lib/rev/models/order_spec.rb +58 -58
  56. data/spec/lib/rev/post_inputs_spec.rb +94 -94
  57. data/spec/lib/rev/post_order_spec.rb +163 -195
  58. data/spec/spec_helper.rb +47 -49
  59. data/spec/test_helpers.rb +5 -0
  60. metadata +10 -28
  61. data/.coveralls.yml +0 -2
  62. data/spec/fixtures/api_cassettes/get_tr_order.yml +0 -44
  63. data/spec/fixtures/api_cassettes/submit_tr_order.yml +0 -44
data/README.md CHANGED
@@ -1,132 +1,131 @@
1
- [![Gem Version](https://badge.fury.io/rb/rev-api.png)](http://badge.fury.io/rb/rev-api)
2
- [![Build Status](https://secure.travis-ci.org/revdotcom/rev-ruby-sdk.png?branch=master)](https://secure.travis-ci.org/revdotcom/rev-ruby-sdk)
3
- [![Dependency Status](https://gemnasium.com/revdotcom/rev-ruby-sdk.png?travis)](https://gemnasium.com/revdotcom/rev-ruby-sdk)
4
- [![Code Climate](https://codeclimate.com/github/revdotcom/rev-ruby-sdk.png)](https://codeclimate.com/github/revdotcom/rev-ruby-sdk)
5
- [![Coverage Status](https://coveralls.io/repos/revdotcom/rev-ruby-sdk/badge.png)](https://coveralls.io/r/revdotcom/rev-ruby-sdk)
6
-
7
- [Reference](https://www.rev.com/api/docs) | [RDocs](http://rubydoc.info/github/revdotcom/rev-ruby-sdk/master/frames)
8
-
9
- rev-ruby-sdk
10
- ------------
11
-
12
- ### Background
13
-
14
- Rev.com provides transcription and translation services powered by humans. The Rev.com API allows you to tap these services
15
- with no manual steps. It's a RESTful API, documented at <https://www.rev.com/api/docs>.
16
-
17
- The Ruby SDK for the Rev API provides a convenient Ruby wrapper for the Rev.com API. All operations of the API, as described
18
- at <https://www.rev.com/api/operations>, are supported.
19
-
20
- ### Install
21
-
22
- ```shell
23
- gem install rev-api
24
- ```
25
-
26
- or put it into your Gemfile.
27
-
28
- When you need it:
29
-
30
- ```ruby
31
- require 'rev-api'
32
- ```
33
-
34
- ### Authentication
35
-
36
- If you are building a new API client, you must first obtain a client API key, which you can do at <https://www.rev.com/api>.
37
-
38
- All operations in the API are performed on behalf of a Rev customer, identified by their user API key. The client key / user
39
- key pair is used to authenticate each API operation. Once you have the two keys, you can create a new Rev.com API client:
40
-
41
-
42
- ```ruby
43
- require 'rev-api'
44
-
45
- rev_client = Rev.new('your_client_key', 'your_user_key')
46
- ```
47
-
48
- You can read more about authentication in the Rev.com API at <https://www.rev.com/api/security>
49
-
50
- ### Sandbox
51
-
52
- All operations can be executed against either the production or the sandbox environment. The sandbox environment allows you
53
- to test your API client without actually paying for orders and having them worked on.
54
-
55
- By default, the Rev.com API client executes against the production environment. To go against the sandbox instead,
56
- initialize the client this way:
57
-
58
- ```ruby
59
- require 'rev-api'
60
-
61
- rev_client = Rev.new('your_client_key', 'your_user_key', Rev::Api::SANDBOX_HOST)
62
- ```
63
-
64
- ### Usage
65
-
66
- The snippets below assume you've initialized `rev_client`:
67
-
68
- ```ruby
69
- rev_client = Rev.new('your_client_key', 'your_user_key')
70
- ```
71
-
72
- #### Listing orders
73
-
74
- ```ruby
75
- orders_page = rev_client.get_orders_page(1); # get a single page of orders
76
- all_orders = rev_client.get_all_orders; # get first page of orders history
77
- orders_by_client_ref = rev_client.get_orders_by_client_ref('some_ref'); # get orders with reference id of 'some_ref'
78
- ```
79
-
80
- #### Get a single order by order number
81
-
82
- ```ruby
83
- some_order = rev_client.get_order('TCxxxxxxxx')
84
- puts "Original comment: #{some_order.comments.first.text}"
85
- ```
86
-
87
- #### Cancel an order given an order number
88
-
89
- ```ruby
90
- rev_client.cancel_order('TCxxxxxxxx')
91
- ```
92
-
93
- #### Print out the text of all transcripts in an order
94
-
95
- ```ruby
96
- order = rev_client.get_order('TCxxxxxxx')
97
-
98
- order.transcripts.each do |t|
99
- puts rev_client.get_attachment_content_as_string t.id
100
- puts
101
- end
102
- ```
103
-
104
- Refer to `cli.rb` in the `examples` directory for a full example illustrating placing orders, handling error responses, etc.
105
-
106
- ### Documentation
107
-
108
- [YARD documentation](http://rubydoc.info/github/revdotcom/rev-ruby-sdk/master/frames) can be generated locally by running `rake yard` command, and will be placed in the `doc` directory.
109
-
110
- You can find API documentation at <https://www.rev.com/api/docs>.
111
-
112
- ### Support
113
-
114
- If you have any questions or suggestions for improvement, email us directly at api[at]rev[dot]com.
115
-
116
- ### Compatibility
117
-
118
- - MRI 2.0.0
119
-
120
- ### Tests
121
-
122
- Minitest suite might be run using `rake test` command.
123
- Current stack:
124
-
125
- - minitest
126
- - webmock
127
- - vcr
128
- - turn
129
-
130
- ### Dependencies
131
-
132
- - httparty
1
+ [![Gem Version](https://badge.fury.io/rb/rev-api.png)](http://badge.fury.io/rb/rev-api)
2
+ [![Build Status](https://secure.travis-ci.org/revdotcom/rev-ruby-sdk.png?branch=master)](https://secure.travis-ci.org/revdotcom/rev-ruby-sdk)
3
+ [![Dependency Status](https://gemnasium.com/revdotcom/rev-ruby-sdk.png?travis)](https://gemnasium.com/revdotcom/rev-ruby-sdk)
4
+ [![Code Climate](https://codeclimate.com/github/revdotcom/rev-ruby-sdk.png)](https://codeclimate.com/github/revdotcom/rev-ruby-sdk)
5
+
6
+ [Reference](https://www.rev.com/api/docs) | [RDocs](http://rubydoc.info/github/revdotcom/rev-ruby-sdk/master/frames)
7
+
8
+ rev-ruby-sdk
9
+ ------------
10
+
11
+ ### Background
12
+
13
+ Rev.com provides transcription, caption and translation services powered by humans. The Rev.com API allows you to tap transcription
14
+ and caption services with no manual steps. It's a RESTful API, documented at <https://www.rev.com/api/docs>.
15
+
16
+ The Ruby SDK for the Rev API provides a convenient Ruby wrapper for the Rev.com API. All operations of the API, as described
17
+ at <https://www.rev.com/api/operations>, are supported.
18
+
19
+ ### Install
20
+
21
+ ```shell
22
+ gem install rev-api
23
+ ```
24
+
25
+ or put it into your Gemfile.
26
+
27
+ When you need it:
28
+
29
+ ```ruby
30
+ require 'rev-api'
31
+ ```
32
+
33
+ ### Authentication
34
+
35
+ If you are building a new API client, you must first obtain a client API key, which you can do at <https://www.rev.com/api>.
36
+
37
+ All operations in the API are performed on behalf of a Rev customer, identified by their user API key. The client key / user
38
+ key pair is used to authenticate each API operation. Once you have the two keys, you can create a new Rev.com API client:
39
+
40
+
41
+ ```ruby
42
+ require 'rev-api'
43
+
44
+ rev_client = Rev.new('your_client_key', 'your_user_key')
45
+ ```
46
+
47
+ You can read more about authentication in the Rev.com API at <https://www.rev.com/api/security>
48
+
49
+ ### Sandbox
50
+
51
+ All operations can be executed against either the production or the sandbox environment. The sandbox environment allows you
52
+ to test your API client without actually paying for orders and having them worked on.
53
+
54
+ By default, the Rev.com API client executes against the production environment. To go against the sandbox instead,
55
+ initialize the client this way:
56
+
57
+ ```ruby
58
+ require 'rev-api'
59
+
60
+ rev_client = Rev.new('your_client_key', 'your_user_key', Rev::Api::SANDBOX_HOST)
61
+ ```
62
+
63
+ ### Usage
64
+
65
+ The snippets below assume you've initialized `rev_client`:
66
+
67
+ ```ruby
68
+ rev_client = Rev.new('your_client_key', 'your_user_key')
69
+ ```
70
+
71
+ #### Listing orders
72
+
73
+ ```ruby
74
+ orders_page = rev_client.get_orders_page(1) # get a single page of orders
75
+ all_orders = rev_client.get_all_orders # get first page of orders history
76
+ orders_by_client_ref = rev_client.get_orders_by_client_ref('some_ref') # get orders with reference id of 'some_ref'
77
+ ```
78
+
79
+ #### Get a single order by order number
80
+
81
+ ```ruby
82
+ some_order = rev_client.get_order('TCxxxxxxxx')
83
+ puts "Original comment: #{some_order.comments.first.text}"
84
+ ```
85
+
86
+ #### Cancel an order given an order number
87
+
88
+ ```ruby
89
+ rev_client.cancel_order('TCxxxxxxxx')
90
+ ```
91
+
92
+ #### Print out the text of all transcripts in an order
93
+
94
+ ```ruby
95
+ order = rev_client.get_order('TCxxxxxxx')
96
+
97
+ order.transcripts.each do |t|
98
+ puts rev_client.get_attachment_content_as_string t.id
99
+ puts
100
+ end
101
+ ```
102
+
103
+ Refer to `cli.rb` in the `examples` directory for a full example illustrating placing orders, handling error responses, etc.
104
+
105
+ ### Documentation
106
+
107
+ [YARD documentation](http://rubydoc.info/github/revdotcom/rev-ruby-sdk/master/frames) can be generated locally by running `rake yard` command, and will be placed in the `doc` directory.
108
+
109
+ You can find API documentation at <https://www.rev.com/api/docs>.
110
+
111
+ ### Support
112
+
113
+ If you have any questions or suggestions for improvement, email us directly at api[at]rev[dot]com.
114
+
115
+ ### Compatibility
116
+
117
+ - MRI 2.0.0
118
+
119
+ ### Tests
120
+
121
+ Minitest suite might be run using `rake test` command.
122
+ Current stack:
123
+
124
+ - minitest
125
+ - webmock
126
+ - vcr
127
+ - turn
128
+
129
+ ### Dependencies
130
+
131
+ - httparty
data/Rakefile CHANGED
@@ -1,14 +1,14 @@
1
- require 'rake/testtask'
2
- require 'yard'
3
- require 'rubygems/tasks'
4
-
5
- Rake::TestTask.new do |t|
6
- t.test_files = FileList['spec/lib/rev/**/*_spec.rb']
7
- t.verbose = true
8
- end
9
-
10
- YARD::Rake::YardocTask.new
11
-
12
- Gem::Tasks.new
13
-
1
+ require 'rake/testtask'
2
+ require 'yard'
3
+ require 'rubygems/tasks'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.test_files = FileList['spec/lib/rev/**/*_spec.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ YARD::Rake::YardocTask.new
11
+
12
+ Gem::Tasks.new
13
+
14
14
  task :default => :test
@@ -1,270 +1,270 @@
1
- require 'rev-api'
2
- require 'optparse'
3
- require 'pp'
4
-
5
- # An example app using the official Ruby SDK for the Rev.com API
6
- # This ruby script sets up a basic command line interface (CLI)
7
- # that prompts a user to authenticate on the web, then
8
- # allows them to type commands to manipulate their orders.
9
- #
10
- # To start, run:
11
- # ruby examples/cli.rb --sandbox --client-key your_client_key --user-key your_user_key
12
-
13
- class RevCLI
14
- COMMANDS = %w{help get get_by_client_ref list transcripts captions dl_transcripts dl_captions dl_sources place_tc place_cp cancel}
15
-
16
- def initialize
17
- options = { :environment => Rev::Api::PRODUCTION_HOST, :verbatim => false, :timestamps => false }
18
- optparse = OptionParser.new do |opts|
19
- opts.banner = "Usage: cli.rb [options]. You will be prompted for command then."
20
-
21
- opts.on("--client-key CLIENT-KEY", "Use CLIENT-KEY as the API Client Key for Authorization") do |client_key|
22
- options[:client_key] = client_key
23
- end
24
-
25
- opts.on("--user-key USER-KEY", "Use USER-KEY as the API User Key for Authorization") do |user_key|
26
- options[:user_key] = user_key
27
- end
28
-
29
- opts.on("--sandbox", "Execute against the Sandbox environment rather than production") do
30
- options[:environment] = Rev::Api::SANDBOX_HOST
31
- end
32
-
33
- opts.on("--[no-]verbatim", "Request verbatim transcription") do |v|
34
- options[:verbatim] = v
35
- end
36
-
37
- opts.on("--[no-]timestamps", "Request timestamps transcription") do |t|
38
- options[:timestamps] = t
39
- end
40
- end
41
- optparse.parse!
42
-
43
- raise OptionParser::MissingArgument, "--client-key" if options[:client_key].nil?
44
- raise OptionParser::MissingArgument, "--user-key" if options[:user_key].nil?
45
-
46
- @rev_client = Rev.new(options[:client_key], options[:user_key], options[:environment])
47
- end
48
-
49
- def command_loop
50
- puts "Enter a command or 'help' or 'exit'"
51
- command_line = ''
52
-
53
- while command_line.strip != 'exit'
54
- begin
55
- execute_command(command_line)
56
- rescue RuntimeError => e
57
- puts "Command Line Error! #{e.class}: #{e}"
58
- puts e.backtrace
59
- end
60
- print '> '
61
- command_line = gets.strip
62
- end
63
-
64
- puts 'Goodbye!'
65
- exit(0)
66
- end
67
-
68
- def execute_command(cmd_line)
69
- command = cmd_line.split
70
- method = command.first
71
- if COMMANDS.include? method
72
- begin
73
- send(method.to_sym, command[1..-1])
74
- rescue Rev::BadRequestError => e
75
- puts "Server returned error with code #{e.code}, message #{e.message}"
76
- rescue Rev::ApiError => e
77
- puts "Server returned error with message #{e.message}"
78
- end
79
- elsif command.first && !command.first.strip.empty?
80
- puts 'Invalid command. Type \'help\' to see commands.'
81
- end
82
- end
83
-
84
- def get(args)
85
- order_num = args[0]
86
- order = @rev_client.get_order order_num
87
- pp order
88
- end
89
-
90
- def list(args)
91
- orders = @rev_client.get_all_orders
92
- orders.each { |o| puts("#{o.order_number}") }
93
-
94
- puts 'There are no orders placed so far.' if orders.empty?
95
- end
96
-
97
- def get_by_client_ref(args)
98
- # client ref can contain spaces, so we have to join client ref from pieces
99
- if (args.last == '0' || args.last.to_i > 0)
100
- client_ref = args[0..-2].join(' ')
101
- else
102
- client_ref = args.join(' ')
103
- end
104
-
105
- client_ref = client_ref.gsub('\'', '').gsub('"', '')
106
- page = args.last.nil? ? 0: args.last.to_i
107
-
108
- orders_first_page = @rev_client.get_orders_by_client_ref(client_ref, page)
109
- puts "Found #{orders_first_page.total_count} order(s), showing page #{page}. Pass page number as second argument"
110
- orders_first_page.orders.each { |o| puts ("#{o.client_ref}: #{o.order_number}") }
111
-
112
- puts "There are no orders associated with the given client reference: #{client_ref}" if orders_first_page.orders.empty?
113
- end
114
-
115
- def transcripts(args)
116
- begin
117
- order_num = args[0]
118
- order = @rev_client.get_order order_num
119
-
120
- if order.transcripts.empty?
121
- puts "There are no transcripts for order #{order_num}"
122
- return
123
- end
124
-
125
- order.transcripts.each do |t|
126
- puts "Contents of #{t.name}"
127
- puts "-----------------------------------"
128
- puts @rev_client.get_attachment_content_as_string t.id
129
- puts
130
- end
131
- rescue Rev::BadRequestError => e
132
- puts "Displaying transcripts failed with error code #{e.code}, message #{e.message}"
133
- end
134
- end
135
-
136
- def captions(args)
137
- begin
138
- order_num = args[0]
139
- order = @rev_client.get_order order_num
140
-
141
- if order.captions.empty?
142
- puts "There are no captions for order #{order_num}"
143
- return
144
- end
145
-
146
- order.captions.each do |t|
147
- puts "Contents of #{t.name}"
148
- puts "-----------------------------------"
149
- puts @rev_client.get_attachment_content(t.id).body
150
- puts
151
- end
152
- rescue Rev::BadRequestError => e
153
- puts "Displaying captions failed with error code #{e.code}, message #{e.message}"
154
- end
155
- end
156
-
157
- def dl_transcripts(args)
158
- begin
159
- order_num = args[0]
160
- order = @rev_client.get_order order_num
161
-
162
- if order.transcripts.empty?
163
- puts "There are no transcripts for order #{order_num}"
164
- return
165
- end
166
-
167
- filenames = order.transcripts.map { |t| t.name}.join(',')
168
- puts "Downloading files: #{filenames}"
169
- order.transcripts.each do |t|
170
- @rev_client.save_attachment_content t.id, t.name
171
- end
172
- rescue Rev::BadRequestError => e
173
- puts "Downloading transcripts failed with error code #{e.code}, message #{e.message}"
174
- end
175
- end
176
-
177
- def dl_captions(args)
178
- begin
179
- order_num = args[0]
180
- order = @rev_client.get_order order_num
181
-
182
- if order.captions.empty?
183
- puts "There are no captions for order #{order_num}"
184
- return
185
- end
186
-
187
- filenames = order.captions.map { |t| t.name}.join(',')
188
- puts "Downloading files: #{filenames}"
189
- order.captions.each do |t|
190
- @rev_client.save_attachment_content t.id, t.name
191
- end
192
- rescue Rev::BadRequestError => e
193
- puts "Downloading captions failed with error code #{e.code}, message #{e.message}"
194
- end
195
- end
196
-
197
- def dl_sources(args)
198
- begin
199
- order_num = args[0]
200
- order = @rev_client.get_order order_num
201
-
202
- if order.sources.empty?
203
- puts "There are no source files for order #{order_num}"
204
- return
205
- end
206
-
207
- filenames = order.sources.map { |t| t.name }.join(',')
208
- puts "Downloading files: #{filenames}"
209
- order.sources.each do |t|
210
- @rev_client.save_attachment_content t.id, t.name
211
- end
212
- rescue Rev::BadRequestError => e
213
- puts "Downloading sources failed with error code #{e.code}, message #{e.message}"
214
- end
215
- end
216
-
217
- def cancel(args)
218
- begin
219
- order_num = args[0]
220
- @rev_client.cancel_order order_num
221
- puts "Order #{order_num} cancelled"
222
- rescue Rev::BadRequestError => e
223
- puts "Cancelling order failed with error code #{e.code}, message #{e.message}"
224
- end
225
- end
226
-
227
- def place_tc(args)
228
- input_urls = upload(args, 'audio/mpeg')
229
- inputs = input_urls.map { |url| Rev::Input.new(:uri => url, :audio_length_seconds => 180) }
230
- tc_options = Rev::TranscriptionOptions.new(inputs)
231
- place_helper(inputs, { :transcription_options => tc_options })
232
- end
233
-
234
- def place_cp(args)
235
- input_urls = upload(args, 'video/mpeg')
236
- inputs = input_urls.map { |url| Rev::Input.new(:uri => url, :video_length_seconds => 180) }
237
- cp_options = Rev::CaptionOptions.new(inputs, {:output_file_formats => [Rev::CaptionOptions::OUTPUT_FILE_FORMATS[:scc]] })
238
- place_helper(inputs, { :caption_options => cp_options })
239
- end
240
-
241
- def help(*args)
242
- puts "commands are: #{COMMANDS.join(' ')} exit"
243
- end
244
-
245
- private
246
-
247
- def upload(args, type)
248
- input_urls = args.map do |f|
249
- puts "Uploading #{f}"
250
- @rev_client.upload_input(f, type)
251
- end
252
- input_urls
253
- end
254
-
255
- def place_helper(inputs, options)
256
- options = options.merge({ :client_ref => 'XB432423', :comment => 'Please work quickly' })
257
- request = Rev::OrderRequest.new(options)
258
-
259
- begin
260
- new_order = @rev_client.submit_order(request)
261
- puts "New order: #{new_order}"
262
- rescue Rev::BadRequestError => e
263
- puts "Order placement failed with error code #{e.code}, message #{e.message}"
264
- end
265
- end
266
-
267
- end
268
-
269
- cli = RevCLI.new
270
- cli.command_loop
1
+ require 'rev-api'
2
+ require 'optparse'
3
+ require 'pp'
4
+
5
+ # An example app using the official Ruby SDK for the Rev.com API
6
+ # This ruby script sets up a basic command line interface (CLI)
7
+ # that prompts a user to authenticate on the web, then
8
+ # allows them to type commands to manipulate their orders.
9
+ #
10
+ # To start, run:
11
+ # ruby examples/cli.rb --sandbox --client-key your_client_key --user-key your_user_key
12
+
13
+ class RevCLI
14
+ COMMANDS = %w{help get get_by_client_ref list transcripts captions dl_transcripts dl_captions dl_sources place_tc place_cp cancel}
15
+
16
+ def initialize
17
+ options = { :environment => Rev::Api::PRODUCTION_HOST, :verbatim => false, :timestamps => false }
18
+ optparse = OptionParser.new do |opts|
19
+ opts.banner = "Usage: cli.rb [options]. You will be prompted for command then."
20
+
21
+ opts.on("--client-key CLIENT-KEY", "Use CLIENT-KEY as the API Client Key for Authorization") do |client_key|
22
+ options[:client_key] = client_key
23
+ end
24
+
25
+ opts.on("--user-key USER-KEY", "Use USER-KEY as the API User Key for Authorization") do |user_key|
26
+ options[:user_key] = user_key
27
+ end
28
+
29
+ opts.on("--sandbox", "Execute against the Sandbox environment rather than production") do
30
+ options[:environment] = Rev::Api::SANDBOX_HOST
31
+ end
32
+
33
+ opts.on("--[no-]verbatim", "Request verbatim transcription") do |v|
34
+ options[:verbatim] = v
35
+ end
36
+
37
+ opts.on("--[no-]timestamps", "Request timestamps transcription") do |t|
38
+ options[:timestamps] = t
39
+ end
40
+ end
41
+ optparse.parse!
42
+
43
+ raise OptionParser::MissingArgument, "--client-key" if options[:client_key].nil?
44
+ raise OptionParser::MissingArgument, "--user-key" if options[:user_key].nil?
45
+
46
+ @rev_client = Rev.new(options[:client_key], options[:user_key], options[:environment])
47
+ end
48
+
49
+ def command_loop
50
+ puts "Enter a command or 'help' or 'exit'"
51
+ command_line = ''
52
+
53
+ while command_line.strip != 'exit'
54
+ begin
55
+ execute_command(command_line)
56
+ rescue RuntimeError => e
57
+ puts "Command Line Error! #{e.class}: #{e}"
58
+ puts e.backtrace
59
+ end
60
+ print '> '
61
+ command_line = gets.strip
62
+ end
63
+
64
+ puts 'Goodbye!'
65
+ exit(0)
66
+ end
67
+
68
+ def execute_command(cmd_line)
69
+ command = cmd_line.split
70
+ method = command.first
71
+ if COMMANDS.include? method
72
+ begin
73
+ send(method.to_sym, command[1..-1])
74
+ rescue Rev::BadRequestError => e
75
+ puts "Server returned error with code #{e.code}, message #{e.message}"
76
+ rescue Rev::ApiError => e
77
+ puts "Server returned error with message #{e.message}"
78
+ end
79
+ elsif command.first && !command.first.strip.empty?
80
+ puts 'Invalid command. Type \'help\' to see commands.'
81
+ end
82
+ end
83
+
84
+ def get(args)
85
+ order_num = args[0]
86
+ order = @rev_client.get_order order_num
87
+ pp order
88
+ end
89
+
90
+ def list(args)
91
+ orders = @rev_client.get_all_orders
92
+ orders.each { |o| puts("#{o.order_number}") }
93
+
94
+ puts 'There are no orders placed so far.' if orders.empty?
95
+ end
96
+
97
+ def get_by_client_ref(args)
98
+ # client ref can contain spaces, so we have to join client ref from pieces
99
+ if (args.last == '0' || args.last.to_i > 0)
100
+ client_ref = args[0..-2].join(' ')
101
+ else
102
+ client_ref = args.join(' ')
103
+ end
104
+
105
+ client_ref = client_ref.gsub('\'', '').gsub('"', '')
106
+ page = args.last.nil? ? 0: args.last.to_i
107
+
108
+ orders_first_page = @rev_client.get_orders_by_client_ref(client_ref, page)
109
+ puts "Found #{orders_first_page.total_count} order(s), showing page #{page}. Pass page number as second argument"
110
+ orders_first_page.orders.each { |o| puts ("#{o.client_ref}: #{o.order_number}") }
111
+
112
+ puts "There are no orders associated with the given client reference: #{client_ref}" if orders_first_page.orders.empty?
113
+ end
114
+
115
+ def transcripts(args)
116
+ begin
117
+ order_num = args[0]
118
+ order = @rev_client.get_order order_num
119
+
120
+ if order.transcripts.empty?
121
+ puts "There are no transcripts for order #{order_num}"
122
+ return
123
+ end
124
+
125
+ order.transcripts.each do |t|
126
+ puts "Contents of #{t.name}"
127
+ puts "-----------------------------------"
128
+ puts @rev_client.get_attachment_content_as_string t.id
129
+ puts
130
+ end
131
+ rescue Rev::BadRequestError => e
132
+ puts "Displaying transcripts failed with error code #{e.code}, message #{e.message}"
133
+ end
134
+ end
135
+
136
+ def captions(args)
137
+ begin
138
+ order_num = args[0]
139
+ order = @rev_client.get_order order_num
140
+
141
+ if order.captions.empty?
142
+ puts "There are no captions for order #{order_num}"
143
+ return
144
+ end
145
+
146
+ order.captions.each do |t|
147
+ puts "Contents of #{t.name}"
148
+ puts "-----------------------------------"
149
+ puts @rev_client.get_attachment_content(t.id).body
150
+ puts
151
+ end
152
+ rescue Rev::BadRequestError => e
153
+ puts "Displaying captions failed with error code #{e.code}, message #{e.message}"
154
+ end
155
+ end
156
+
157
+ def dl_transcripts(args)
158
+ begin
159
+ order_num = args[0]
160
+ order = @rev_client.get_order order_num
161
+
162
+ if order.transcripts.empty?
163
+ puts "There are no transcripts for order #{order_num}"
164
+ return
165
+ end
166
+
167
+ filenames = order.transcripts.map { |t| t.name}.join(',')
168
+ puts "Downloading files: #{filenames}"
169
+ order.transcripts.each do |t|
170
+ @rev_client.save_attachment_content t.id, t.name
171
+ end
172
+ rescue Rev::BadRequestError => e
173
+ puts "Downloading transcripts failed with error code #{e.code}, message #{e.message}"
174
+ end
175
+ end
176
+
177
+ def dl_captions(args)
178
+ begin
179
+ order_num = args[0]
180
+ order = @rev_client.get_order order_num
181
+
182
+ if order.captions.empty?
183
+ puts "There are no captions for order #{order_num}"
184
+ return
185
+ end
186
+
187
+ filenames = order.captions.map { |t| t.name}.join(',')
188
+ puts "Downloading files: #{filenames}"
189
+ order.captions.each do |t|
190
+ @rev_client.save_attachment_content t.id, t.name
191
+ end
192
+ rescue Rev::BadRequestError => e
193
+ puts "Downloading captions failed with error code #{e.code}, message #{e.message}"
194
+ end
195
+ end
196
+
197
+ def dl_sources(args)
198
+ begin
199
+ order_num = args[0]
200
+ order = @rev_client.get_order order_num
201
+
202
+ if order.sources.empty?
203
+ puts "There are no source files for order #{order_num}"
204
+ return
205
+ end
206
+
207
+ filenames = order.sources.map { |t| t.name }.join(',')
208
+ puts "Downloading files: #{filenames}"
209
+ order.sources.each do |t|
210
+ @rev_client.save_attachment_content t.id, t.name
211
+ end
212
+ rescue Rev::BadRequestError => e
213
+ puts "Downloading sources failed with error code #{e.code}, message #{e.message}"
214
+ end
215
+ end
216
+
217
+ def cancel(args)
218
+ begin
219
+ order_num = args[0]
220
+ @rev_client.cancel_order order_num
221
+ puts "Order #{order_num} cancelled"
222
+ rescue Rev::BadRequestError => e
223
+ puts "Cancelling order failed with error code #{e.code}, message #{e.message}"
224
+ end
225
+ end
226
+
227
+ def place_tc(args)
228
+ input_urls = upload(args, 'audio/mpeg')
229
+ inputs = input_urls.map { |url| Rev::Input.new(:uri => url, :audio_length_seconds => 180) }
230
+ tc_options = Rev::TranscriptionOptions.new(inputs)
231
+ place_helper(inputs, { :transcription_options => tc_options })
232
+ end
233
+
234
+ def place_cp(args)
235
+ input_urls = upload(args, 'video/mpeg')
236
+ inputs = input_urls.map { |url| Rev::Input.new(:uri => url, :video_length_seconds => 180) }
237
+ cp_options = Rev::CaptionOptions.new(inputs, {:output_file_formats => [Rev::CaptionOptions::OUTPUT_FILE_FORMATS[:scc]] })
238
+ place_helper(inputs, { :caption_options => cp_options })
239
+ end
240
+
241
+ def help(*args)
242
+ puts "commands are: #{COMMANDS.join(' ')} exit"
243
+ end
244
+
245
+ private
246
+
247
+ def upload(args, type)
248
+ input_urls = args.map do |f|
249
+ puts "Uploading #{f}"
250
+ @rev_client.upload_input(f, type)
251
+ end
252
+ input_urls
253
+ end
254
+
255
+ def place_helper(inputs, options)
256
+ options = options.merge({ :client_ref => 'XB432423' })
257
+ request = Rev::OrderRequest.new(options)
258
+
259
+ begin
260
+ new_order = @rev_client.submit_order(request)
261
+ puts "New order: #{new_order}"
262
+ rescue Rev::BadRequestError => e
263
+ puts "Order placement failed with error code #{e.code}, message #{e.message}"
264
+ end
265
+ end
266
+
267
+ end
268
+
269
+ cli = RevCLI.new
270
+ cli.command_loop