Get the new Dropbox Watch Agent off the ground.

Guilherme J. Tramontina 10 ans auparavant
Parent
Commettre
32b5650ece
2 fichiers modifiés avec 103 ajouts et 0 suppressions
  1. 49 0
      app/models/agents/dropbox_watch_agent.rb
  2. 54 0
      spec/models/agents/dropbox_watch_agent_spec.rb

+ 49 - 0
app/models/agents/dropbox_watch_agent.rb

@@ -0,0 +1,49 @@
1
+module Agents
2
+  class DropboxWatchAgent < Agent
3
+    cannot_receive_events!
4
+    default_schedule "every_1m"
5
+
6
+    description <<-MD
7
+      The _DropboxWatchAgent_ watches the given `dir_to_watch` and emits events with the detected changes.
8
+      It requires a [Dropbox App](https://www.dropbox.com/developers/apps) and its `access_token`, which will be used to authenticate on your account.
9
+    MD
10
+
11
+    event_description <<-MD
12
+      The event payload will contain the following fields, when applicable:
13
+      ```
14
+      {
15
+        "added": [ "path/to/new/file" ],
16
+        "removed": [ "path/to/removed/file" ],
17
+        "updated": [ "path/to/updated/file" ]
18
+      }
19
+      ```
20
+    MD
21
+
22
+    def default_options
23
+      {
24
+        access_token: 'your_dropbox_app_access_token',
25
+        dir_to_watch: '/',
26
+        expected_update_period_in_days: 1
27
+      }
28
+    end
29
+
30
+    def validate_options
31
+      errors.add(:base, 'The `access_token` property is required.') unless options['access_token'].present?
32
+      errors.add(:base, 'The `dir_to_watch` property is required.') unless options['dir_to_watch'].present?
33
+      errors.add(:base, 'Invalid `expected_update_period_in_days` format.') unless options['expected_update_period_in_days'].present? && is_positive_integer?(options['expected_update_period_in_days'])
34
+    end
35
+
36
+    def working?
37
+      event_created_within?(interpolated[:expected_update_period_in_days]) && !received_event_without_error?
38
+    end
39
+
40
+    private
41
+
42
+    def is_positive_integer?(value)
43
+      Integer(value) >= 0
44
+    rescue
45
+      false
46
+    end
47
+
48
+  end
49
+end

+ 54 - 0
spec/models/agents/dropbox_watch_agent_spec.rb

@@ -0,0 +1,54 @@
1
+require 'spec_helper'
2
+
3
+describe Agents::DropboxWatchAgent do
4
+  before(:each) do
5
+    @agent = Agents::DropboxWatchAgent.new(
6
+      name: 'save to dropbox',
7
+      options: {
8
+        access_token: '70k3n',
9
+        dir_to_watch: '/my/dropbox/dir',
10
+        expected_update_period_in_days: 2
11
+      }
12
+    )
13
+    @agent.user = users(:bob)
14
+  end
15
+
16
+  it 'cannot receive events' do
17
+    expect(@agent.cannot_receive_events?).to eq true
18
+  end
19
+
20
+  it 'has agent description' do
21
+    expect(@agent.description).to_not be_nil
22
+  end
23
+
24
+  it 'has event description' do
25
+    expect(@agent.event_description).to_not be_nil
26
+  end
27
+
28
+  describe '#valid?' do
29
+    before { expect(@agent.valid?).to eq true }
30
+
31
+    it 'requires the "access_token"' do
32
+      @agent.options[:access_token] = nil
33
+      expect(@agent.valid?).to eq false
34
+    end
35
+
36
+    it 'requires a "dir_to_watch"' do
37
+      @agent.options[:dir_to_watch] = nil
38
+      expect(@agent.valid?).to eq false
39
+    end
40
+
41
+    describe 'expected_update_period_in_days' do
42
+      it 'needs to be present' do
43
+        @agent.options[:expected_update_period_in_days] = nil
44
+        expect(@agent.valid?).to eq false
45
+      end
46
+
47
+      it 'needs to be a positive integer' do
48
+        @agent.options[:expected_update_period_in_days] = -1
49
+        expect(@agent.valid?).to eq false
50
+      end
51
+    end
52
+  end
53
+
54
+end