Browse Source

Migrated TranslationAgent to liquid

Dominik Sander 11 years ago
parent
commit
39360929df

+ 6 - 5
app/models/agents/translation_agent.rb

@@ -1,5 +1,6 @@
 module Agents
   class TranslationAgent < Agent
+    include LiquidInterpolatable
 
     cannot_be_scheduled!
 
@@ -8,7 +9,7 @@ module Agents
       Services are provided using Microsoft Translator. You can [sign up](https://datamarket.azure.com/dataset/bing/microsofttranslator) and [register your application](https://datamarket.azure.com/developer/applications/register) to get `client_id` and `client_secret` which are required to use this agent.
       `to` must be filled with a [translator language code](http://msdn.microsoft.com/en-us/library/hh456380.aspx).
 
-      Specify what you would like to translate in `content` field, by specifying key and JSONPath of content to be translated.
+      Specify what you would like to translate in `content` field, you can use [Liquid](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) specify which part of the payload you want to translate.
 
       `expected_receive_period_in_days` is the maximum number of days you would allow to pass between events.
     MD
@@ -22,8 +23,8 @@ module Agents
         'to' => "fi",
         'expected_receive_period_in_days' => 1,
         'content' => {
-          'text' => "$.message.text",
-          'content' => "$.xyz"
+          'text' => "{{message.text}}",
+          'content' => "{{xyz}}"
         }
       }
     end
@@ -68,8 +69,8 @@ module Agents
       incoming_events.each do |event|
         translated_event = {}
         options['content'].each_pair do |key, value|
-          to_be_translated = Utils.values_at event.payload, value
-          translated_event[key] = translate to_be_translated.first, options['to'], access_token
+          to_be_translated = interpolate_string(value, event.payload)
+          translated_event[key] = translate(to_be_translated.first, options['to'], access_token)
         end
         create_event :payload => translated_event
       end

+ 8 - 0
db/migrate/20140501200859_migrate_translation_agent_to_liquid.rb

@@ -0,0 +1,8 @@
+class MigrateTranslationAgentToLiquid < ActiveRecord::Migration
+  def change
+    Agent.where(:type => 'Agents::TranslationAgent').each do |agent|
+      agent.options['content'] = LiquidMigrator.convert_hash(agent.options['content'], {:merge_path_attributes => true, :leading_dollarsign_is_jsonpath => true})
+      agent.save
+    end
+  end
+end

+ 6 - 2
spec/models/agents/translation_agent_spec.rb

@@ -1,6 +1,10 @@
 require 'spec_helper'
+require 'models/concerns/liquid_interpolatable'
+
 
 describe Agents::TranslationAgent do
+    it_behaves_like LiquidInterpolatable
+
     before do
         @valid_params = {
             :name    => "somename",
@@ -10,8 +14,8 @@ describe Agents::TranslationAgent do
                 :to            => "fi",
                 :expected_receive_period_in_days => 1,
                 :content       => {
-                    :text => "$.message",
-                    :content => "$.xyz"
+                    :text => "{{message}}",
+                    :content => "{{xyz}}"
                 }
             }
         }