Class: EasyTranslate::Translation::TranslationRequest

Inherits:
Request
  • Object
show all
Defined in:
lib/easy_translate/translation.rb

Overview

A convenience class for wrapping a translation request

Instance Attribute Summary

Attributes inherited from Request

#http_options

Instance Method Summary collapse

Methods inherited from Request

#perform_raw

Constructor Details

#initialize(texts, options, http_options = {}) ⇒ TranslationRequest

Set the texts and options

Parameters:

  • texts (String, Array)
    • the text (or texts) to translate

  • options (Hash)
    • Options to override or pass along with the request

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/easy_translate/translation.rb', line 31

def initialize(texts, options, http_options={})
  self.texts = texts
  self.html = options.delete(:html)
  @source = options.delete(:from)
  @target = options.delete(:to)
  raise ArgumentError.new('No target language provded') unless @target
  raise ArgumentError.new('Support for multiple targets dropped in V2') if @target.is_a?(Array)
  @http_options = http_options
  if options
    @options = options
    if replacement_api_key = @options.delete(:api_key)
      @options[:key] = replacement_api_key
    end
  end
end

Instance Method Details

#bodyString

The body for the request

Returns:

  • (String)

    the body for the request, URL escaped



66
67
68
# File 'lib/easy_translate/translation.rb', line 66

def body
  @texts.map { |t| "q=#{URI.escape(t)}" }.join '&'
end

#multi?Boolean

Whether or not this was a request for multiple texts

Returns:

  • (Boolean)


72
73
74
# File 'lib/easy_translate/translation.rb', line 72

def multi?
  @multi
end

#paramsHash

The params for this request

Returns:

  • (Hash)

    the params for the request



49
50
51
52
53
54
55
56
# File 'lib/easy_translate/translation.rb', line 49

def params
  params = super || {}
  params[:source] = lang(@source) unless @source.nil?
  params[:target] = lang(@target) unless @target.nil?
  params[:format] = @format unless @format.nil?
  params.merge! @options if @options
  params
end

#pathString

The path for the request

Returns:

  • (String)

    The path for the request



60
61
62
# File 'lib/easy_translate/translation.rb', line 60

def path
  '/language/translate/v2'
end