-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[f] Rewrite some stubs methods for dropbox_sdk:
- put_file, get_file, file_delete, metadata
- Loading branch information
Tam + Dung
committed
Jun 19, 2012
1 parent
eff9877
commit a4aea43
Showing
1 changed file
with
89 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,23 +44,23 @@ 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 | ||
|
||
def serialize | ||
return 'dummy serial'.to_yaml | ||
end | ||
|
||
def authorized? | ||
return true | ||
end | ||
|
||
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' => '[email protected]' | ||
} | ||
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 |