huginn_time_filter_agent 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +7 -0
- data/lib/huginn_time_filter_agent.rb +4 -0
- data/lib/huginn_time_filter_agent/time_filter_agent.rb +59 -0
- data/spec/time_filter_agent_spec.rb +97 -0
- metadata +90 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 92342e4bdc63cda2208359dbb7d013fb8e0eec29c6ad9893cdcef616d113ed5f
|
4
|
+
data.tar.gz: cafc671f5c71152f00fda4cbcb80563b8d092b6e496c2fed5d71e5625769cfb2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3518a73a74d3ddab49eb7b6dc4d8aa30b1e63a23710a4cf45d765b260f36318088a13cff49cee00c06dfa3a5b91745299abd5bfc0698124951e9249e122bebad
|
7
|
+
data.tar.gz: 6138d9cc2662c129e7f6e2157de1739703614f6fedc5860cc107b50bf5f83645ad9f3bac30a3d9c669e3f2eea25e9c4ea3fee07315548ae4ac5d94b67e884fb9
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2019 Adrian Schoenig
|
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,59 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
module Agents
|
4
|
+
class TimeFilterAgent < Agent
|
5
|
+
cannot_be_scheduled!
|
6
|
+
|
7
|
+
description <<-MD
|
8
|
+
The Time Filter Agent filters events based on whether a 24h or AM/PM time in them is within a provided range.
|
9
|
+
|
10
|
+
`time_path` should be set to where in the event's payload the time information is, default is `time`
|
11
|
+
|
12
|
+
`earliest` should be set to earliest allowed time, default is `08:00`
|
13
|
+
|
14
|
+
`latest` should be set to the latest allowed time, default is `20:00`
|
15
|
+
MD
|
16
|
+
|
17
|
+
def default_options
|
18
|
+
{
|
19
|
+
"earliest": "08:00",
|
20
|
+
"latest": "20:00",
|
21
|
+
"time_path": "time"
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
def validate_options
|
26
|
+
errors.add(:base, "time_path needs to be present") if options['time_path'].blank?
|
27
|
+
errors.add(:base, "earliest and/or latest needs to be present") if options['earliest'].blank? or options['latest'].blank?
|
28
|
+
end
|
29
|
+
|
30
|
+
def working?
|
31
|
+
checked_without_error?
|
32
|
+
end
|
33
|
+
|
34
|
+
def receive(incoming_events)
|
35
|
+
incoming_events.each do |event|
|
36
|
+
event_time = get_time(event.payload[options['time_path']])
|
37
|
+
return if event_time.nil?
|
38
|
+
|
39
|
+
earliest_time = get_time(options['earliest'])
|
40
|
+
latest_time = get_time(options['latest'])
|
41
|
+
return unless earliest_time.nil? or event_time >= earliest_time
|
42
|
+
return unless latest_time.nil? or event_time <= latest_time
|
43
|
+
|
44
|
+
create_event payload: event.payload
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def get_time(string)
|
51
|
+
return if string.blank?
|
52
|
+
begin
|
53
|
+
Time.parse(string)
|
54
|
+
rescue
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
require 'huginn_agent/spec_helper'
|
3
|
+
|
4
|
+
describe Agents::TimeFilterAgent do
|
5
|
+
before(:each) do
|
6
|
+
@valid_options = Agents::TimeFilterAgent.new.default_options
|
7
|
+
@checker = Agents::TimeFilterAgent.new(:name => "TimeFilterAgent", :options => @valid_options)
|
8
|
+
@checker.user = users(:bob)
|
9
|
+
@checker.save!
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'renders the event description without errors' do
|
13
|
+
expect { @checker.event_description }.not_to raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
context '#validate_options' do
|
17
|
+
it 'is valid with the default options' do
|
18
|
+
expect(@checker).to be_valid
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'requires either time to be set' do
|
22
|
+
@checker.options['earliest'] = ""
|
23
|
+
@checker.options['latest'] = ""
|
24
|
+
expect(@checker).not_to be_valid
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'requires time_path to be set' do
|
28
|
+
@checker.options['time_path'] = ""
|
29
|
+
expect(@checker).not_to be_valid
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context '#receive' do
|
34
|
+
it "accepts an AM/PM time in timeframe" do
|
35
|
+
event = Event.new(payload: {"time" => "5:30PM"})
|
36
|
+
|
37
|
+
expect { @checker.receive([event]) }.to change(Event, :count).by(1)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "accepts an AM/PM time in timeframe for alternative path" do
|
41
|
+
event = Event.new(payload: {"my_time" => "5:30PM"})
|
42
|
+
@checker.options['time_path'] = "my_time"
|
43
|
+
|
44
|
+
expect { @checker.receive([event]) }.to change(Event, :count).by(1)
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
it "rejects an AM/PM time after timeframe" do
|
49
|
+
event = Event.new(payload: {"time" => "11:30PM"})
|
50
|
+
|
51
|
+
expect { @checker.receive([event]) }.to change(Event, :count).by(0)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "rejects an AM/PM time before timeframe" do
|
55
|
+
event = Event.new(payload: {"time" => "7:30AM"})
|
56
|
+
|
57
|
+
expect { @checker.receive([event]) }.to change(Event, :count).by(0)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "accepts a 24h time in timeframe" do
|
61
|
+
event = Event.new(payload: {"time" => "20:00"})
|
62
|
+
|
63
|
+
expect { @checker.receive([event]) }.to change(Event, :count).by(1)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "accepts a 24h time with seconds in timeframe" do
|
67
|
+
event = Event.new(payload: {"time" => "8:00:00"})
|
68
|
+
|
69
|
+
expect { @checker.receive([event]) }.to change(Event, :count).by(1)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "rejects a 24h time after timeframe" do
|
73
|
+
event = Event.new(payload: {"time" => "23:30"})
|
74
|
+
|
75
|
+
expect { @checker.receive([event]) }.to change(Event, :count).by(0)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "rejects a 24h time before timeframe" do
|
79
|
+
event = Event.new(payload: {"time" => "3:30"})
|
80
|
+
|
81
|
+
expect { @checker.receive([event]) }.to change(Event, :count).by(0)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "rejects an event without a time" do
|
85
|
+
event = Event.new(payload: {"no_time" => "for you"})
|
86
|
+
|
87
|
+
expect { @checker.receive([event]) }.to change(Event, :count).by(0)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "rejects an event without a bad time" do
|
91
|
+
event = Event.new(payload: {"time" => "not a time"})
|
92
|
+
|
93
|
+
expect { @checker.receive([event]) }.to change(Event, :count).by(0)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: huginn_time_filter_agent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adrian Schoenig
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-02-09 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: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
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:
|
56
|
+
email:
|
57
|
+
- adrian.schoenig@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- LICENSE.txt
|
63
|
+
- lib/huginn_time_filter_agent.rb
|
64
|
+
- lib/huginn_time_filter_agent/time_filter_agent.rb
|
65
|
+
- spec/time_filter_agent_spec.rb
|
66
|
+
homepage: https://github.com/nighthawk/huginn_time_filter_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.0.2
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: Huginn agent that filters events based on their time information
|
89
|
+
test_files:
|
90
|
+
- spec/time_filter_agent_spec.rb
|