huginn_todoist_agent 0.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5fa7c7d07478fe20d4e6615c5dc1888717fbea36
4
- data.tar.gz: 85253fc515dcf5b317956a39e56ee5a3016db05f
3
+ metadata.gz: ceaded2f438e7776737271ddd74d420635c51f6a
4
+ data.tar.gz: 65cc2cc94fffbe3b4295a63ade0b58e66b78bf10
5
5
  SHA512:
6
- metadata.gz: df7be486f40ee95d74a4db03a8c98cc8a8b762a6d20f624aa28b07fe1d0a58192300e26cf61b14cab93862b3ed8c19eb06b3b74fa47dd4e98c6e5c067e7a3f87
7
- data.tar.gz: 7bb8c657d62a72379b4c6034cca488482260466c3cf146024df4bf116087ba2f5a7f88e7ede6da20ff08c231692550102c0d5bd0c46e3e71583f28f383a26b97
6
+ metadata.gz: 6cbe9da9d7d30f012138d3d9dc934e0a16219dce2249443b64c6fd4f98b9ad09d38479894f66ae02ebbf46a89bac6238a8718af832715b934d3de5dd27c9bdcf
7
+ data.tar.gz: 9bc0e9eb66217d6b4566e5246df7694c51dde0e6800b643e357b80d903e90031c414e5ce73724f021f53e9bbff6c4d384ec6266e0d4c6f16602824a7efea316d
@@ -4,8 +4,10 @@ require 'huginn_agent'
4
4
  # still starts up successfully, yet shows "Missing Gems" error in the frontend.
5
5
  begin
6
6
  require 'todoist'
7
+ require 'todoist_querynaut'
7
8
  rescue LoadError
8
9
  end
9
10
 
10
11
  #HuginnAgent.load 'huginn_todoist_agent/concerns/my_agent_concern'
11
12
  HuginnAgent.register 'huginn_todoist_agent/todoist_agent'
13
+ HuginnAgent.register 'huginn_todoist_agent/todoist_query_agent'
@@ -0,0 +1,61 @@
1
+ module Agents
2
+ class TodoistQueryAgent < Agent
3
+ include FormConfigurable
4
+
5
+ cannot_receive_events!
6
+
7
+ gem_dependency_check { defined?(Todoist::Client) && defined?(TodoistQuerynaut::Client) }
8
+
9
+ description do
10
+ <<-MD
11
+ The Todoist Query Agent allows to search items on your Todoist.
12
+
13
+ To authenticate you need to either set `api_token` or provide a credential named
14
+ `todoist_api_token` to your Todoist API token. You can find it within the
15
+ Todoist web frontend from "Gear Menu" > Todoist Settings > Account tab.
16
+
17
+ Change `query` to the [Todoist filter expression](https://support.todoist.com/hc/en-us/articles/205248842-Filters)
18
+ you'd like to be carried out.
19
+ MD
20
+ end
21
+
22
+ default_schedule "every_1h"
23
+
24
+ def default_options
25
+ {
26
+ "api_token" => "",
27
+ "query" => "today | overdue",
28
+ }
29
+ end
30
+
31
+ form_configurable :api_token
32
+ form_configurable :query, type: :text
33
+
34
+ def working?
35
+ !recent_error_logs?
36
+ end
37
+
38
+ def validate_options
39
+ errors.add(:base, "you need to specify your Todoist API token or provide a credential named todoist_api_token") unless options["api_token"].present? || credential("todoist_api_token").present?
40
+
41
+ if options["query"].present?
42
+ begin
43
+ TodoistQuerynaut::Parser.parse(options["query"])
44
+ rescue Exception
45
+ errors.add(:base, "query cannot be parsed correctly, check against Todoist filter expression manual")
46
+ end
47
+ else
48
+ errors.add(:base, "query must not be empty")
49
+ end
50
+ end
51
+
52
+ def check
53
+ todoist = Todoist::Client.new(interpolated["api_token"].present? ? interpolated["api_token"] : credential("todoist_api_token"))
54
+ result = TodoistQuerynaut::Client.new(todoist).run(options["query"])
55
+
56
+ result.each do |item|
57
+ create_event payload: item
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ def json_response_raw(fixture_name)
2
+ File.read(File.join(__dir__, "/fixtures/#{fixture_name}.json"))
3
+ end
@@ -0,0 +1,56 @@
1
+ require "rails_helper"
2
+ require "huginn_agent/spec_helper"
3
+ require "uri"
4
+
5
+ require_relative "./spec_helper"
6
+
7
+ describe Agents::TodoistQueryAgent do
8
+ before(:each) do
9
+ @valid_options = {
10
+ "api_token" => "some_token_here",
11
+ "query" => "overdue",
12
+ }
13
+ @checker = Agents::TodoistQueryAgent.new(:name => "TodoistQueryAgent", :options => @valid_options)
14
+ @checker.user = users(:bob)
15
+ @checker.save!
16
+ end
17
+
18
+ describe "#validate_options" do
19
+ before do
20
+ expect(@checker).to be_valid
21
+ end
22
+
23
+ it "should reject an empty api_token" do
24
+ @checker.options["api_token"] = nil
25
+ expect(@checker).not_to be_valid
26
+ end
27
+
28
+ it "should allow a credential (instead of api_token)" do
29
+ @checker.user.user_credentials.create :credential_name => "todoist_api_token", :credential_value => "some_credential_here"
30
+ @checker.options["api_token"] = nil
31
+ expect(@checker).to be_valid
32
+ end
33
+
34
+ it "should reject an empty query" do
35
+ @checker.options["query"] = nil
36
+ expect(@checker).not_to be_valid
37
+ end
38
+
39
+ it "should reject invalid query syntax" do
40
+ @checker.options["query"] = "some_invalid_query"
41
+ expect(@checker).not_to be_valid
42
+ end
43
+ end
44
+
45
+ describe "#check" do
46
+ before :each do
47
+ stub_request(:post, "https://todoist.com/API/v6/query").
48
+ with(:body => {"queries" => "[\"overdue\"]", "token" => "some_token_here"}).
49
+ to_return(:status => 200, :body => json_response_raw("query_overdue"), :headers => {})
50
+ end
51
+
52
+ it "should execute the query and emit events for every item" do
53
+ expect { @checker.check }.to change {Event.count}.by(2)
54
+ end
55
+ end
56
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: huginn_todoist_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Siegl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-02 00:00:00.000000000 Z
11
+ date: 2017-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: todoist_querynaut
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.1'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.1'
69
83
  description: The Todoist Agent will create one item on your Todoist for every Huginn
70
84
  event it receives.
71
85
  email:
@@ -77,7 +91,10 @@ files:
77
91
  - LICENSE.txt
78
92
  - lib/huginn_todoist_agent.rb
79
93
  - lib/huginn_todoist_agent/todoist_agent.rb
94
+ - lib/huginn_todoist_agent/todoist_query_agent.rb
95
+ - spec/spec_helper.rb
80
96
  - spec/todoist_agent_spec.rb
97
+ - spec/todoist_query_agent_spec.rb
81
98
  homepage: https://github.com/stesie/huginn_todoist_agent
82
99
  licenses:
83
100
  - MIT
@@ -104,3 +121,5 @@ specification_version: 4
104
121
  summary: Huginn agent to add items to your Todoist.
105
122
  test_files:
106
123
  - spec/todoist_agent_spec.rb
124
+ - spec/todoist_query_agent_spec.rb
125
+ - spec/spec_helper.rb