Add 'no_merge' option to PostAgent.

Andrew Cantino 10 years ago
parent
commit
f23dea9472
2 changed files with 51 additions and 7 deletions
  1. 16 6
      app/models/agents/post_agent.rb
  2. 35 1
      spec/models/agents/post_agent_spec.rb

+ 16 - 6
app/models/agents/post_agent.rb

@@ -5,7 +5,7 @@ module Agents
5 5
     default_schedule "never"
6 6
 
7 7
     description <<-MD
8
-      A PostAgent receives events from other agents (or runs periodically), merges those events with the contents of `payload`, and sends the results as POST (or GET) requests to a specified url.
8
+      A PostAgent receives events from other agents (or runs periodically), merges those events with the [Liquid-interpolated](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) contents of `payload`, and sends the results as POST (or GET) requests to a specified url.  To skip merging in the incoming event, but still send the interpolated payload, set `no_merge` to `true`.
9 9
 
10 10
       The `post_url` field must specify where you would like to send requests. Please include the URI scheme (`http` or `https`).
11 11
 
@@ -21,11 +21,12 @@ module Agents
21 21
     def default_options
22 22
       {
23 23
         'post_url' => "http://www.example.com",
24
-        'expected_receive_period_in_days' => 1,
24
+        'expected_receive_period_in_days' => '1',
25 25
         'content_type' => 'form',
26 26
         'method' => 'post',
27 27
         'payload' => {
28
-          'key' => 'value'
28
+          'key' => 'value',
29
+          'something' => 'the event contained {{ somekey }}'
29 30
         },
30 31
         'headers' => {}
31 32
       }
@@ -52,8 +53,12 @@ module Agents
52 53
         errors.add(:base, "if provided, payload must be a hash")
53 54
       end
54 55
 
55
-      unless %w[post get].include?(method)
56
-        errors.add(:base, "method must be 'post' or 'get'")
56
+      unless %w[post get put delete patch].include?(method)
57
+        errors.add(:base, "method must be 'post', 'get', 'put', 'delete', or 'patch'")
58
+      end
59
+
60
+      if options['no_merge'].present? && !%[true false].include?(options['no_merge'].to_s)
61
+        errors.add(:base, "if provided, no_merge must be 'true' or 'false'")
57 62
       end
58 63
 
59 64
       unless headers.is_a?(Hash)
@@ -63,7 +68,12 @@ module Agents
63 68
 
64 69
     def receive(incoming_events)
65 70
       incoming_events.each do |event|
66
-        handle (interpolated(event.payload)['payload'].presence || {}).merge(event.payload)
71
+        outgoing = interpolated(event.payload)['payload'].presence || {}
72
+        if interpolated['no_merge'].to_s == 'true'
73
+          handle outgoing
74
+        else
75
+          handle outgoing.merge(event.payload)
76
+        end
67 77
       end
68 78
     end
69 79
 

+ 35 - 1
spec/models/agents/post_agent_spec.rb

@@ -38,6 +38,7 @@ describe Agents::PostAgent do
38 38
         'post' => Net::HTTP::Post, 'patch' => Net::HTTP::Patch,
39 39
         'delete' => Net::HTTP::Delete }.each.with_index do |(verb, type), index|
40 40
         @checker.options['method'] = verb
41
+        @checker.should be_valid
41 42
         @checker.check
42 43
         @requests.should == index + 1
43 44
         @sent_requests[type].length.should == 1
@@ -76,6 +77,15 @@ describe Agents::PostAgent do
76 77
 
77 78
       @sent_requests[Net::HTTP::Get][0].should == @event.payload.merge('default' => 'value')
78 79
     end
80
+
81
+    it "can skip merging the incoming event when no_merge is set, but it still interpolates" do
82
+      @checker.options['no_merge'] = 'true'
83
+      @checker.options['payload'] = {
84
+        'key' => 'it said: {{ someotherkey.somekey }}'
85
+      }
86
+      @checker.receive([@event])
87
+      @sent_requests[Net::HTTP::Post].first.should == { 'key' => 'it said: value' }
88
+    end
79 89
   end
80 90
 
81 91
   describe "#check" do
@@ -125,7 +135,7 @@ describe Agents::PostAgent do
125 135
       @checker.should_not be_valid
126 136
     end
127 137
 
128
-    it "should validate method as post or get, defaulting to post" do
138
+    it "should validate method as post, get, put, patch, or delete, defaulting to post" do
129 139
       @checker.options['method'] = ""
130 140
       @checker.method.should == "post"
131 141
       @checker.should be_valid
@@ -138,11 +148,35 @@ describe Agents::PostAgent do
138 148
       @checker.method.should == "get"
139 149
       @checker.should be_valid
140 150
 
151
+      @checker.options['method'] = "patch"
152
+      @checker.method.should == "patch"
153
+      @checker.should be_valid
154
+
141 155
       @checker.options['method'] = "wut"
142 156
       @checker.method.should == "wut"
143 157
       @checker.should_not be_valid
144 158
     end
145 159
 
160
+    it "should validate that no_merge is 'true' or 'false', if present" do
161
+      @checker.options['no_merge'] = ""
162
+      @checker.should be_valid
163
+
164
+      @checker.options['no_merge'] = "true"
165
+      @checker.should be_valid
166
+
167
+      @checker.options['no_merge'] = "false"
168
+      @checker.should be_valid
169
+
170
+      @checker.options['no_merge'] = false
171
+      @checker.should be_valid
172
+
173
+      @checker.options['no_merge'] = true
174
+      @checker.should be_valid
175
+
176
+      @checker.options['no_merge'] = 'blarg'
177
+      @checker.should_not be_valid
178
+    end
179
+
146 180
     it "should validate payload as a hash, if present" do
147 181
       @checker.options['payload'] = ""
148 182
       @checker.should be_valid