From a4aea43e25a76f03c0801cd15b4b35b4cd8f5557 Mon Sep 17 00:00:00 2001 From: Tam + Dung Date: Tue, 19 Jun 2012 16:53:53 +0700 Subject: [PATCH] [f] Rewrite some stubs methods for dropbox_sdk: - put_file, get_file, file_delete, metadata --- lib/dummy_dropbox_sdk.rb | 122 ++++++++++++++++++++++++++++----------- 1 file changed, 89 insertions(+), 33 deletions(-) diff --git a/lib/dummy_dropbox_sdk.rb b/lib/dummy_dropbox_sdk.rb index 3ea8a5c..4dc69bf 100644 --- a/lib/dummy_dropbox_sdk.rb +++ b/lib/dummy_dropbox_sdk.rb @@ -5,14 +5,30 @@ require 'dropbox_sdk' end require 'ostruct' +require 'mime/types' + +GIGA_SIZE = 1073741824.0 +MEGA_SIZE = 1048576.0 +KILO_SIZE = 1024.0 + +# Return the file size with a readable style. +def readable_file_size(size, precision) + case + when size == 1 : "1 Byte" + when size < KILO_SIZE : "%d Bytes" % size + when size < MEGA_SIZE : "%.#{precision}f KB" % (size / KILO_SIZE) + when size < GIGA_SIZE : "%.#{precision}f MB" % (size / MEGA_SIZE) + else "%.#{precision}f GB" % (size / GIGA_SIZE) + end +end module DummyDropbox @@root_path = File.expand_path( "./test/fixtures/dropbox" ) - + def self.root_path=(path) @@root_path = path end - + def self.root_path @@root_path end @@ -28,7 +44,7 @@ def initialize(oauth_key, oauth_secret, options={}) def self.deserialize(data) return DropboxSession.new( 'dummy_key', 'dummy_secret' ) end - + def get_authorize_url(*args) return 'https://www.dropbox.com/0/oauth/authorize' end @@ -36,7 +52,7 @@ def get_authorize_url(*args) def serialize return 'dummy serial'.to_yaml end - + def authorized? return true end @@ -44,7 +60,7 @@ def authorized? def dummy? return true end - + end class DropboxClient @@ -64,25 +80,49 @@ def file_create_folder(path, options={}) end def metadata(path, options={}) - response = <<-RESPONSE + dummy_path = "#{DummyDropbox::root_path}/#{path}" + raise DropboxError.new("File not found") unless File.exists?(dummy_path) + + list_hash_files = [] + if File.directory?(dummy_path) + Dir.entries(dummy_path).each do |file_name| + file_path = "#{dummy_path}/#{file_name}" + unless File.directory?(file_path) + list_hash_files << {"size" => readable_file_size(File.size(file_path), 2), + "bytes" => File.size(file_path), + "is_dir" => false, + "modified" => File.mtime(file_path), + "mime_type" => MIME::Types.type_for(file_path)[0].content_type, + "path" => "#{path}/#{file_name}"} + end + end + + end + + response = { - "thumb_exists": false, - "bytes": "#{File.size( "#{DummyDropbox::root_path}/#{path}" )}", - "modified": "Tue, 04 Nov 2008 02:52:28 +0000", - "path": "#{path}", - "is_dir": #{File.directory?( "#{DummyDropbox::root_path}/#{path}" )}, - "size": "566.0KB", - "root": "dropbox", - "icon": "page_white_acrobat", - "hash": "theHash" + "thumb_exists" => false, + "bytes" => File.size(dummy_path), + "modified" => "Tue, 04 Nov 2008 02:52:28 +0000", + "path" => path, + "is_dir" => File.directory?( "#{DummyDropbox::root_path}/#{path}" ), + "size" => readable_file_size(File.size(dummy_path), 2), + "root" => "dropbox", + "icon" => "page_white_acrobat", + "hash" => "theHash", + "contents" => list_hash_files } - RESPONSE - return JSON.parse(response) + + return response end def put_file(to_path, file_obj, overwrite=false, parent_rev=nil) - File.join(DummyDropbox::root_path, to_path) - FileUtils.copy_file(file_obj.path, File.join(DummyDropbox::root_path, to_path)) + file_path = File.join(DummyDropbox::root_path, to_path) + # FileUtils.copy_file(file_obj.path, File.join(DummyDropbox::root_path, to_path)) + File.open(file_path, "w") do |f| + f.write(file_obj) + end + return self.metadata(to_path) end @@ -92,7 +132,24 @@ def account_info() 'email' => 'dummy_dropbox@example.com' } end - + + def get_file(path) + dummy_file_path = File.join(DummyDropbox::root_path, path) + raise DropboxError.new("File not found") unless File.exists?(dummy_file_path) + + File.read(dummy_file_path) + end + + def file_delete(path) + dummy_file_path = File.join(DummyDropbox::root_path, path) + raise DropboxError.new("File not found") unless File.exists?(dummy_file_path) + + metadata = self.metadata(path) + FileUtils.rm_rf(dummy_file_path) + + return metadata + end + # TODO these methods I don't used yet. They are commented out because they # have to be checked against the api if the signature match @@ -102,21 +159,21 @@ def account_info() # # def delete(path, options={}) # FileUtils.rm_rf( "#{Dropbox.files_root_path}/#{path}" ) - # + # # return true # end - # + # # def upload(local_file_path, remote_folder_path, options={}) # end - # - # + # + # # def list(path, options={}) # result = [] - # + # # Dir["#{Dropbox.files_root_path}/#{path}/**"].each do |element_path| # element_path.gsub!( "#{Dropbox.files_root_path}/", '' ) - # - # element = + # + # element = # OpenStruct.new( # :icon => 'folder', # :'directory?' => File.directory?( "#{Dropbox.files_root_path}/#{element_path}" ), @@ -128,13 +185,13 @@ def account_info() # :is_dir => File.directory?( "#{Dropbox.files_root_path}/#{element_path}" ), # :size => '0 bytes' # ) - # + # # result << element # end - # + # # return result # end - # + # # def account # response = <<-RESPONSE # { @@ -149,9 +206,8 @@ def account_info() # "uid": "174" # } # RESPONSE - # + # # return JSON.parse(response).symbolize_keys_recursively.to_struct_recursively # end - -end +end