Merge pull request #348 from dsander/liquid-credentials

Added a liquid tag to access user credentials

Dominik Sander 10 年 前
コミット
44853e481f
共有2 個のファイルを変更した32 個の追加6 個の削除を含む
  1. 19 4
      app/concerns/liquid_interpolatable.rb
  2. 13 2
      spec/models/concerns/liquid_interpolatable.rb

+ 19 - 4
app/concerns/liquid_interpolatable.rb

@@ -4,7 +4,7 @@ module LiquidInterpolatable
4 4
   def interpolate_options(options, payload)
5 5
     case options.class.to_s
6 6
     when 'String'
7
-      Liquid::Template.parse(options).render(payload)
7
+      interpolate_string(options, payload)
8 8
     when 'ActiveSupport::HashWithIndifferentAccess', 'Hash'
9 9
       duped_options = options.dup
10 10
       duped_options.each do |key, value|
@@ -18,17 +18,32 @@ module LiquidInterpolatable
18 18
   end
19 19
 
20 20
   def interpolate_string(string, payload)
21
-    Liquid::Template.parse(string).render(payload)
21
+    Liquid::Template.parse(string).render!(payload, registers: {agent: self})
22 22
   end
23 23
 
24 24
   require 'uri'
25 25
   # Percent encoding for URI conforming to RFC 3986.
26 26
   # Ref: http://tools.ietf.org/html/rfc3986#page-12
27
-  module Huginn
27
+  module Filters
28 28
     def uri_escape(string)
29 29
       CGI::escape string
30 30
     end
31 31
   end
32
+  Liquid::Template.register_filter(LiquidInterpolatable::Filters)
32 33
 
33
-  Liquid::Template.register_filter(LiquidInterpolatable::Huginn)
34
+  module Tags
35
+    class Credential < Liquid::Tag
36
+      def initialize(tag_name, name, tokens)
37
+        super
38
+        @credential_name = name.strip
39
+      end
40
+
41
+      def render(context)
42
+        credential = context.registers[:agent].credential(@credential_name)
43
+        raise "No user credential named '#{@credential_name}' defined" if credential.nil?
44
+        credential
45
+      end
46
+    end
47
+  end
48
+  Liquid::Template.register_tag('credential', LiquidInterpolatable::Tags::Credential)
34 49
 end

+ 13 - 2
spec/models/concerns/liquid_interpolatable.rb

@@ -49,8 +49,19 @@ shared_examples_for LiquidInterpolatable do
49 49
     end
50 50
 
51 51
     it "should work for strings" do
52
-      @checker.send(:interpolate_string, "{{variable}}", @event.payload).should == "hello"
53
-      @checker.send(:interpolate_string, "{{variable}} you", @event.payload).should == "hello you"
52
+      @checker.interpolate_string("{{variable}}", @event.payload).should == "hello"
53
+      @checker.interpolate_string("{{variable}} you", @event.payload).should == "hello you"
54
+    end
55
+  end
56
+  describe "liquid tags" do
57
+    it "should work with existing credentials" do
58
+      @checker.interpolate_string("{% credential aws_key %}", {}).should == '2222222222-jane'
59
+    end
60
+
61
+    it "should raise an exception for undefined credentials" do
62
+      expect {
63
+        @checker.interpolate_string("{% credential unknown %}", {})
64
+      }.to raise_error
54 65
     end
55 66
   end
56 67
 end