dropbox 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/README +3 -0
  2. data/Rakefile +22 -0
  3. data/VERSION +1 -0
  4. data/lib/dropbox.rb +1 -0
  5. data/lib/dropbox/dropbox.rb +174 -0
  6. metadata +80 -0
data/README ADDED
@@ -0,0 +1,3 @@
1
+ A simple Ruby API for DropBox
2
+
3
+ Install with: gem install dropbox (using gemcutter as source)
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "dropbox"
8
+ gem.summary = %Q{A simple Ruby API for DropBox }
9
+ gem.description = <<-DESC
10
+ A simple Ruby API for DropBox
11
+ DESC
12
+ gem.email = ""
13
+ gem.homepage = ""
14
+ gem.authors = []
15
+ gem.files = FileList["[A-Z]*", "{lib}/**/*"]
16
+
17
+ gem.add_dependency("nokogiri")
18
+ gem.add_dependency("mechanize")
19
+ end
20
+ rescue LoadError
21
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
22
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
data/lib/dropbox.rb ADDED
@@ -0,0 +1 @@
1
+ require 'dropbox/dropbox'
@@ -0,0 +1,174 @@
1
+ require 'rubygems'
2
+ require 'nokogiri'
3
+ require 'mechanize'
4
+
5
+ WWW::Mechanize.html_parser = Nokogiri::HTML
6
+
7
+ # TODO: Tests (mock out expected DropBox output)
8
+ # TODO: Directory object, File object
9
+
10
+ class DropBox
11
+ #before_filter :login_filter, :except => [:login]
12
+
13
+ def initialize(email, password, folder_namespace = "")
14
+ @email = email
15
+ @password = password
16
+ @agent = WWW::Mechanize.new
17
+ @folder_namespace = folder_namespace.gsub(/^\//,"")
18
+ @logged_in = false
19
+ end
20
+
21
+ def login
22
+ page = @agent.get('https://www.dropbox.com/login')
23
+ login_form = page.forms.detect { |f| f.action == "/login" }
24
+ login_form.login_email = @email
25
+ login_form.login_password = @password
26
+
27
+ home_page = @agent.submit(login_form)
28
+ # todo check if we are logged in! (ie search for email and "Log out"
29
+ @logged_in = true
30
+ @token = home_page.at('//script[contains(text(), "TOKEN")]').content.match("TOKEN: '(.*)',$")[1]
31
+
32
+ # check if we have our namespace
33
+
34
+ home_page
35
+ end
36
+
37
+ def login_filter
38
+ login unless @logged_in
39
+ end
40
+
41
+ def list(path = "/")
42
+ index(path)
43
+ end
44
+
45
+ def index(path = "/")
46
+ login_filter
47
+ path = namespace_path(path)
48
+
49
+ list = @agent.post("/browse2#{path}?ajax=yes", {"d"=> 1, "t" => @token })
50
+
51
+ listing = list.search('div.browse-file-box-details').collect do |file|
52
+ details = {}
53
+ details['name'] = file.at('div.details-filename a').content.strip
54
+ details['url'] = file.at('div.details-filename a')["href"]
55
+ #details['size'] = file.at('div.details-size a').try(:content).try(:strip)
56
+ #details['modified'] = file.at('div.details-modified a').try(:content).try(:strip)
57
+
58
+ if match_data = details['url'].match(/^\/browse_plain(.*)$/)
59
+ details['directory'] = true
60
+ details['path'] = normalize_namespace(match_data[1])
61
+ else
62
+ details['directory'] = false
63
+ details['path'] = normalize_namespace(details['url'][33..-1])
64
+ end
65
+
66
+ details
67
+ end
68
+
69
+ return listing
70
+ end
71
+
72
+ def list_history(path)
73
+ login_filter
74
+
75
+ path = namespace_path(path)
76
+
77
+ history = @agent.get("/revisions#{path}")
78
+ listing = history.search("table.filebrowser > tr").select{|r| r.search("td").count > 1 }.collect do |r|
79
+
80
+ # warning, this is very brittle!
81
+ details = {}
82
+ details["version"] = r.search("td a").first.content.strip
83
+ details["url"] = r.search("td a").first["href"]
84
+ details["size"] = r.search("td").last.content.strip
85
+ details["modified"] = r.search("td")[2].content.strip
86
+ details["version_id"] = details["url"].match(/^.*sjid=([\d]*)$/)[1]
87
+ details['path'] = normalize_namespace(details['url'][33..-1])
88
+
89
+ details
90
+ end
91
+
92
+ return listing
93
+ end
94
+
95
+ def get(path)
96
+ show(path)
97
+ end
98
+
99
+ def show(path)
100
+ # change to before filter
101
+ login_filter
102
+
103
+ path = namespace_path(path)
104
+
105
+ #https://dl-web.dropbox.com/get/testing.txt?w=0ff80d5d&sjid=125987568
106
+ @agent.get("https://dl-web.dropbox.com/get/#{path}").content
107
+ end
108
+
109
+ def create_directory(new_path, destination = "/" )
110
+ # change to before filter
111
+ login unless @logged_in
112
+ destination = namespace_path(destination)
113
+ @agent.post("/cmd/new#{destination}",{"to_path"=>new_path, "folder"=>"yes", "t" => @token })
114
+ # check status code
115
+ end
116
+
117
+ def create(file, destination = "/")
118
+ # change to before filter
119
+ if @logged_in
120
+ home_page = @agent.get('https://www.dropbox.com/home')
121
+ else
122
+ home_page = login
123
+ end
124
+
125
+ destination = namespace_path(destination)
126
+
127
+ upload_form = home_page.forms.detect{ |f| f.action == "https://dl-web.dropbox.com/upload" }
128
+ upload_form.dest = "/#{@folder_namespace}/#{destination}"
129
+ upload_form.file_uploads.first.file_name = file if file
130
+
131
+ @agent.submit(upload_form)
132
+ end
133
+
134
+ def update(file, destination = "/")
135
+ create(file, destination)
136
+ end
137
+
138
+ def rename(file, destination)
139
+ login_filter
140
+ file = namespace_path(file)
141
+ destination = namespace_path(destination)
142
+ @agent.post("/cmd/rename#{file}", {"to_path"=> destination, "t" => @token })
143
+ end
144
+
145
+ def destroy(paths)
146
+ login_filter
147
+ paths = [paths].flatten
148
+ paths = paths.collect { |path| namespace_path(path) }
149
+ @agent.post("/cmd/delete", {"files"=> paths, "t" => @token })
150
+ end
151
+
152
+ def purge(paths)
153
+ login_filter
154
+ paths = [paths].flatten
155
+ paths = paths.collect { |path| namespace_path(path) }
156
+ @agent.post("/cmd/purge", {"files"=> paths, "t" => @token })
157
+ end
158
+
159
+ private
160
+ def namespace_path(path)
161
+ # remove the start slash if we have one
162
+ path.gsub(/^\//,"")
163
+ if @folder_namespace.empty?
164
+ "/#{path}"
165
+ else
166
+ "/#{@folder_namespace}/#{path}"
167
+ end
168
+ end
169
+
170
+ def normalize_namespace(file)
171
+ file.gsub(/^\/#{@folder_namespace}/,"")
172
+ end
173
+
174
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dropbox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors: []
7
+
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-06 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: nokogiri
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: mechanize
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: |
36
+ A simple Ruby API for DropBox
37
+
38
+ email: ""
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README
45
+ files:
46
+ - README
47
+ - Rakefile
48
+ - VERSION
49
+ - lib/dropbox.rb
50
+ - lib/dropbox/dropbox.rb
51
+ has_rdoc: true
52
+ homepage: ""
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --charset=UTF-8
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.3.5
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: A simple Ruby API for DropBox
79
+ test_files: []
80
+