speechmatics 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -0
  3. data/lib/speechmatics.rb +2 -0
  4. data/lib/speechmatics/api.rb +1 -1
  5. data/lib/speechmatics/response.rb +9 -12
  6. data/lib/speechmatics/response/error.rb +46 -0
  7. data/lib/speechmatics/response/error/bad_gateway.rb +8 -0
  8. data/lib/speechmatics/response/error/cannot_align.rb +8 -0
  9. data/lib/speechmatics/response/error/insufficient_credit.rb +8 -0
  10. data/lib/speechmatics/response/error/internal_server_error.rb +8 -0
  11. data/lib/speechmatics/response/error/invalid_audio.rb +8 -0
  12. data/lib/speechmatics/response/error/job_in_progress.rb +8 -0
  13. data/lib/speechmatics/response/error/job_not_found.rb +8 -0
  14. data/lib/speechmatics/response/error/malformed_request.rb +8 -0
  15. data/lib/speechmatics/response/error/missing_callback.rb +8 -0
  16. data/lib/speechmatics/response/error/missing_data_file.rb +8 -0
  17. data/lib/speechmatics/response/error/missing_language.rb +8 -0
  18. data/lib/speechmatics/response/error/not_alignment_job.rb +8 -0
  19. data/lib/speechmatics/response/error/product_not_available.rb +8 -0
  20. data/lib/speechmatics/response/error/rejected_on_submission.rb +8 -0
  21. data/lib/speechmatics/response/error/service_unavailable.rb +8 -0
  22. data/lib/speechmatics/response/error/too_many_requests.rb +8 -0
  23. data/lib/speechmatics/response/error/unauthorized.rb +8 -0
  24. data/lib/speechmatics/response/error/unknown.rb +7 -0
  25. data/lib/speechmatics/response/error/unsupported_output_format.rb +8 -0
  26. data/lib/speechmatics/version.rb +1 -1
  27. data/speechmatics.gemspec +1 -0
  28. data/test/response/error_test.rb +94 -0
  29. data/test/response_test.rb +27 -0
  30. data/test/test_helper.rb +4 -0
  31. metadata +41 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ec521f15165eb5671307764e769b2d06650fe094
4
- data.tar.gz: d9ffee30ce7281635338fbdd10593ae9e1070f2d
3
+ metadata.gz: e754cb933758ea9ee1d9c6425fd294bc4c1431bd
4
+ data.tar.gz: 41b3c4c00034d9a6094b24b44f6586f658f9bafb
5
5
  SHA512:
6
- metadata.gz: e64b84ee8115088b7f3f03f4dbe42dc195101232cb5fd90ae2a4df5d67bb7389251861053a0f4e78446aa87b3154d4c88ff9890522ae395bf1023c7abe2592ac
7
- data.tar.gz: a99c7b5633fa325e24d35111d375dcbefaf4836105f5b893ad39da0c5afdf36f7e22b05af7ffed178b542d35dff93a7e1bcb16933e56095d13b97146ed589a89
6
+ metadata.gz: 13e42ae2ee8a6c501a21a537e314ccfb5f2ba3faae4a8cf538d6b1c0ce8c0e6cb337ceb50f6f348509aaa2aa0ebc76e5957938a4d99e40c38832c067551cff17
7
+ data.tar.gz: 919864f0a5588bd41547294e4775e6b0e8522bef2befa477a4976991ffcd399a9176bae238cc13c8c0a9b794110d12f357cfe0d3e94b7734d2fe3eb47c8df1fe
data/README.md CHANGED
@@ -73,6 +73,12 @@ trans = c.user.jobs.transcript(5678)
73
73
 
74
74
  ## Changes
75
75
 
76
+ * 0.2.1 - 13 Sept 2017
77
+ - Updated error handling with a class for each error type. Thanks @mziwisky
78
+
79
+ * 0.2.0 - 10/12/2016
80
+ - Added alignment support, thanks @rogerz42892
81
+
76
82
  * 0.1.4 - 8/21/2015
77
83
  - Add support for video files.
78
84
 
@@ -7,6 +7,8 @@ require "speechmatics/version"
7
7
  require 'speechmatics/configuration'
8
8
  require 'speechmatics/connection'
9
9
  require 'speechmatics/response'
10
+ require 'speechmatics/response/error'
11
+ Gem.find_files('speechmatics/response/error/*.rb').each { |path| require path }
10
12
  require 'speechmatics/api'
