Sha256: 1e93a91ce98024d43403a658711e50b21cc35068b7434eec8d4fbccc4dbe55fb

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

require "faraday"

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

module GoogleTranslateTts
  include Constanting
  include Fileable
  include Utils

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

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

        file.write(response.body)

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

    private

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

    def default_params
      { ie: "UTF-8", client: "tw-ob", q: "", tl: "en_US" }
    end

    def build_params(**custom_params)
      custom_params[:tl] ||= "en"
      params_encode(default_params.merge(**custom_params))
    end

    def params_encode(params)
      params
        .map { |(key, value)| "#{key}=#{url_encode(value)}" }
        .join("&")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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