class TranslatorText::Client

Constants

API_VERSION

Public Class Methods

new(api_key) click to toggle source

Initialize the client @since 1.0.0

@param api_key [String] the Cognitive Services API Key

# File lib/translator_text/client.rb, line 17
def initialize(api_key)
  @api_key = api_key
end

Public Instance Methods

detect(sentences) click to toggle source

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.

@see docs.microsoft.com/en-us/azure/cognitive-services/translator/reference/v3-0-detect

@param sentences [Array<String, TranslatorText::Types::Sentence>] the sentences to process @return [Array<TranslatorText::Types::DetectionResult>] the detection results

# 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) click to toggle source

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.

@see docs.microsoft.com/en-us/azure/cognitive-services/translator/reference/v3-0-translate

@param sentences [Array<String, TranslatorText::Types::Sentence>] the sentences to process @param to [Symbol] Specifies the language of the output text (required) @param options [Hash] the optional options to transmit to the service. Consult the API documentation for the exhaustive available options. @return [Array<TranslatorText::Types::TranslationResult>] the translation results

# 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

Private Instance Methods

build_sentences(sentences) click to toggle source
# File lib/translator_text/client.rb, line 63
def build_sentences(sentences)
  sentences.map do |sentence|
    Types::Sentence(sentence)
  end
end
handle_response(response) click to toggle source

Handle the response

If success, return the response body If failure, raise an error

# File lib/translator_text/client.rb, line 85
def handle_response(response)
  case response.code
  when 200..299
    response
  else
    if response.request.format == :json
      raise ServiceError.new(
        code: response['error']['code'],
        message: response['error']['message']
      )
    else
      raise NetError.new(
        code: response.code,
        message: response.response.message
      )
    end
  end
end
headers() click to toggle source
# File lib/translator_text/client.rb, line 104
def headers
  {
    'Ocp-Apim-Subscription-Key' => @api_key,
    'X-ClientTraceId' => SecureRandom.uuid,
    'Content-type' => 'application/json'
  }
end
post(path, params) click to toggle source
# File lib/translator_text/client.rb, line 69
def post(path, params)
  options = {
    headers: headers,
    query: {}
  }.merge(params)

  options[:query][:'api-version'] = API_VERSION

  response = self.class.post(path, options)
  handle_response(response)
end