@@ -72,6 +72,36 @@ module Agents |
||
72 | 72 |
|
73 | 73 |
class DropboxDirDiff |
74 | 74 |
def initialize(previous, current) |
75 |
+ @previous, @current = [previous, current] |
|
76 |
+ end |
|
77 |
+ |
|
78 |
+ def empty? |
|
79 |
+ (@previous == @current) |
|
80 |
+ end |
|
81 |
+ |
|
82 |
+ def to_hash |
|
83 |
+ calculate_diff |
|
84 |
+ { added: @added, removed: @removed, updated: @updated } |
|
85 |
+ end |
|
86 |
+ |
|
87 |
+ private |
|
88 |
+ |
|
89 |
+ def calculate_diff |
|
90 |
+ @updated = @current.select do |current_entry| |
|
91 |
+ previous_entry = find_by_path(@previous, current_entry[:path]) |
|
92 |
+ (current_entry != previous_entry) && !previous_entry.nil? |
|
93 |
+ end |
|
94 |
+ |
|
95 |
+ updated_entries = @updated + @previous.select do |previous_entry| |
|
96 |
+ find_by_path(@updated, previous_entry[:path]) |
|
97 |
+ end |
|
98 |
+ |
|
99 |
+ @added = @current - @previous - updated_entries |
|
100 |
+ @removed = @previous - @current - updated_entries |
|
101 |
+ end |
|
102 |
+ |
|
103 |
+ def find_by_path(array, path) |
|
104 |
+ array.find { |entry| entry[:path] == path } |
|
75 | 105 |
end |
76 | 106 |
end |
77 | 107 |
|
@@ -110,4 +110,50 @@ describe Agents::DropboxWatchAgent do |
||
110 | 110 |
end |
111 | 111 |
end |
112 | 112 |
|
113 |
+ describe Agents::DropboxWatchAgent::DropboxDirDiff do |
|
114 |
+ |
|
115 |
+ let(:previous) { [ |
|
116 |
+ { path: '1.json', rev: '1' }, |
|
117 |
+ { path: '2.json', rev: '1' }, |
|
118 |
+ { path: '3.json', rev: '1' } |
|
119 |
+ ] } |
|
120 |
+ |
|
121 |
+ let(:current) { [ |
|
122 |
+ { path: '1.json', rev: '2' }, |
|
123 |
+ { path: '3.json', rev: '1' }, |
|
124 |
+ { path: '4.json', rev: '1' } |
|
125 |
+ ] } |
|
126 |
+ |
|
127 |
+ describe '#empty?' do |
|
128 |
+ |
|
129 |
+ it 'is true when no differences are detected' do |
|
130 |
+ diff = Agents::DropboxWatchAgent::DropboxDirDiff.new(previous, previous) |
|
131 |
+ expect(diff.empty?).to eq true |
|
132 |
+ end |
|
133 |
+ |
|
134 |
+ it 'is false when differences were detected' do |
|
135 |
+ diff = Agents::DropboxWatchAgent::DropboxDirDiff.new(previous, current) |
|
136 |
+ expect(diff.empty?).to eq false |
|
137 |
+ end |
|
138 |
+ |
|
139 |
+ end |
|
140 |
+ |
|
141 |
+ describe '#to_hash' do |
|
142 |
+ |
|
143 |
+ subject(:diff_hash) { Agents::DropboxWatchAgent::DropboxDirDiff.new(previous, current).to_hash } |
|
144 |
+ |
|
145 |
+ it 'detects additions' do |
|
146 |
+ expect(diff_hash[:added]).to eq [{ path: '4.json', rev: '1' }] |
|
147 |
+ end |
|
148 |
+ |
|
149 |
+ it 'detects removals' do |
|
150 |
+ expect(diff_hash[:removed]).to eq [ { path: '2.json', rev: '1' } ] |
|
151 |
+ end |
|
152 |
+ |
|
153 |
+ it 'detects updates' do |
|
154 |
+ expect(diff_hash[:updated]).to eq [ { path: '1.json', rev: '2' } ] |
|
155 |
+ end |
|
156 |
+ |
|
157 |
+ end |
|
158 |
+ end |
|
113 | 159 |
end |