Class: TranslatorText::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/translator_text/client.rb

Constant Summary collapse

API_VERSION =
'3.0'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Initialize the client

Parameters:

  • api_key (String)

    the Cognitive Services API Key

Since:

  • 1.0.0



17
18
19
# File 'lib/translator_text/client.rb', line 17

def initialize(api_key)
  @api_key = api_key
end

Instance Method Details

#detect(sentences) ⇒ Array<TranslatorText::Types::DetectionResult>

Identifies the language of a piece of text.

The following limitations apply:

  • The array sentences can have at most 100 elements.

  • The text value of an array element cannot exceed 10,000 characters including spaces.

  • The entire text included in the request cannot exceed 50,000 characters including spaces.

Parameters:

Returns:

See Also:



52
53
54
55
56
57
58
59
# File 'lib/translator_text/client.rb', line 52

def detect(sentences)
  results = post(
    '/detect',
    body: build_sentences(sentences).to_json
  )

  results.map { |r| Types::DetectionResult.new(r) }
end

#translate(sentences, to:, **options) ⇒ Array<TranslatorText::Types::TranslationResult>

Translate a group of sentences.

The following limitations apply:

  • The array sentences can have at most 25 elements.

  • The entire text included in the request cannot exceed 5,000 characters including spaces.

Parameters:

  • sentences (Array<String, TranslatorText::Types::Sentence>)

    the sentences to process

  • to (Symbol)

    Specifies the language of the output text (required)

  • options (Hash)

    the optional options to transmit to the service. Consult the API documentation for the exhaustive available options.

Returns:

See Also:



32
33
34
35
36
37
38
39
40
# File 'lib/translator_text/client.rb', line 32

def translate(sentences, to:, **options)
  results = post(
    '/translate',
    body: build_sentences(sentences).to_json,
    query: Hash[to: to, **options]
  )

  results.map { |r| Types::TranslationResult.new(r) }
end