@@ -37,6 +37,16 @@ module Agents |
||
| 37 | 37 |
event_created_within?(interpolated[:expected_update_period_in_days]) && !received_event_without_error? |
| 38 | 38 |
end |
| 39 | 39 |
|
| 40 |
+ def check |
|
| 41 |
+ api = DropboxAPI.new(interpolated[:access_token]) |
|
| 42 |
+ current_contents = api.dir(interpolated[:dir_to_watch]) |
|
| 43 |
+ |
|
| 44 |
+ diff = DropboxDirDiff.new(previous_contents, current_contents) |
|
| 45 |
+ create_event(payload: diff.to_hash) unless previous_contents.nil? || diff.empty? |
|
| 46 |
+ |
|
| 47 |
+ remember(current_contents) |
|
| 48 |
+ end |
|
| 49 |
+ |
|
| 40 | 50 |
private |
| 41 | 51 |
|
| 42 | 52 |
def is_positive_integer?(value) |
@@ -45,5 +55,26 @@ module Agents |
||
| 45 | 55 |
false |
| 46 | 56 |
end |
| 47 | 57 |
|
| 58 |
+ def previous_contents |
|
| 59 |
+ self.memory['contents'] |
|
| 60 |
+ end |
|
| 61 |
+ |
|
| 62 |
+ def remember(contents) |
|
| 63 |
+ self.memory['contents'] = contents |
|
| 64 |
+ end |
|
| 65 |
+ |
|
| 66 |
+ # == Auxiliary classes == |
|
| 67 |
+ |
|
| 68 |
+ class DropboxAPI |
|
| 69 |
+ def initialize(access_token) |
|
| 70 |
+ end |
|
| 71 |
+ end |
|
| 72 |
+ |
|
| 73 |
+ class DropboxDirDiff |
|
| 74 |
+ def initialize(previous, current) |
|
| 75 |
+ end |
|
| 76 |
+ end |
|
| 77 |
+ |
|
| 48 | 78 |
end |
| 79 |
+ |
|
| 49 | 80 |
end |
@@ -11,6 +11,7 @@ describe Agents::DropboxWatchAgent do |
||
| 11 | 11 |
} |
| 12 | 12 |
) |
| 13 | 13 |
@agent.user = users(:bob) |
| 14 |
+ @agent.save! |
|
| 14 | 15 |
end |
| 15 | 16 |
|
| 16 | 17 |
it 'cannot receive events' do |
@@ -26,7 +27,7 @@ describe Agents::DropboxWatchAgent do |
||
| 26 | 27 |
end |
| 27 | 28 |
|
| 28 | 29 |
describe '#valid?' do |
| 29 |
- before { expect(@agent.valid?).to eq true }
|
|
| 30 |
+ before(:each) { expect(@agent.valid?).to eq true }
|
|
| 30 | 31 |
|
| 31 | 32 |
it 'requires the "access_token"' do |
| 32 | 33 |
@agent.options[:access_token] = nil |
@@ -51,4 +52,62 @@ describe Agents::DropboxWatchAgent do |
||
| 51 | 52 |
end |
| 52 | 53 |
end |
| 53 | 54 |
|
| 55 |
+ describe '#check' do |
|
| 56 |
+ |
|
| 57 |
+ let(:first_result) { 'first_result' }
|
|
| 58 |
+ |
|
| 59 |
+ before(:each) do |
|
| 60 |
+ stub.proxy(Agents::DropboxWatchAgent::DropboxAPI).new('70k3n') do |api|
|
|
| 61 |
+ stub(api).dir('/my/dropbox/dir') { first_result }
|
|
| 62 |
+ end |
|
| 63 |
+ end |
|
| 64 |
+ |
|
| 65 |
+ it 'saves the directory listing in its memory' do |
|
| 66 |
+ @agent.check |
|
| 67 |
+ expect(@agent.memory).to eq 'contents' => first_result |
|
| 68 |
+ end |
|
| 69 |
+ |
|
| 70 |
+ context 'first time' do |
|
| 71 |
+ |
|
| 72 |
+ before(:each) { @agent.memory = {} }
|
|
| 73 |
+ |
|
| 74 |
+ it 'does not send any events' do |
|
| 75 |
+ expect { @agent.check }.to_not change(Event, :count)
|
|
| 76 |
+ end |
|
| 77 |
+ |
|
| 78 |
+ end |
|
| 79 |
+ |
|
| 80 |
+ context 'subsequent calls' do |
|
| 81 |
+ |
|
| 82 |
+ let(:second_result) { 'second_result' }
|
|
| 83 |
+ |
|
| 84 |
+ before(:each) do |
|
| 85 |
+ @agent.memory = { 'contents' => 'not_empty' }
|
|
| 86 |
+ |
|
| 87 |
+ stub.proxy(Agents::DropboxWatchAgent::DropboxAPI).new('70k3n') do |api|
|
|
| 88 |
+ stub(api).dir('/my/dropbox/dir') { second_result }
|
|
| 89 |
+ end |
|
| 90 |
+ end |
|
| 91 |
+ |
|
| 92 |
+ it 'sends an event upon a different directory listing' do |
|
| 93 |
+ payload = { 'diff' => 'object as hash' }
|
|
| 94 |
+ stub.proxy(Agents::DropboxWatchAgent::DropboxDirDiff).new(@agent.memory['contents'], second_result) do |diff| |
|
| 95 |
+ stub(diff).empty? { false }
|
|
| 96 |
+ stub(diff).to_hash { payload }
|
|
| 97 |
+ end |
|
| 98 |
+ expect { @agent.check }.to change(Event, :count).by(1)
|
|
| 99 |
+ expect(Event.last.payload).to eq(payload) |
|
| 100 |
+ end |
|
| 101 |
+ |
|
| 102 |
+ it 'does not sent any events when there is no difference on the directory listing' do |
|
| 103 |
+ stub.proxy(Agents::DropboxWatchAgent::DropboxDirDiff).new(@agent.memory['contents'], second_result) do |diff| |
|
| 104 |
+ stub(diff).empty? { true }
|
|
| 105 |
+ end |
|
| 106 |
+ |
|
| 107 |
+ expect { @agent.check }.to_not change(Event, :count)
|
|
| 108 |
+ end |
|
| 109 |
+ |
|
| 110 |
+ end |
|
| 111 |
+ end |
|
| 112 |
+ |
|
| 54 | 113 |
end |