Sha256: 958e3c2e850520d3e240d825dc8e116bffa5a040a7079128ac2bf6b18db9b516

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require "cgi"
require "faraday"
require "fileutils"
require "securerandom"
require "tempfile"

require_relative "google_translate_tts/constanting"
require_relative "google_translate_tts/fileable"
require_relative "google_translate_tts/version"

module GoogleTranslateTts
  include Constanting
  include Fileable

  class << self
    def fetch(phrase, **options)
      with_temp_file do |file|
        lang = options.fetch(:lang, "en_US")
        filename = options.fetch(:file, "file")

        response = Faraday.get(tts_resource_url(q: phrase, tl: lang))

        file.write(response.body)

        FileUtils.cp(file.path, "#{filename}.mp3")
      end
    end

    private

    def tts_resource_url(**params)
      "#{self::BASE_URL}/translate_tts?#{build_params(**params)}"
    end

    def build_params(**kwargs)
      kwargs[:tl] ||= 'en'

      params = {
        ie: 'UTF-8',
        client: 'tw-ob',
        q: "",
      }.merge(**kwargs)

      # params.map { |(key, value)| pp [key, value] }
      params.map { |(key, value)| "#{key}=#{CGI.escape(value)}" }.join("&")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
google-translate-tts-0.1.0 lib/google_translate_tts.rb