Deal with JSON vs. Hash - String vs. Symbol.

Guilherme J. Tramontina 10 anos atrás
pai
commit
2030227a49

+ 5 - 5
app/models/agents/dropbox_watch_agent.rb

@@ -78,13 +78,13 @@ module Agents
78 78
         options = @options.deep_merge({ query: { list: true } })
79 79
         response = self.class.get("/metadata/auto#{to_watch}", options)
80 80
         raise ResourceNotFound.new(to_watch) if response.not_found?
81
-        JSON.parse(response)['contents'].map { |entry| slice_json(entry, :path, :rev, :modified) }
81
+        JSON.parse(response)['contents'].map { |entry| slice_json(entry, 'path', 'rev', 'modified') }
82 82
       end
83 83
 
84 84
       private
85 85
 
86 86
       def slice_json(json, *keys)
87
-        keys.each_with_object({}){|key, hash| hash[key] = json[key.to_s]}
87
+        keys.each_with_object({}){|key, hash| hash[key.to_s] = json[key.to_s]}
88 88
       end
89 89
     end
90 90
 
@@ -106,12 +106,12 @@ module Agents
106 106
 
107 107
       def calculate_diff
108 108
         @updated = @current.select do |current_entry|
109
-          previous_entry = find_by_path(@previous, current_entry[:path])
109
+          previous_entry = find_by_path(@previous, current_entry['path'])
110 110
           (current_entry != previous_entry) && !previous_entry.nil?
111 111
         end
112 112
 
113 113
         updated_entries = @updated + @previous.select do |previous_entry|
114
-          find_by_path(@updated, previous_entry[:path])
114
+          find_by_path(@updated, previous_entry['path'])
115 115
         end
116 116
 
117 117
         @added = @current - @previous - updated_entries
@@ -119,7 +119,7 @@ module Agents
119 119
       end
120 120
 
121 121
       def find_by_path(array, path)
122
-        array.find { |entry| entry[:path] == path }
122
+        array.find { |entry| entry['path'] == path }
123 123
       end
124 124
     end
125 125
 

+ 11 - 11
spec/models/agents/dropbox_watch_agent_spec.rb

@@ -113,15 +113,15 @@ describe Agents::DropboxWatchAgent do
113 113
   describe Agents::DropboxWatchAgent::DropboxDirDiff do
114 114
 
115 115
     let(:previous) { [
116
-      { path: '1.json', rev: '1' },
117
-      { path: '2.json', rev: '1' },
118
-      { path: '3.json', rev: '1' }
116
+      { 'path' => '1.json', 'rev' => '1' },
117
+      { 'path' => '2.json', 'rev' => '1' },
118
+      { 'path' => '3.json', 'rev' => '1' }
119 119
     ] }
120 120
 
121 121
     let(:current) { [
122
-      { path: '1.json', rev: '2' },
123
-      { path: '3.json', rev: '1' },
124
-      { path: '4.json', rev: '1' }
122
+      { 'path' => '1.json', 'rev' => '2' },
123
+      { 'path' => '3.json', 'rev' => '1' },
124
+      { 'path' => '4.json', 'rev' => '1' }
125 125
     ] }
126 126
 
127 127
     describe '#empty?' do
@@ -143,15 +143,15 @@ describe Agents::DropboxWatchAgent do
143 143
       subject(:diff_hash) { Agents::DropboxWatchAgent::DropboxDirDiff.new(previous, current).to_hash }
144 144
 
145 145
       it 'detects additions' do
146
-        expect(diff_hash[:added]).to eq [{ path: '4.json', rev: '1' }]
146
+        expect(diff_hash[:added]).to eq [{ 'path' => '4.json', 'rev' => '1' }]
147 147
       end
148 148
 
149 149
       it 'detects removals' do
150
-        expect(diff_hash[:removed]).to eq [ { path: '2.json', rev: '1' } ]
150
+        expect(diff_hash[:removed]).to eq [ { 'path' => '2.json', 'rev' => '1' } ]
151 151
       end
152 152
 
153 153
       it 'detects updates' do
154
-        expect(diff_hash[:updated]).to eq [ { path: '1.json', rev: '2' } ]
154
+        expect(diff_hash[:updated]).to eq [ { 'path' => '1.json', 'rev' => '2' } ]
155 155
       end
156 156
 
157 157
       context 'when the previous value is not defined' do
@@ -219,8 +219,8 @@ describe Agents::DropboxWatchAgent do
219 219
         it 'trims down the attributes of the response to our needs' do
220 220
           dir_list = Agents::DropboxWatchAgent::DropboxAPI.new(access_token).dir(dir_to_watch)
221 221
           expect(dir_list).to eq [
222
-            { path: "#{dir_to_watch}/1.json", rev: '1', modified: 'Mon, 11 Mar 2013 15:41:44 +0000' },
223
-            { path: "#{dir_to_watch}/2.json", rev: '4', modified: 'Mon, 12 Mar 2013 15:41:44 +0000' }
222
+            { 'path' => "#{dir_to_watch}/1.json", 'rev' => '1', 'modified' => 'Mon, 11 Mar 2013 15:41:44 +0000' },
223
+            { 'path' => "#{dir_to_watch}/2.json", 'rev' => '4', 'modified' => 'Mon, 12 Mar 2013 15:41:44 +0000' }
224 224
           ]
225 225
         end
226 226
       end