Преглед на файлове

Implement the "merge" mode for GoogleTranslationAgent

Akinori MUSHA преди 4 месеца
родител
ревизия
c2df2da949
променени са 2 файла, в които са добавени 29 реда и са изтрити 5 реда
  1. 20 5
      app/models/agents/google_translation_agent.rb
  2. 9 0
      spec/models/agents/google_translation_agent_spec.rb

+ 20 - 5
app/models/agents/google_translation_agent.rb

@@ -28,6 +28,8 @@ module Agents
       Specify an object in `content` field using [Liquid](https://github.com/huginn/huginn/wiki/Formatting-Events-using-Liquid) expressions, which will be evaluated for each incoming event, and then translated to become the payload of the new event.
       You can specify a nested object of any levels containing arrays and objects, and all string values except for object keys will be recursively translated.
 
+      Set `mode` to `merge` if you want to merge each translated content with the original event payload.  The default behavior (`clean`) is to emit events with only translated contents.
+
       `expected_receive_period_in_days` is the maximum number of days you would allow to pass between events.
     MD
 
@@ -35,13 +37,14 @@ module Agents
 
     def default_options
       {
-        'to' => "sv",
+        'mode' => 'clean',
+        'to' => 'sv',
         'from' => 'en',
         'google_api_key' => '',
         'expected_receive_period_in_days' => 1,
         'content' => {
           'text' => "{{message}}",
-          'moretext' => "{{another message}}"
+          'moretext' => "{{another_message}}"
         }
       }
     end
@@ -54,14 +57,26 @@ module Agents
       unless options['google_api_key'].present? && options['to'].present? && options['content'].present? && options['expected_receive_period_in_days'].present?
         errors.add :base, "google_api_key, to, content and expected_receive_period_in_days are all required"
       end
+
+      case options['mode'].presence
+      when nil, /\A(?:clean|merge)\z|\{/
+        # ok
+      else
+        errors.add(:base, "mode must be 'clean' or 'merge'")
+      end
     end
 
     def receive(incoming_events)
       incoming_events.each do |event|
         interpolate_with(event) do
-          translated_event = translate(interpolated['content'])
-
-          create_event payload: translated_event
+          translated_content = translate(interpolated['content'])
+
+          case interpolated['mode']
+          when 'merge'
+            create_event payload: event.payload.merge(translated_content)
+          else
+            create_event payload: translated_content
+          end
         end
       end
     end

+ 9 - 0
spec/models/agents/google_translation_agent_spec.rb

@@ -79,5 +79,14 @@ describe Agents::GoogleTranslationAgent, :vcr do
       @checker.options[:to] = ""
       expect(@checker).not_to be_valid
     end
+
+    it "should validate the value of 'mode' key" do
+      @checker.options[:mode] = "clean"
+      expect(@checker).to be_valid
+      @checker.options[:mode] = "merge"
+      expect(@checker).to be_valid
+      @checker.options[:mode] = "clear"
+      expect(@checker).not_to be_valid
+    end
   end
 end