11
13
  require 'speechmatics/api_factory'
12
14
  require 'speechmatics/client'
@@ -48,7 +48,7 @@ module Speechmatics
48
48
  request.body = params[:data]
49
49
  end
50
50
  end
51
- Speechmatics::Response.new(response, {api: self, method: method, path: path, params: params})
51
+ Speechmatics::Response.parse(response, {api: self, method: method, path: path, params: params})
52
52
  end
53
53
 
54
54
  def base_path
@@ -2,25 +2,22 @@
2
2
 
3
3
  module Speechmatics
4
4
  class Response
5
+
6
+ def self.parse(response, request={})
7
+ new(response, request).tap do |res|
8
+ res.check_for_error
9
+ end
10
+ end
11
+
5
12
  attr_accessor :raw, :request
6
13
 
7
14
  def initialize(response, request={})
8
15
  @raw = response
9
16
  @request = request
10
-
11
- check_for_error(response)
12
17
  end
13
18
 
14
- def check_for_error(response)
15
- status_code_type = response.status.to_s[0]
16
- case status_code_type
17
- when "2"
18
- # puts "all is well, status: #{response.status}"
19
- when "4", "5"
20
- raise "Whoops, error back from Speechmatics: #{response.status}"
21
- else
22
- raise "Unrecongized status code: #{response.status}"
23
- end
19
+ def check_for_error
20
+ raise Error.classify(self) if raw.status >= 400
24
21
  end
25
22
 
26
23
  def body
@@ -0,0 +1,46 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Speechmatics
4
+ class Response
5
+ class Error < StandardError
6
+ TYPES = []
7
+
8
+ class << self
9
+ def inherited(subclass)
10
+ TYPES << subclass
11
+ end
12
+
13
+ def classify(response)
14
+ klass = TYPES.find { |t| t.matches?(response) } || Unknown
15
+ klass.new(response)
16
+ end
17
+
18
+ def matches?(response)
19
+ matches_status?(response.raw.status) &&
20
+ matches_message?(error_message(response))
21
+ end
22
+
23
+ def matches_status?(status)
24
+ self::ERROR_STATUS == status
25
+ end
26
+
27
+ def matches_message?(message)
28
+ self::ERROR_MESSAGE == message
29
+ end
30
+
31
+ def error_message(response)
32
+ response['error']
33
+ rescue
34
+ ''
35
+ end
36
+ end
37
+
38
+ attr_reader :response
39
+
40
+ def initialize(response)
41
+ @response = response
42
+ super self.class.error_message(response)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class BadGateway < self
5
+ ERROR_STATUS = 502
6
+ def self.matches_message?(_message); true; end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class CannotAlign < self
5
+ ERROR_STATUS = 404
6
+ ERROR_MESSAGE = 'Text could not be aligned to audio'
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class InsufficientCredit < self
5
+ ERROR_STATUS = 403
6
+ ERROR_MESSAGE = 'Insufficient Credit'
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class InternalServerError < self
5
+ ERROR_STATUS = 500
6
+ def self.matches_message?(_message); true; end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class InvalidAudio < self
5
+ ERROR_STATUS = 403
6
+ ERROR_MESSAGE = 'Job rejected due to invalid audio'
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class JobInProgress < self
5
+ ERROR_STATUS = 404
6
+ ERROR_MESSAGE = 'Job In Progress'
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class JobNotFound < self
5
+ ERROR_STATUS = 404
6
+ ERROR_MESSAGE = 'Job not found'
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class MalformedRequest < self
5
+ ERROR_STATUS = 400
6
+ ERROR_MESSAGE = 'Malformed request'
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class MissingCallback < self
5
+ ERROR_STATUS = 400
6
+ ERROR_MESSAGE = 'Missing callback'
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class MissingDataFile < self
5
+ ERROR_STATUS = 400
6
+ ERROR_MESSAGE = 'Missing data_file'
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class MissingLanguage < self
5
+ ERROR_STATUS = 400
6
+ ERROR_MESSAGE = 'No language selected'
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class NotAlignmentJob < self
5
+ ERROR_STATUS = 404
6
+ ERROR_MESSAGE = 'Job is not of type alignment'
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class ProductNotAvailable < self
5
+ ERROR_STATUS = 400
6
+ ERROR_MESSAGE = 'Requested product not available'
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class RejectedOnSubmission < self
5
+ ERROR_STATUS = 404
6
+ ERROR_MESSAGE = 'Job was rejected on submission'
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class ServiceUnavailable < self
5
+ ERROR_STATUS = 503
6
+ def self.matches_message?(_message); true; end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class TooManyRequests < self
5
+ ERROR_STATUS = 429
6
+ ERROR_MESSAGE = 'Too Many requests'
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class Unauthorized < self
5
+ ERROR_STATUS = 401
6
+ ERROR_MESSAGE = 'Invalid User Id or Token'
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class Unknown < self
5
+ def self.matches?(_response); false; end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Speechmatics::Response::Error
4
+ class UnsupportedOutputFormat < self
5
+ ERROR_STATUS = 404
6
+ ERROR_MESSAGE = 'Output format Not Supported'
7
+ end
8
+ end
@@ -1,5 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  module Speechmatics
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.1'
5
5
  end
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
28
28
 
