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

This commit is contained in:
Guilherme J. Tramontina 2014-10-10 16:16:52 -03:00
parent 70bb48b295
commit 2030227a49
2 changed files with 16 additions and 16 deletions

View file

@ -78,13 +78,13 @@ module Agents
options = @options.deep_merge({ query: { list: true } })
response = self.class.get("/metadata/auto#{to_watch}", options)
raise ResourceNotFound.new(to_watch) if response.not_found?
JSON.parse(response)['contents'].map { |entry| slice_json(entry, :path, :rev, :modified) }
JSON.parse(response)['contents'].map { |entry| slice_json(entry, 'path', 'rev', 'modified') }
end
private
def slice_json(json, *keys)
keys.each_with_object({}){|key, hash| hash[key] = json[key.to_s]}
keys.each_with_object({}){|key, hash| hash[key.to_s] = json[key.to_s]}
end
end
@ -106,12 +106,12 @@ module Agents
def calculate_diff
@updated = @current.select do |current_entry|
previous_entry = find_by_path(@previous, current_entry[:path])
previous_entry = find_by_path(@previous, current_entry['path'])
(current_entry != previous_entry) && !previous_entry.nil?
end
updated_entries = @updated + @previous.select do |previous_entry|
find_by_path(@updated, previous_entry[:path])
find_by_path(@updated, previous_entry['path'])
end
@added = @current - @previous - updated_entries
@ -119,7 +119,7 @@ module Agents
end
def find_by_path(array, path)
array.find { |entry| entry[:path] == path }
array.find { |entry| entry['path'] == path }
end
end

View file

@ -113,15 +113,15 @@ describe Agents::DropboxWatchAgent do
describe Agents::DropboxWatchAgent::DropboxDirDiff do
let(:previous) { [
{ path: '1.json', rev: '1' },
{ path: '2.json', rev: '1' },
{ path: '3.json', rev: '1' }
{ 'path' => '1.json', 'rev' => '1' },
{ 'path' => '2.json', 'rev' => '1' },
{ 'path' => '3.json', 'rev' => '1' }
] }
let(:current) { [
{ path: '1.json', rev: '2' },
{ path: '3.json', rev: '1' },
{ path: '4.json', rev: '1' }
{ 'path' => '1.json', 'rev' => '2' },
{ 'path' => '3.json', 'rev' => '1' },
{ 'path' => '4.json', 'rev' => '1' }
] }
describe '#empty?' do
@ -143,15 +143,15 @@ describe Agents::DropboxWatchAgent do
subject(:diff_hash) { Agents::DropboxWatchAgent::DropboxDirDiff.new(previous, current).to_hash }
it 'detects additions' do
expect(diff_hash[:added]).to eq [{ path: '4.json', rev: '1' }]
expect(diff_hash[:added]).to eq [{ 'path' => '4.json', 'rev' => '1' }]
end
it 'detects removals' do
expect(diff_hash[:removed]).to eq [ { path: '2.json', rev: '1' } ]
expect(diff_hash[:removed]).to eq [ { 'path' => '2.json', 'rev' => '1' } ]
end
it 'detects updates' do
expect(diff_hash[:updated]).to eq [ { path: '1.json', rev: '2' } ]
expect(diff_hash[:updated]).to eq [ { 'path' => '1.json', 'rev' => '2' } ]
end
context 'when the previous value is not defined' do
@ -219,8 +219,8 @@ describe Agents::DropboxWatchAgent do
it 'trims down the attributes of the response to our needs' do
dir_list = Agents::DropboxWatchAgent::DropboxAPI.new(access_token).dir(dir_to_watch)
expect(dir_list).to eq [
{ path: "#{dir_to_watch}/1.json", rev: '1', modified: 'Mon, 11 Mar 2013 15:41:44 +0000' },
{ path: "#{dir_to_watch}/2.json", rev: '4', modified: 'Mon, 12 Mar 2013 15:41:44 +0000' }
{ 'path' => "#{dir_to_watch}/1.json", 'rev' => '1', 'modified' => 'Mon, 11 Mar 2013 15:41:44 +0000' },
{ 'path' => "#{dir_to_watch}/2.json", 'rev' => '4', 'modified' => 'Mon, 12 Mar 2013 15:41:44 +0000' }
]
end
end