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)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/easy_translate/translation.rb', line 47

def initialize(texts, options, http_options = {})
  options = options.dup
  self.texts = texts
  self.html = options.delete(:html)
  @source = options.delete(:from)
  @target = options.delete(:to)
  @model = options.delete(:model)
  raise ArgumentError.new('No target language provided') 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



85
86
87
# File 'lib/easy_translate/translation.rb', line 85

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

#multi?Boolean

Whether or not this was a request for multiple texts

Returns:

  • (Boolean)


91
92
93
# File 'lib/easy_translate/translation.rb', line 91

def multi?
  @multi
end

#paramsHash

The params for this request

Returns:

  • (Hash)

    the params for the request



67
68
69
70
71
72
73
74
75
# File 'lib/easy_translate/translation.rb', line 67

def params
  params          = super || {}
  params[:source] = lang(@source) unless @source.nil?
  params[:target] = lang(@target) unless @target.nil?
  params[:model]  = @model unless @model.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



79
80
81
# File 'lib/easy_translate/translation.rb', line 79

def path
  '/language/translate/v2'
end