29
29
  spec.add_development_dependency "bundler", "~> 1.3"
30
30
  spec.add_development_dependency "rake"
31
+ spec.add_development_dependency "byebug"
31
32
 
32
33
  spec.add_development_dependency('minitest')
33
34
  spec.add_development_dependency('simplecov')
@@ -0,0 +1,94 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
4
+
5
+ describe Speechmatics::Response::Error do
6
+ described_class = Speechmatics::Response::Error
7
+
8
+ describe '::classify' do
9
+ # see error table at https://app.speechmatics.com/api-details#errorCodes
10
+
11
+ describe '4xx errors' do
12
+ tests = [{
13
+ response: {status: 400, error: 'Malformed request'},
14
+ error_class: described_class::MalformedRequest
15
+ },{
16
+ response: {status: 400, error: 'Missing data_file'},
17
+ error_class: described_class::MissingDataFile
18
+ },{
19
+ response: {status: 400, error: 'No language selected'},
20
+ error_class: described_class::MissingLanguage
21
+ },{
22
+ response: {status: 400, error: 'Missing callback'},
23
+ error_class: described_class::MissingCallback
24
+ },{
25
+ response: {status: 400, error: 'Requested product not available'},
26
+ error_class: described_class::ProductNotAvailable
27
+ },{
28
+ response: {status: 401, error: 'Invalid User Id or Token'},
29
+ error_class: described_class::Unauthorized
30
+ },{
31
+ response: {status: 403, error: 'Job rejected due to invalid audio'},
32
+ error_class: described_class::InvalidAudio
33
+ },{
34
+ response: {status: 403, error: 'Insufficient Credit'},
35
+ error_class: described_class::InsufficientCredit
36
+ },{
37
+ response: {status: 404, error: 'Job not found'},
38
+ error_class: described_class::JobNotFound
39
+ },{
40
+ response: {status: 404, error: 'Text could not be aligned to audio'},
41
+ error_class: described_class::CannotAlign
42
+ },{
43
+ response: {status: 404, error: 'Job was rejected on submission'},
44
+ error_class: described_class::RejectedOnSubmission
45
+ },{
46
+ response: {status: 404, error: 'Job is not of type alignment'},
47
+ error_class: described_class::NotAlignmentJob
48
+ },{
49
+ response: {status: 404, error: 'Job In Progress'},
50
+ error_class: described_class::JobInProgress
51
+ },{
52
+ response: {status: 404, error: 'Output format Not Supported'},
53
+ error_class: described_class::UnsupportedOutputFormat
54
+ },{
55
+ response: {status: 429, error: 'Too Many requests'},
56
+ error_class: described_class::TooManyRequests
57
+ }]
58
+
59
+ tests.each do |t|
60
+ it "correctly classifies #{t[:response][:status]}: #{t[:response][:error]}" do
61
+ raw_response = raw_response_stub(
62
+ t[:response][:status],
63
+ { 'error' => t[:response][:error] }
64
+ )
65
+ response = Speechmatics::Response.new(raw_response)
66
+ err = described_class.classify(response)
67
+ err.class.must_equal t[:error_class]
68
+ end
69
+ end
70
+ end
71
+
72
+ describe '5xx errors' do
73
+ tests = [{
74
+ status: 500,
75
+ error_class: described_class::InternalServerError
76
+ },{
77
+ status: 502,
78
+ error_class: described_class::BadGateway
79
+ },{
80
+ status: 503,
81
+ error_class: described_class::ServiceUnavailable
82
+ }]
83
+
84
+ tests.each do |t|
85
+ it "correctly classifies status #{t[:status]}" do
86
+ raw_response = raw_response_stub(t[:status])
87
+ response = Speechmatics::Response.new(raw_response)
88
+ err = described_class.classify(response)
89
+ err.class.must_equal t[:error_class]
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
4
+
5
+ describe Speechmatics::Response do
6
+ describe '::parse' do
7
+ describe 'with an error response' do
8
+ let(:response) { raw_response_stub(422) }
9
+
10
+ it 'raises a Speechmatics::Response::Error' do
11
+ lambda {
12
+ Speechmatics::Response.parse(response)
13
+ }.must_raise Speechmatics::Response::Error
14
+ end
15
+ end
16
+
17
+ describe 'with an successful response' do
18
+ let(:response) { raw_response_stub(200, 'good stuff') }
19
+
20
+ it 'returns a Response object' do
21
+ res = Speechmatics::Response.parse(response)
22
+ res.class.must_equal Speechmatics::Response
23
+ res.body.must_equal 'good stuff'
24
+ end
25
+ end
26
+ end
27
+ end
@@ -9,3 +9,7 @@ require 'minitest/autorun'
9
9
  require 'minitest/spec'
