The GoogleCalendarPublishAgent now accepts Liquid in the calendar_id option

Andrew Cantino 8 年之前
父节点
当前提交
d6a0e3d8fb
共有 2 个文件被更改,包括 93 次插入33 次删除
  1. 3 4
      app/models/agents/google_calendar_publish_agent.rb
  2. 90 29
      spec/models/agents/google_calendar_publish_agent_spec.rb

+ 3 - 4
app/models/agents/google_calendar_publish_agent.rb

@@ -25,7 +25,7 @@ module Agents
25 25
 
26 26
       Agent Configuration:
27 27
 
28
-      `calendar_id` - The id the calendar you want to publish to. Typically your google account email address.
28
+      `calendar_id` - The id the calendar you want to publish to. Typically your google account email address.  Liquid formatting (e.g. `{{ cal_id }}`) is allowed here in order to extract the calendar_id from the incoming event.
29 29
 
30 30
       `google` A hash of configuration options for the agent.
31 31
 
@@ -35,7 +35,6 @@ module Agents
35 35
 
36 36
       `google` `key_secret` - The secret for the key, typically 'notasecret'
37 37
 
38
-      
39 38
 
40 39
       Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent.
41 40
 
@@ -94,8 +93,8 @@ module Agents
94 93
      incoming_events.each do |event|
95 94
         calendar = GoogleCalendar.new(options, Rails.logger)
96 95
 
