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
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/easy_translate/translation.rb', line 64 def initialize(texts, , = {}) = .dup self.texts = texts self.html = .delete(:html) @source = .delete(:from) @target = .delete(:to) 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 = 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
100 101 102 |
# File 'lib/easy_translate/translation.rb', line 100 def body @texts.map { |t| "q=#{CGI::escape(t)}" }.join '&' end |
#multi? ⇒ Boolean
Whether or not this was a request for multiple texts
106 107 108 |
# File 'lib/easy_translate/translation.rb', line 106 def multi? @multi end |
#params ⇒ Hash
The params for this request
83 84 85 86 87 88 89 90 |
# File 'lib/easy_translate/translation.rb', line 83 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
94 95 96 |
# File 'lib/easy_translate/translation.rb', line 94 def path '/language/translate/v2' end |