@@ -25,17 +25,16 @@ 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 |
|
| 32 | 32 |
`google` `service_account_email` - The authorised service account. |
| 33 | 33 |
|
| 34 |
- `google` `key_file` - The path to the key file. |
|
| 34 |
+ `google` `key_file` OR `google` `key` - The path to the key file or the key itself. Liquid formatting is supported if you want to use a Credential. (E.g., `{% credential google_key %}`)
|
|
| 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 |
|
@@ -92,10 +91,10 @@ module Agents |
||
| 92 | 91 |
|
| 93 | 92 |
def receive(incoming_events) |
| 94 | 93 |
incoming_events.each do |event| |
| 95 |
- calendar = GoogleCalendar.new(options, Rails.logger) |
|
| 94 |
+ calendar = GoogleCalendar.new(interpolate_options(options, event), Rails.logger) |
|
| 95 |
+ |
|
| 96 |
+ calendar_event = JSON.parse(calendar.publish_as(interpolated(event)['calendar_id'], event.payload["message"]).response.body) |
|
| 96 | 97 |
|
| 97 |
- calendar_event = JSON.parse(calendar.publish_as(options['calendar_id'], event.payload["message"]).response.body) |
|
| 98 |
- |
|
| 99 | 98 |
create_event :payload => {
|
| 100 | 99 |
'success' => true, |
| 101 | 100 |
'published_calendar_event' => calendar_event, |
@@ -1,7 +1,13 @@ |
||
| 1 | 1 |
class GoogleCalendar |
| 2 | 2 |
def initialize(config, logger) |
| 3 | 3 |
@config = config |
| 4 |
- @key = Google::APIClient::PKCS12.load_key(@config['google']['key_file'], @config['google']['key_secret']) |
|
| 4 |
+ |
|
| 5 |
+ if @config['google']['key'].present? |
|
| 6 |
+ @key = OpenSSL::PKCS12.new(@config['google']['key'], @config['google']['key_secret']).key |
|
| 7 |
+ else |
|
| 8 |
+ @key = Google::APIClient::PKCS12.load_key(@config['google']['key_file'], @config['google']['key_secret']) |
|
| 9 |
+ end |
|
| 10 |
+ |
|
| 5 | 11 |
@client = Google::APIClient.new(application_name: "Huginn", application_version: "0.0.1") |
| 6 | 12 |
@client.retries = 2 |
| 7 | 13 |
@logger ||= logger |
@@ -60,4 +66,4 @@ class GoogleCalendar |
||
| 60 | 66 |
@logger.debug ret.to_yaml |
| 61 | 67 |
ret |
| 62 | 68 |
end |
| 63 |
-end |
|
| 69 |
+end |
@@ -1,43 +1,124 @@ |
||
| 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 |
+ def setup_mock! |
|
| 74 |
+ fake_interface = Object.new |
|
| 75 |
+ mock(GoogleCalendar).new(agent.interpolate_options(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 |
+ setup_mock! |
|
| 82 |
+ |
|
| 83 |
+ expect {
|
|
| 84 |
+ agent.receive([event]) |
|
| 85 |
+ }.to change { agent.events.count }.by(1)
|
|
| 86 |
+ |
|
| 87 |
+ 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 })
|
|
| 88 |
+ end |
|
| 89 |
+ end |
|
| 90 |
+ |
|
| 91 |
+ describe 'with Liquid templating' do |
|
| 92 |
+ it 'should allow Liquid in the calendar_id' do |
|
| 93 |
+ setup_mock! |
|
| 94 |
+ |
|
| 95 |
+ agent.options['calendar_id'] = '{{ cal_id }}'
|
|
| 96 |
+ agent.save! |
|
| 97 |
+ |
|
| 98 |
+ event.payload['cal_id'] = calendar_id |
|
| 99 |
+ event.save! |
|
| 100 |
+ |
|
| 101 |
+ agent.receive([event]) |
|
| 102 |
+ |
|
| 103 |
+ expect(agent.events.count).to eq(1) |
|
| 104 |
+ 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 })
|
|
| 105 |
+ end |
|
| 106 |
+ |
|
| 107 |
+ it 'should allow Liquid in the key' do |
|
| 108 |
+ agent.options['google'].delete('key_file')
|
|
| 109 |
+ agent.options['google']['key'] = '{% credential google_key %}'
|
|
| 110 |
+ agent.save! |
|
| 111 |
+ |
|
| 112 |
+ users(:jane).user_credentials.create! credential_name: 'google_key', credential_value: 'something' |
|
| 113 |
+ |
|
| 114 |
+ agent.reload |
|
| 115 |
+ |
|
| 116 |
+ setup_mock! |
|
| 37 | 117 |
|
| 38 |
- @checker.receive([event1]) |
|
| 118 |
+ agent.receive([event]) |
|
| 39 | 119 |
|
| 40 |
- expect(@checker.events.count).to eq(1) |
|
| 120 |
+ expect(agent.events.count).to eq(1) |
|
| 121 |
+ end |
|
| 41 | 122 |
end |
| 42 | 123 |
end |
| 43 | 124 |
end |