97
-        calendar_event = JSON.parse(calendar.publish_as(options['calendar_id'], event.payload["message"]).response.body)
98
-  
96
+        calendar_event = JSON.parse(calendar.publish_as(interpolated(event)['calendar_id'], event.payload["message"]).response.body)
97
+
99 98
         create_event :payload => {
100 99
           'success' => true,
101 100
           'published_calendar_event' => calendar_event,

+ 90 - 29
spec/models/agents/google_calendar_publish_agent_spec.rb

@@ -1,43 +1,104 @@
1 1
 require 'rails_helper'
2 2
 
3 3
 describe Agents::GoogleCalendarPublishAgent, :vcr do
4
-  before do
5
-    @valid_params = {
6
-        'expected_update_period_in_days' => "10",
7
-        'calendar_id' => 'sqv39gj35tc837gdns1g4d81cg@group.calendar.google.com',
8
-        'google' => {
9
-          'key_file' => File.dirname(__FILE__) + '/../../data_fixtures/private.key',
10
-          'key_secret' => 'notasecret',
11
-          'service_account_email' => '1029936966326-ncjd7776pcspc98hsg82gsb56t3217ef@developer.gserviceaccount.com'
12
-        }
4
+  let(:valid_params) do
5
+    {
6
+      'expected_update_period_in_days' => "10",
7
+      'calendar_id' => calendar_id,
8
+      'google' => {
9
+        'key_file' => File.dirname(__FILE__) + '/../../data_fixtures/private.key',
10
+        'key_secret' => 'notasecret',
11
+        'service_account_email' => '1029936966326-ncjd7776pcspc98hsg82gsb56t3217ef@developer.gserviceaccount.com'
13 12
       }
14
-    @checker = Agents::GoogleCalendarPublishAgent.new(:name => "somename", :options => @valid_params)
15
-    @checker.user = users(:jane)
16
-    @checker.save!
13
+    }
14
+  end
15
+
16
+  let(:agent) do
17
+    _agent = Agents::GoogleCalendarPublishAgent.new(name: "somename", options: valid_params)
18
+    _agent.user = users(:jane)
19
+    _agent.save!
20
+    _agent
17 21
   end
18 22
 
19 23
   describe '#receive' do
20
-    it 'should publish any payload it receives' do
21
-      event1 = Event.new
22
-      event1.agent = agents(:bob_manual_event_agent)
23
-      event1.payload = {
24
-        'message' => { 
25
-          'visibility' => 'default',
26
-          'summary' => "Awesome event",
27
-          'description' => "An example event with text. Pro tip: DateTimes are in RFC3339",
28
-          'end' => {
29
-            'dateTime' => '2014-10-02T11:00:00-05:00'
30
-          },
31
-          'start' => {
32
-            'dateTime' => '2014-10-02T10:00:00-05:00'
33
-          }
24
+    let(:event) do
25
+      _event = Event.new
26
+      _event.agent = agents(:bob_manual_event_agent)
27
+      _event.payload = { 'message' => message }
28
+      _event.save!
29
+      _event
30
+    end
31
+
32
+    let(:calendar_id) { 'sqv39gj35tc837gdns1g4d81cg@group.calendar.google.com' }
33
+    let(:message) do
34
+      {
35
+        'visibility' => 'default',
36
+        'summary' => "Awesome event",
37
+        'description' => "An example event with text. Pro tip: DateTimes are in RFC3339",
38
+        'end' => {
39
+          'dateTime' => '2014-10-02T11:00:00-05:00'
40
+        },
41
+        'start' => {
42
+          'dateTime' => '2014-10-02T10:00:00-05:00'
34 43
         }
35 44
       }
36
-      event1.save!
45
+    end
46
+    let(:response_body) do
47
+      {"kind"=>"calendar#event",
48
+        "etag"=>"\"2908684044040000\"",
49
+        "id"=>"baz",
50
+        "status"=>"confirmed",
51
+        "htmlLink"=>
52
+          "https://calendar.google.com/calendar/event?eid=foobar",
53
+        "created"=>"2016-02-01T15:53:41.000Z",
54
+        "updated"=>"2016-02-01T15:53:42.020Z",
55
+        "summary"=>"Awesome event",
56
+        "description"=>
57
+          "An example event with text. Pro tip: DateTimes are in RFC3339",
58
+        "creator"=>
59
+          {"email"=>
60
+            "blah-foobar@developer.gserviceaccount.com"},
61
+        "organizer"=>
62
+          {"email"=>calendar_id,
63
+            "displayName"=>"Huginn Location Log",
64
+            "self"=>true},
65
+        "start"=>{"dateTime"=>"2014-10-03T00:30:00+09:30"},
66
+        "end"=>{"dateTime"=>"2014-10-03T01:30:00+09:30"},
67
+        "iCalUID"=>"blah@google.com",
68
+        "sequence"=>0,
69
+        "reminders"=>{"useDefault"=>true}
70
+      }.to_json
71
+    end
72
+
73
+    before do
74
+      fake_interface = Object.new
75
+      mock(GoogleCalendar).new(agent.options, Rails.logger) { fake_interface }
76
+      mock(fake_interface).publish_as(calendar_id, message) { stub!.response.stub!.body { response_body } }
77
+    end
78
+
79
+    describe 'when the calendar_id is in the options' do
80
+      it 'should publish any payload it receives' do
81
+        expect {
82
+          agent.receive([event])
83
+        }.to change { agent.events.count }.by(1)
84
+
85
+        expect(agent.events.last.payload).to eq({ "success" => true, "published_calendar_event" => JSON.parse(response_body), "agent_id" => event.agent_id, "event_id" => event.id })
86
+      end
87
+    end
88
+
89
+    describe 'with Liquid templating' do
90
+      it 'should allow Liquid in the calendar_id' do
91
+        agent.options['calendar_id'] = '{{ cal_id }}'
92
+        agent.save!
93
+
94
+        event.payload['cal_id'] = calendar_id
95
+        event.save!
37 96
 
38
-      @checker.receive([event1])
97
+        agent.receive([event])
39 98
 
40
-      expect(@checker.events.count).to eq(1)
99
+        expect(agent.events.count).to eq(1)
100
+        expect(agent.events.last.payload).to eq({ "success" => true, "published_calendar_event" => JSON.parse(response_body), "agent_id" => event.agent_id, "event_id" => event.id })
101
+      end
41 102
     end
42 103
   end
43 104
 end