~singpolyma/sgx-jmp

4ace34178d25dd9d356a5ade1bbc29f249094c79 — Stephen Paul Weber 13 days ago a9caee5
Log errors from rev.ai
2 files changed, 22 insertions(+), 4 deletions(-)

M lib/rev_ai.rb
M web.rb
M lib/rev_ai.rb => lib/rev_ai.rb +12 -0
@@ 5,6 5,8 @@ require "em_promise"
require "json"

class RevAi
	class Failed < StandardError; end

	def initialize(token: CONFIG[:rev_ai_token])
		@token = token
	end


@@ 24,6 26,8 @@ class RevAi

	def stt_result(job)
		job = job["job"]
		return failed(job) if job["status"] == "failed"

		req(
			:get,
			"https://api.rev.ai/speechtotext/v1/jobs/#{job['id']}/transcript",


@@ 46,6 50,8 @@ class RevAi

	def language_id_result(job)
		job = job["job"]
		return failed(job) if job["status"] == "failed"

		req(
			:get,
			"https://api.rev.ai/languageid/v1/jobs/#{job['id']}/result"


@@ 55,6 61,8 @@ class RevAi
		end
	end

protected

	def req(m, url, accept: nil, **kwargs)
		EM::HttpRequest.new(
			url, tls: { verify_peer: true }


@@ 67,4 75,8 @@ class RevAi
			}, body: kwargs.to_json
		)
	end

	def failed(job)
		EMPromise.reject(Failed.new("#{job['failure']} #{job['failure_detail']}"))
	end
end

M web.rb => web.rb +10 -4
@@ 241,7 241,7 @@ class Web < Roda
						end

						r.post "language_id" do
							rev_ai.language_id_result(params).then do |result|
							rev_ai.language_id_result(params).then { |result|
								rev_ai.stt(
									result["top_language"],
									result.dig("metadata", "media_url"),


@@ 251,11 251,14 @@ class Web < Roda
									)),
									**result["metadata"].transform_keys(&:to_sym)
								).then { "OK" }
							end
							}.catch_only(RevAi::Failed) { |e|
								log_error(e)
								"Failure logged"
							}
						end

						r.post "transcription" do
							rev_ai.stt_result(params).then do |result|
							rev_ai.stt_result(params).then { |result|
								next "OK" if result["text"].empty?

								customer_repo.find(


@@ 270,7 273,10 @@ class Web < Roda

									"OK"
								end
							end
							}.catch_only(RevAi::Failed) { |e|
								log_error(e)
								"Failure logged"
							}
						end

						r.post do