class EasyTranslate::Translation::TranslationRequest
A convenience class for wrapping a translation request
Public Class Methods
new(texts, options, http_options = {})
click to toggle source
Set the texts and options @param [String, Array] texts - the text (or texts) to translate @param [Hash] options - Options to override or pass along with the request
# 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
Public Instance Methods
body()
click to toggle source
The body for the request @return [String] the body for the request, URL escaped
# File lib/easy_translate/translation.rb, line 85 def body @texts.map { |t| "q=#{CGI::escape(t)}" }.join '&' end
multi?()
click to toggle source
Whether or not this was a request for multiple texts @return [Boolean]
# File lib/easy_translate/translation.rb, line 91 def multi? @multi end
params()
click to toggle source
The params for this request @return [Hash] the params for the request
Calls superclass method
EasyTranslate::Request#params
# 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
path()
click to toggle source
The path for the request @return [String] The path for the request
# File lib/easy_translate/translation.rb, line 79 def path '/language/translate/v2' end
Private Instance Methods
html=(b)
click to toggle source
Set the HTML attribute, if true add a format @param [Boolean] b - Whether or not the text supplied iS HTML
# File lib/easy_translate/translation.rb, line 109 def html=(b) @format = b ? 'html' : nil end
lang(orig)
click to toggle source
Look up a language in the table (if needed)
# File lib/easy_translate/translation.rb, line 98 def lang(orig) look = orig.is_a?(String) ? orig : orig.to_s return look if LANGUAGES[look] # shortcut iteration if val = LANGUAGES.detect { |k, v| v == look } return val.first end look end
texts=(texts)
click to toggle source
Set the texts for this request @param [String, Array] texts - The text or texts for this request
# File lib/easy_translate/translation.rb, line 115 def texts=(texts) if texts.is_a?(String) @multi = false @texts = [texts] else @multi = true @texts = texts end end