Class: YandexTranslator::Yat
- Inherits:
-
Object
- Object
- YandexTranslator::Yat
- Includes:
- Celluloid, Celluloid::Internals::Logger, Celluloid::Notifications
- Defined in:
- lib/yat.rb
Instance Method Summary collapse
- #__receive_responses(topic, responses) ⇒ Object
- #__return_responses(topic, response, index) ⇒ Object
- #ambulance(actor, reason) ⇒ Object
- #detect(params) ⇒ Object
- #finalize ⇒ Object
- #get_languages(params = nil) ⇒ Object
-
#initialize(format = :json) ⇒ Yat
constructor
A new instance of Yat.
- #parse_loop ⇒ Object
- #parser ⇒ Object
- #translate(params) ⇒ Object
Constructor Details
#initialize(format = :json) ⇒ Yat
Returns a new instance of Yat.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/yat.rb', line 33 def initialize(format = :json) subscribe(:translator_ready, :__receive_responses) subscribe(:parsing_done, :__return_responses) @format = format @responses_num = 0 @counter = 0 @translator = YandexTranslator::Medium.new_link @parse_loop = self.async.parse_loop end |
Instance Method Details
#__receive_responses(topic, responses) ⇒ Object
123 124 125 126 |
# File 'lib/yat.rb', line 123 def __receive_responses(topic, responses) @responses_num = responses.length signal :got_responses, responses end |
#__return_responses(topic, response, index) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/yat.rb', line 105 def __return_responses(topic, response, index) if response["message"] error response["message"] @result.push nil signal :return return end @counter += 1 @result[index] = response if @counter == @responses_num @responses_num = @counter = 0 signal :return end end |
#ambulance(actor, reason) ⇒ Object
23 24 25 26 |
# File 'lib/yat.rb', line 23 def ambulance(actor, reason) warn "#{actor.inspect} died because of #{reason.inspect}" unless reason.nil? signal :return end |
#detect(params) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/yat.rb', line 83 def detect(params) unless params[:text] error "text parameter missing" return nil end @result = [] @translator.async.detect(@format, params) wait :return @result = @result.shift if @result.size == 1 @result end |
#finalize ⇒ Object
28 29 30 31 |
# File 'lib/yat.rb', line 28 def finalize @parse_loop.terminate @translator.terminate if @translator.alive? end |
#get_languages(params = nil) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/yat.rb', line 75 def get_languages(params = nil) @result = [] params ||= Hash.new @translator.async.getLangs(@format, params) wait :return @result.shift end |
#parse_loop ⇒ Object
44 45 46 47 48 49 |
# File 'lib/yat.rb', line 44 def parse_loop loop do responses = wait :got_responses responses.each.with_index { |response, index| parser.async.parse(response.body, index) } end end |
#parser ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/yat.rb', line 96 def parser case @format when :json JSONParser.new_link when :xml XMLParser.new_link end end |
#translate(params) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/yat.rb', line 51 def translate(params) unless params[:text] error "wrong or missing parameters: ':text'" return nil end unless params[:lang] error "wrong or missing parameters: ':lang'" return nil end @result = [] @translator.async.translate(@format, params) wait :return res = Hash.new("") res = @result.shift if @result @result.each do |hash| res["text"] += hash["text"] end end res end |