class Sinamak::TranslationSource
Public Class Methods
new()
click to toggle source
# File lib/sinamak/translation_source.rb, line 30 def initialize @connection = CONNECTION end
Public Instance Methods
detect_language(text)
click to toggle source
# File lib/sinamak/translation_source.rb, line 38 def detect_language(text) request_language(text).body_object.lang end
supported_languages()
click to toggle source
# File lib/sinamak/translation_source.rb, line 42 def supported_languages supported_languages_response.body_object.dirs end
translate(text, from:, to:)
click to toggle source
# File lib/sinamak/translation_source.rb, line 34 def translate(text, from:, to:) request_translation(text, from, to).body_object.text end
Private Instance Methods
noop_response()
click to toggle source
# File lib/sinamak/translation_source.rb, line 48 def noop_response @noop_response ||= NoopResponse.new end
request_language(text)
click to toggle source
# File lib/sinamak/translation_source.rb, line 61 def request_language(text) @connection.post('detect') do |request| request['Content-Type'] = 'application/x-www-form-urlencoded' request.body = { key: Environment::KEY, text: text.to_s } end rescue Faraday::ClientError => e rescue_handle(e) end
request_supported_languages()
click to toggle source
# File lib/sinamak/translation_source.rb, line 70 def request_supported_languages @connection.post('getLangs') do |request| request['Content-Type'] = 'application/x-www-form-urlencoded' request.body = { key: Environment::KEY, ui: 'en' } end rescue Faraday::ClientError => e rescue_handle(e) end
request_translation(text, *lang)
click to toggle source
# File lib/sinamak/translation_source.rb, line 52 def request_translation(text, *lang) @connection.post('translate') do |request| request['Content-Type'] = 'application/x-www-form-urlencoded' request.body = { key: Environment::KEY, text: text.to_s, lang: lang.to_a.join('-') } end rescue Faraday::ClientError => e rescue_handle(e) end
rescue_handle(error)
click to toggle source
# File lib/sinamak/translation_source.rb, line 83 def rescue_handle(error) error.display_message noop_response end
supported_languages_response()
click to toggle source
# File lib/sinamak/translation_source.rb, line 79 def supported_languages_response @supported_languages_response ||= request_supported_languages end