lib/rapidshare-ext/api.rb in rapidshare-ext-0.0.2 vs lib/rapidshare-ext/api.rb in rapidshare-ext-0.0.3
- old
+ new
@@ -10,10 +10,12 @@
#
# Creates a folder in a Rapidshare virtual filesystem
#
# api.add_folder("/a/b/c") #=> <Random folder ID from Rapidshare>, 1234 for example
def add_folder(path, params = {})
+ path = path_trim path
+
@tree = folders_hierarchy
i = 1
parent = 0
folder_id = nil
while i <= path.split('/').count do
@@ -34,11 +36,11 @@
folder_id = "#{addrealfolder(add_folder_params)}".to_i
raise "error while creating folder" if parent < 0
@tree[folder_id] = {
:parent => parent,
:name => folder_name,
- :path => (@tree[parent] || {})[:path].to_s + ('/' if @tree[parent]).to_s + folder_name
+ :path => path_canonize((@tree[parent] || {})[:path].to_s + ('/' if @tree[parent]).to_s + folder_name)
}
parent = folder_id
path == base_path + '/' + folder_name
i += 1
next
@@ -84,11 +86,11 @@
# @param [Hash] params
# :to => <destination folder path>, default: "/"
#
# api.move_folder("/a/b/c", :to => "/a")
def move_folder(source_path, params = {})
- dest_path = path_trim(params.delete(:to) || '/')
+ dest_path = (params.delete(:to) || '/')
source_folder_id = folder_id(source_path)
dest_folder_id = folder_id(dest_path)
params = {
:realfolder => source_folder_id,
@@ -97,11 +99,11 @@
moverealfolder params
@tree = folders_hierarchy
@tree[source_folder_id][:parent] = dest_folder_id
- @tree[source_folder_id][:path] = "#{folder_path(dest_folder_id)}/#{@tree[source_folder_id][:name]}"
+ @tree[source_folder_id][:path] = path_canonize "#{folder_path(dest_folder_id)}/#{@tree[source_folder_id][:name]}"
true
end
# Upload file to a specified folder
#
@@ -259,10 +261,11 @@
return slice_tree @tree, :from => from_folder_path
end
end
return @tree if @tree && !force_load # TODO: about slices here (:from parameter)
+ @tree = {}
from_folder_id = folder_id from_folder_path
raise Exception, "Folder #{from_folder_path} could not be found" if from_folder_id.nil?
response = listrealfolders
@@ -320,16 +323,16 @@
result_tree = tree.dup
unless from_folder_path == ''
result_tree.keep_if do |folder_id, data|
- data[:path].start_with? "#{from_folder_path}/"
+ path_trim(data[:path]).start_with? "#{from_folder_path}/"
end
result_tree.each_pair do |folder_id, data|
path = result_tree[folder_id][:path]
- result_tree[folder_id][:path] = path.gsub /#{from_folder_path.gsub /\//, '\/'}\//, ''
+ result_tree[folder_id][:path] = path_canonize path_trim(path.gsub /#{from_folder_path.gsub /\//, '\/'}\//, '')
end
end
result_tree
end
@@ -414,11 +417,12 @@
#
# api.folder_path(123) # -> "foo/bar/baz"
def folder_path(folder_id)
@tree = folders_hierarchy
parent_id = @tree[folder_id][:parent]
- (folder_path(parent_id) if parent_id.nonzero?).to_s + ('/' if parent_id.nonzero?).to_s + @tree[folder_id][:name]
+ path = (folder_path(parent_id) if parent_id.nonzero?).to_s + ('/' if parent_id.nonzero?).to_s + @tree[folder_id][:name]
+ parent_id.zero? ? "/#{path}" : path
end
# Get folder ID by path
#
# api.folder_id("foo/bar/baz") # -> 123
@@ -426,11 +430,11 @@
folder_path = path_trim(folder_path)
return 0 if folder_path.empty?
@tree = folders_hierarchy
index = @tree.find_index do |folder_id, data|
- data[:path] == folder_path
+ path_trim(data[:path]) == path_trim(folder_path)
end
@tree.keys[index] unless index.nil?
end
# Get file info in the following format:
@@ -475,25 +479,31 @@
fields.unshift "id"
fields.each_with_index do |value, index|
response[value.to_sym] = resp[index]
end
+ response[:url] = "https://rapidshare.com/files/#{response[:id]}/#{URI::encode response[:filename]}" if response[:filename]
+
response
end
- # Get file ID by absolute path
+ # Returns file ID by absolute path
#
- # api.file_id("foo/bar/baz/file.rar") # -> 456
+ # api.file_id("foo/bar/baz/file.rar") # => <FILE_ID>
def file_id(file_path, params = {})
params[:fields] = ""
file_info = file_info file_path, params
(file_info || {})[:id].to_i
end
protected
def path_trim(path)
path.gsub(/\A\/+/, '').gsub(/\/+\Z/, '')
+ end
+
+ def path_canonize(path)
+ "/" + path_trim(path)
end
end
end
end
\ No newline at end of file