10
10
 
11
11
  require 'speechmatics'
12
+
13
+ def raw_response_stub(status, body = '')
14
+ Struct.new(:status, :body).new(status, body)
15
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: speechmatics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kuklewicz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-12 00:00:00.000000000 Z
11
+ date: 2017-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: byebug
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
139
153
  - !ruby/object:Gem::Dependency
140
154
  name: minitest
141
155
  requirement: !ruby/object:Gem::Requirement
@@ -184,12 +198,34 @@ files:
184
198
  - lib/speechmatics/connection.rb
185
199
  - lib/speechmatics/resource.rb
186
200
  - lib/speechmatics/response.rb
201
+ - lib/speechmatics/response/error.rb
202
+ - lib/speechmatics/response/error/bad_gateway.rb
203
+ - lib/speechmatics/response/error/cannot_align.rb
204
+ - lib/speechmatics/response/error/insufficient_credit.rb
205
+ - lib/speechmatics/response/error/internal_server_error.rb
206
+ - lib/speechmatics/response/error/invalid_audio.rb
207
+ - lib/speechmatics/response/error/job_in_progress.rb
208
+ - lib/speechmatics/response/error/job_not_found.rb
209
+ - lib/speechmatics/response/error/malformed_request.rb
210
+ - lib/speechmatics/response/error/missing_callback.rb
211
+ - lib/speechmatics/response/error/missing_data_file.rb
212
+ - lib/speechmatics/response/error/missing_language.rb
213
+ - lib/speechmatics/response/error/not_alignment_job.rb
214
+ - lib/speechmatics/response/error/product_not_available.rb
215
+ - lib/speechmatics/response/error/rejected_on_submission.rb
216
+ - lib/speechmatics/response/error/service_unavailable.rb
217
+ - lib/speechmatics/response/error/too_many_requests.rb
218
+ - lib/speechmatics/response/error/unauthorized.rb
219
+ - lib/speechmatics/response/error/unknown.rb
220
+ - lib/speechmatics/response/error/unsupported_output_format.rb
187
221
  - lib/speechmatics/user.rb
188
222
  - lib/speechmatics/user/jobs.rb
189
223
  - lib/speechmatics/version.rb
190
224
  - speechmatics.gemspec
191
225
  - test/api_test.rb
192
226
  - test/client_test.rb
227
+ - test/response/error_test.rb
228
+ - test/response_test.rb
193
229
  - test/test_helper.rb
194
230
  - test/user/jobs_test.rb
195
231
  - test/user_test.rb
@@ -215,13 +251,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
215
251
  version: '0'
216
252
  requirements: []
217
253
  rubyforge_project:
218
- rubygems_version: 2.6.4
254
+ rubygems_version: 2.4.5.2
219
255
  signing_key:
220
256
  specification_version: 4
221
257
  summary: 'Ruby client for Speechmatics API: https://speechmatics.com/api-details'
222
258
  test_files:
223
259
  - test/api_test.rb
224
260
  - test/client_test.rb
261
+ - test/response/error_test.rb
262
+ - test/response_test.rb
225
263
  - test/test_helper.rb
226
264
  - test/user/jobs_test.rb
227
265
  - test/user_test.rb