Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set/unset alarms (configurable) #29

Merged
merged 12 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ filtering options (looking at your Google Calendar)?
Use ical-filter-proxy to (you guessed it) proxy and filter your iCal feed. Define your source
calendar and filtering options in `config.yml` and you're good to go.

In addition, display alarms can be created or cleared.

## Configuration

```yaml
Expand All @@ -27,7 +29,12 @@ my_calendar_name:
- field: summary # summary and description supported
operator: matches # match against regex pattern
val: # array of values also supported
- "/Team A/i"
- '/Team A/i'
alarms: # (optional) create/clear alarms for filtered events
clear_existing: true # (optional) if true, existing alarms will be removed, default: false
triggers: # (optional) triggers for new alarms. Description will be the alarm summary, action is 'DISPLAY'
- '-P1DT0H0M0S' # iso8061 supported
- 2 days # supports full day[s], hour[s], minute[s], no combination in one trigger
```

## Additional Rules
Expand Down
5 changes: 5 additions & 0 deletions config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ rota:
- field: start_time
operator: equals
val: '09:00'
alarms:
clear_existing: true
triggers:
- '-P1DT0H0M0S'
- '-P1DT1H1M2S'
27 changes: 25 additions & 2 deletions lib/ical_filter_proxy/calendar.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
module IcalFilterProxy
class Calendar
attr_accessor :ical_url, :api_key, :timezone, :filter_rules
attr_accessor :ical_url, :api_key, :timezone, :filter_rules, :clear_existing_alarms, :alarm_triggers

def initialize(ical_url, api_key, timezone = 'UTC')
self.ical_url = ical_url
self.api_key = api_key
self.timezone = timezone

self.filter_rules = []
self.clear_existing_alarms = false
self.alarm_triggers = []
end

def add_rule(field, operator, value)
self.filter_rules << FilterRule.new(field, operator, value)
end

def add_alarm_trigger(alarm_trigger)
self.alarm_triggers << alarm_trigger
end

def set_clear_existing_alarms
odin568 marked this conversation as resolved.
Show resolved Hide resolved
self.clear_existing_alarms = true
end

def filtered_calendar
filtered_calendar = Icalendar::Calendar.new

filtered_events.each do |original_event|
filtered_calendar.add_event(original_event)
end

filtered_calendar.events.select do |e|
e.alarms.clear if clear_existing_alarms
alarm_triggers.each do |t|
e.alarm do |a|
a.action = "DISPLAY"
a.description = e.summary
a.trigger = t
end
end
end

filtered_calendar.to_ical
end

Expand All @@ -39,7 +62,7 @@ def original_ics
end

def raw_original_ical
open(ical_url).read
URI.open(ical_url).read
end
end
end
32 changes: 32 additions & 0 deletions lib/ical_filter_proxy/calendar_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def initialize(calendar_config)
def build
create_calendar
add_rules
add_alarms

calendar
end
Expand All @@ -33,5 +34,36 @@ def add_rules
end
end

def add_alarms
alarms = calendar_config["alarms"]
return unless alarms

if alarms["clear_existing"] == true
odin568 marked this conversation as resolved.
Show resolved Hide resolved
calendar.set_clear_existing_alarms
end

triggers = alarms["triggers"]
return unless triggers

triggers.each do |trigger|
calendar.add_alarm_trigger(generate_iso_format(trigger))
end
odin568 marked this conversation as resolved.
Show resolved Hide resolved
end

def generate_iso_format(input)
case input
when /(\d+)\s+days?/i
return "-P#{$1}D"
odin568 marked this conversation as resolved.
Show resolved Hide resolved
when /(\d+)\s+hours?/i
return "-PT#{$1}H"
when /(\d+)\s+minutes?/i
return "-PT#{$1}M"
when /^-P.*/
return input
else
raise "Unknown trigger pattern: " + input
end
end

end
end
6 changes: 5 additions & 1 deletion spec/ical_filter_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ def example_config
'api_key' => 'abc12',
'rules' => [
{ 'field' => 'start_time', 'operator' => 'equals', 'val' => '09:00' }
]
],
'alarms' => {
'clear_existing' => true,
'triggers'=> [ '-P1DT0H0M0S', '-P1DT1H1M2S' ]
}
}
}
end
Expand Down