Coder Social home page Coder Social logo

fun_translations's Introduction

FunTranslations API Ruby interface

Gem CI Coverage Status Maintainability Downloads total

This is a Ruby client that enables you to easily perform translations using FunTranslations API.

If would like to learn how this gem was built, please check out this and subsequent lessons on my YT channel (currently only available in Russian).

Prerequisites

Ruby 2.7+ and RubyGems subsystem is required.

Installation

Install this gem by running:

$ gem install fun_translations

Or add it to your Gemfile:

gem 'fun_translations'

And run:

bundle install

Usage

Include the client in your script:

require 'fun_translations'

Next, instantiate the client:

client = FunTranslations.client

And perform translations:

translation_yoda = client.translate :yoda, "Hello, my padawan"
translation_klingon = client.translate :klingon, "We are klingons."
translation_morse = client.translate 'morse/audio', "Morse code is dit and dash."

Additional request parameters

In most cases you will only need to pass the text to translate. However, certain translators (for example, 'morse/audio') might require additional params. You can specify these params as a third hash argument:

translation_morse = client.translate 'morse/audio', "Morse code is dit and dash.", speed: 5, tone: 700

Translation object

The translate method returns an instance of the FunTranslations::Translation class. It responds to the following methods:

  • translated_text — the actual translation result. Please note that in very rare cases this method will return nil, specifically, when you are using 'morse/audio' translator that returns encoded audio only.
  • original_text — your initial (base) text.
  • translation — translator that you've chosen.
translation_yoda.translated_text # => 'A planet, master obi wan lost'
translation_yoda.original_text # => 'Master Obi Wan lost a planet'
translation_yoda.translation # => 'yoda'

There are additional methods available only for "audio" translators:

  • audio — returns base64-encoded audio stream.
  • speed — audio speed.
  • tone — audio tone.
translation_morse = client.translate 'morse/audio', "Morse code is dit and dash.", speed: 5, tone: 700

translation_yoda.audio # => 'data:audio/wave;base64,UklGRjiBCQBXQVZFZm1...'
translation_yoda.speed # => '5 WPM'
translation_yoda.tone # => '700 Hz'

# Please note that the `translation` method returns a hash in this case:

translation_yoda.translation['source'] # => 'english'
translation_yoda.translation['destination'] # => 'morse audio'

API token

FunTranslations API has a free pricing plan which does not require registering and obtaining an API key. In other words, you can start using the gem right away. However, please be aware that the free plan allows only 5 requests per hour and 60 requests per day. Therefore, you might want to purchase a paid plan and obtain an API key. Then, simply pass this key when instantiating the client:

client = FunTranslations.client('123abc')

Then perform translations as usual.

Running tests

Tests are written in RSpec (all HTTP requests are stubbed):

rspec .

Observe test results and coverage.

Copyright and license

Licensed under the MIT license.

Copyright (c) 2022 Ilya Krukowski

fun_translations's People

Contributors

bodrovis avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.