lib/filbunke/client.rb in filbunke-0.3.2 vs lib/filbunke/client.rb in filbunke-1.0.0

- old
+ new

@@ -1,39 +1,51 @@ require 'active_support' +require 'active_support/all' rescue nil require 'json' require 'net/http' require 'fileutils' module Filbunke class Client + attr_reader :repository + UPDATES_ACTION = 'updates' FILES_ACTION = 'files' URL_KEY = 'url' FROM_CHECKPOINT_KEY = 'from_checkpoint' HASH_KEY = 'hash' - def initialize(repository) + def initialize(repository, logger, callbacks = []) @repository = repository + @logger = logger + @callbacks = callbacks end - def with_updated_files(last_checkpoint, local_path) + def with_updated_files(last_checkpoint) updates = get_updated_file_list(last_checkpoint) updated_files = updates["files"] || [] updated_files.each do |raw_file| file = File.new(raw_file) if file.url.start_with?("http://") - local_file_path = File.join(local_path, file.path) + local_file_path = ::File.join(repository.local_path, file.path) update_http_file!(file, local_file_path) + @callbacks.each do |callback| + callback.on_update(file) + end yield file else raise "Unsupported protocol for file: #{file.inspect}" end end updates["checkpoint"] || last_checkpoint end + def update_files!(last_checkpoint) + with_updated_files(last_checkpoint) {} + end + def register_updated_file!(path, url, hash = nil) register_http = Net::HTTP.new(@repository.host, @repository.port) register_http.start do |http| register_path = "/#{FILES_ACTION}/#{@repository.name}/#{path}?#{URL_KEY}=#{url}" register_path += "&#{HASH_KEY}=#{hash}" if hash @@ -89,18 +101,19 @@ end end end def write_file!(file_path, contents) - FileUtils.mkdir_p(File.dirname(file_path)) - File.open(file_path, 'w') do |file| + @logger.log("Writing: #{file_path}") + ::FileUtils.mkdir_p(::File.dirname(file_path)) + ::File.open(file_path, 'w') do |file| file.write(contents); file.close end end def delete_file!(file_path) - File.delete(file_path) + ::File.delete(file_path) end end end