huginn_google_calendar_event_checker_agent 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 78038dac4d5a6cb199e500c9eaca8b462762eca345a420f09518f9b3293a6cb5
4
+ data.tar.gz: 007a49bf1c8858d6ef3a4de4c9a4dc0f9baaf4b2f9ca2589425dbba79d4599b3
5
+ SHA512:
6
+ metadata.gz: b0af8fd2c1b8b6e6362d09f26bfaa288c6188c3b035b06be1a9709f77e71d345183d1418ee6d043c2e0329502762c922dff775087b7f0a68419030134e6c7300
7
+ data.tar.gz: 3bc7799a2512f243978e0c3b453a7904d93e16edee8db20d9006359b32c8ea3fb5b85aa48213dff57d68959a35b3292cd05f9164ec87b22a2f120dc692e06dfa
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2024 Nicolas Germain
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,167 @@
1
+ module Agents
2
+ class GoogleCalendarEventCheckerAgent < Agent
3
+ include FormConfigurable
4
+ can_dry_run!
5
+ no_bulk_receive!
6
+ default_schedule 'every_1h'
7
+
8
+ description do
9
+ <<-MD
10
+ The GoogleCalendarEventChecker Agent interacts with Google calendar's api.
11
+
12
+ `debug` is used for verbose mode.
13
+
14
+ `time_max` is needed to limit the result.
15
+
16
+ `calendar_id` is the wanted id of the calendar you want to check.
17
+
18
+ `service_acount_credentials` is needed for auth.
19
+
20
+ `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
21
+ that you anticipate passing without this Agent receiving an incoming Event.
22
+ MD
23
+ end
24
+
25
+ event_description <<-MD
26
+ Events look like this:
27
+
28
+ {
29
+ "created": "2024-06-02T10:05:50.000+00:00",
30
+ "creator": {
31
+ "email": "XXXXXXXXXX@gmail.com"
32
+ },
33
+ "end": {
34
+ "dateTime": "2024-06-02T14:00:00.000+02:00",
35
+ "timeZone": "Europe/Paris"
36
+ },
37
+ "etag": "\"XXXXXXXXXXXXXXXX\"",
38
+ "htmlLink": "https://www.google.com/calendar/event?eid=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
39
+ "iCalUID": "XXXXXXXXXXXXXXXXXXXXXXXXXX@google.com",
40
+ "id": "XXXXXXXXXXXXXXXXXXXXXXXXXX",
41
+ "kind": "calendar#event",
42
+ "organizer": {
43
+ "displayName": "XXXXXXXXXXXXX",
44
+ "email": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX@group.calendar.google.com",
45
+ "self": true
46
+ },
47
+ "reminders": {
48
+ "useDefault": true
49
+ },
50
+ "sequence": 0,
51
+ "start": {
52
+ "dateTime": "2024-06-02T13:00:00.000+02:00",
53
+ "timeZone": "Europe/Paris"
54
+ },
55
+ "status": "confirmed",
56
+ "summary": "test new event",
57
+ "updated": "2024-06-02T10:05:50.005+00:00"
58
+ }
59
+
60
+ MD
61
+
62
+ def default_options
63
+ {
64
+ 'calendar_id' => '',
65
+ 'time_max' => '10',
66
+ 'service_acount_credentials' => '',
67
+ 'debug' => 'false',
68
+ 'expected_receive_period_in_days' => '31',
69
+ }
70
+ end
71
+
72
+ form_configurable :calendar_id, type: :string
73
+ form_configurable :time_max, type: :string
74
+ form_configurable :service_acount_credentials, type: :string
75
+ form_configurable :debug, type: :boolean
76
+ form_configurable :expected_receive_period_in_days, type: :string
77
+ def validate_options
78
+
79
+ unless options['calendar_id'].present?
80
+ errors.add(:base, "calendar_id is a required field")
81
+ end
82
+
83
+ unless options['service_acount_credentials'].present?
84
+ errors.add(:base, "service_acount_credentials is a required field")
85
+ end
86
+
87
+ unless options['time_max'].present?
88
+ errors.add(:base, "time_max is a required field")
89
+ end
90
+
91
+ if options.has_key?('debug') && boolify(options['debug']).nil?
92
+ errors.add(:base, "if provided, debug must be true or false")
93
+ end
94
+
95
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
96
+ errors.add(:base, "Please provide 'expected_receive_period_in_days' to indicate how many days can pass before this Agent is considered to be not working")
97
+ end
98
+ end
99
+
100
+ def working?
101
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
102
+ end
103
+
104
+ def check
105
+ check_events
106
+ end
107
+
108
+ private
109
+
110
+ def already_notified(id)
111
+ memory['notified'].include?(id)
112
+ end
113
+
114
+ def clean_notified(response)
115
+ current_event_ids = response.items.map(&:id)
116
+ memory['notified'].reject! { |id| !current_event_ids.include?(id) }
117
+ memory['notified'].uniq!
118
+ end
119
+
120
+ def check_events()
121
+ calendar = Google::Apis::CalendarV3::CalendarService.new
122
+ calendar.client_options.application_name = 'App Name'
123
+ calendar.client_options.application_version = 'App Version'
124
+
125
+ #deal with possible service account in huginn
126
+ Tempfile.create(['service_acount_credentials_google_calendar', '.json']) do |file|
127
+ file.write(interpolated['service_acount_credentials'])
128
+ file.rewind
129
+ ENV['GOOGLE_APPLICATION_CREDENTIALS'] = file.path
130
+ scopes = [Google::Apis::CalendarV3::AUTH_CALENDAR]
131
+ calendar.authorization = Google::Auth.get_application_default(scopes)
132
+ end
133
+ time_max = (Time.now + interpolated['time_max'].to_i * 24 * 60 * 60).iso8601
134
+ begin
135
+ response = calendar.list_events(interpolated['calendar_id'],
136
+ max_results: 10,
137
+ single_events: true,
138
+ order_by: 'startTime',
139
+ time_min: Time.now.iso8601,
140
+ time_max: time_max)
141
+
142
+ if response.items.empty?
143
+ memory['notified'] = []
144
+ if interpolated['debug'] == 'true'
145
+ puts 'No upcoming events found'
146
+ end
147
+ else
148
+ response.items.each do |event|
149
+ memory['notified'] = [] if memory['notified'].nil?
150
+ if event.id && !already_notified(event.id)
151
+ if interpolated['debug'] == 'true'
152
+ log "not already notified"
153
+ end
154
+ create_event payload: event.to_json
155
+ memory['notified'] << event.id
156
+ else
157
+ if interpolated['debug'] == 'true'
158
+ log "already notified"
159
+ end
160
+ end
161
+ end
162
+ clean_notified(response)
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_google_calendar_event_checker_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_google_calendar_event_checker_agent/google_calendar_event_checker_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::GoogleCalendarEventCheckerAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::GoogleCalendarEventCheckerAgent.new.default_options
7
+ @checker = Agents::GoogleCalendarEventCheckerAgent.new(:name => "GoogleCalendarEventCheckerAgent", :options => @valid_options)
8
+ @checker.user = users(:bob)
9
+ @checker.save!
10
+ end
11
+
12
+ pending "add specs here"
13
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huginn_google_calendar_event_checker_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Germain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-06-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 12.3.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 12.3.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: huginn_agent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Write a longer description or delete this line.
56
+ email:
57
+ - ngermain@hihouhou.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - lib/huginn_google_calendar_event_checker_agent.rb
64
+ - lib/huginn_google_calendar_event_checker_agent/google_calendar_event_checker_agent.rb
65
+ - spec/google_calendar_event_checker_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_google_calendar_event_checker_agent
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.3.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Write a short summary, because Rubygems requires one.
89
+ test_files:
90
+ - spec/google_calendar_event_checker_agent_spec.rb