Class: EasyTranslate::Translation::TranslationRequest
- Defined in:
- lib/easy_translate/translation.rb
Overview
A convenience class for wrapping a translation request
Instance Attribute Summary
Attributes inherited from Request
Instance Method Summary collapse
-
#body ⇒ String
The body for the request.
-
#initialize(texts, options, http_options = {}) ⇒ TranslationRequest
constructor
Set the texts and options.
-
#multi? ⇒ Boolean
Whether or not this was a request for multiple texts.
-
#params ⇒ Hash
The params for this request.
-
#path ⇒ String
The path for the request.
Methods inherited from Request
Constructor Details
#initialize(texts, options, http_options = {}) ⇒ TranslationRequest
Set the texts and options
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/easy_translate/translation.rb', line 32 def initialize(texts, , = {}) self.texts = texts self.html = .delete(:html) @source = .delete(:from) @target = .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 = if @options = if replacement_api_key = @options.delete(:api_key) @options[:key] = replacement_api_key end end end |
Instance Method Details
#body ⇒ String
The body for the request
67 68 69 |
# File 'lib/easy_translate/translation.rb', line 67 def body @texts.map { |t| "q=#{CGI::escape(t)}" }.join '&' end |
#multi? ⇒ Boolean
Whether or not this was a request for multiple texts
73 74 75 |
# File 'lib/easy_translate/translation.rb', line 73 def multi? @multi end |
#params ⇒ Hash
The params for this request
50 51 52 53 54 55 56 57 |
# File 'lib/easy_translate/translation.rb', line 50 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 |
#path ⇒ String
The path for the request
61 62 63 |
# File 'lib/easy_translate/translation.rb', line 61 def path '/language/translate/v2' end |