Class: EasyTranslate::Detection::DetectionRequest

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

Overview

A convenience class for wrapping a detection 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 = {}) ⇒ DetectionRequest

Set the texts and options

Parameters:

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

  • options (Hash) (defaults to: {})
    • Options to override or pass along with the request



29
30
31
32
33
34
35
# File 'lib/easy_translate/detection.rb', line 29

def initialize(texts, options = {}, http_options = {})
  super(options, http_options)
  if replacement_api_key = @options.delete(:api_key)
    @options[:key] = replacement_api_key
  end
  self.texts = texts
end

Instance Method Details

#bodyString

The body for the request

Returns:

  • (String)

    the body for the request, URL escaped



53
54
55
# File 'lib/easy_translate/detection.rb', line 53

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)


59
60
61
# File 'lib/easy_translate/detection.rb', line 59

def multi?
  @multi
end

#paramsHash

The params for this request

Returns:

  • (Hash)

    the params for the request



39
40
41
42
43
# File 'lib/easy_translate/detection.rb', line 39

def params
  params = super || {}
  params.merge! @options if @options
  params
end

#pathString

The path for the request

Returns:

  • (String)

    The path for the request



47
48
49
# File 'lib/easy_translate/detection.rb', line 47

def path
  '/language/translate/v2/detect'
end