Class: DribbbleBucketSync::Folder

Inherits:
Object
  • Object
show all
Defined in:
lib/dribbble_bucket_sync/folder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory, name) ⇒ Folder

Returns a new instance of Folder.



10
11
12
13
14
15
16
17
18
# File 'lib/dribbble_bucket_sync/folder.rb', line 10

def initialize(directory, name)
	name = sanitize(name)
	# store the path
	@path = File.join(directory, name)
	# ensure our folder exists
	ensure_folder_exists
	# create storage for the shots
	@shots = []
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/dribbble_bucket_sync/folder.rb', line 8

def path
  @path
end

#shotsObject (readonly)

Returns the value of attribute shots.



8
9
10
# File 'lib/dribbble_bucket_sync/folder.rb', line 8

def shots
  @shots
end

Instance Method Details

#add_shot(shot) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/dribbble_bucket_sync/folder.rb', line 20

def add_shot(shot)
	# calculate the filename
	filename = "#{shot.id}.#{shot.ext}"
	# calculate the path
	path = File.join(@path, filename)
	# add the shot to the storage
	@shots << { filename: filename, destination: path, url: shot.image_url }
end

#current_shot_filesObject

returns an array of existing shot files



56
57
58
# File 'lib/dribbble_bucket_sync/folder.rb', line 56

def current_shot_files
	Dir.glob(File.join(@path, "*.{jpg,jpeg,png,gif}"))
end

#download_new_shotsObject

downloads any shots missing from the folder



35
36
37
38
39
40
41
42
# File 'lib/dribbble_bucket_sync/folder.rb', line 35

def download_new_shots
	# select all the shots that don't exist
	new_shots = @shots.select do |s|
		!File.exist?(s[:destination])
	end
	# download them all
	download_files(new_shots)
end

#remove_old_shotsObject

deletes any shots in the folder that no longer exist in the bucket



45
46
47
48
49
50
51
52
53
# File 'lib/dribbble_bucket_sync/folder.rb', line 45

def remove_old_shots
	# loop through all shots in the folder
	current_shot_files.each do |file|
		# delete file if the shot isn't in the array
		unless @shots.map { |s| s[:filename] }.include?(File.basename(file))
			FileUtils.rm(file)
		end
	end
end

#syncObject



29
30
31
32
# File 'lib/dribbble_bucket_sync/folder.rb', line 29

def sync
	download_new_shots
	remove_old_shots
end