cloudpt-api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ spec/connection.yml
6
+ bin
7
+ coverage
8
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cloudpt-api.gemspec
4
+ gemspec
5
+
6
+ gem "rspec"
7
+ gem "rake"
8
+ gem 'simplecov'
9
+ gem "ruby-debug19"
10
+ gem "yajl-ruby"
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2011 Marcin Bunsch, Future Simple Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,322 @@
1
+ Cloudpt::API - Cloudpt Ruby API client
2
+ =========
3
+
4
+ A Ruby client for the Cloudpt REST API.
5
+
6
+ Goal:
7
+
8
+ To deliver a more Rubyesque experience when using the Cloudpt API.
9
+
10
+ Current state:
11
+
12
+ First release, whole API covered.
13
+
14
+ Installation
15
+ ------------
16
+
17
+ Cloudpt::API is available on RubyGems, so:
18
+
19
+ ```
20
+ gem install cloudpt-api
21
+ ```
22
+
23
+ Or in your Gemfile:
24
+
25
+ ```ruby
26
+ gem "cloudpt-api"
27
+ ```
28
+
29
+ Configuration
30
+ -------------
31
+
32
+ In order to use this client, you need to have an app created on https://www.Cloudpt.com/developers/apps.
33
+
34
+ Once you have it, put this configuration somewhere in your code, before you start working with the client.
35
+
36
+ ```ruby
37
+ Cloudpt::API::Config.app_key = YOUR_APP_TOKEN
38
+ Cloudpt::API::Config.app_secret = YOUR_APP_SECRET
39
+ Cloudpt::API::Config.mode = "sandbox" # if you have a single-directory app or "Cloudpt" if it has access to the whole Cloudpt
40
+ ```
41
+
42
+ Cloudpt::API::Client
43
+ --------------------
44
+
45
+ The client is the base for all communication with the API and wraps around almost all calls
46
+ available in the API.
47
+
48
+ In order to create a Cloudpt::API::Client object, you need to have the configuration set up for OAuth.
49
+ Second thing you need is to have the user authorize your app using OAuth. Here's a short intro
50
+ on how to do this:
51
+
52
+ ```ruby
53
+ consumer = Cloudpt::API::OAuth.consumer(:authorize)
54
+ request_token = consumer.get_request_token
55
+ request_token.authorize_url(:oauth_callback => 'http://yoursite.com/callback')
56
+ # Here the user goes to Cloudpt, authorizes the app and is redirected
57
+ # The oauth_token will be available in the params
58
+ request_token.get_access_token(:oauth_verifier => oauth_token)
59
+ ```
60
+
61
+ Now that you have the oauth token and secret, you can create a new instance of the Cloudpt::API::Client, like this:
62
+
63
+ ```ruby
64
+ client = Cloudpt::API::Client.new :token => token, :secret => secret
65
+ ```
66
+
67
+ Rake-based authorization
68
+ ------------------------
69
+
70
+ Cloudpt::API supplies you with a helper rake which will authorize a single client. This is useful for development and testing.
71
+
72
+ In order to have this rake available, put this on your Rakefile:
73
+
74
+ ```ruby
75
+ require "cloudpt-api/tasks"
76
+ Cloudpt::API::Tasks.install
77
+ ```
78
+
79
+ You will notice that you have a new rake task - Cloudpt:authorize
80
+
81
+ When you call this Rake task, it will ask you to provide the consumer key and secret. Afterwards it will present you with an authorize url on Cloudpt.
82
+
83
+ Simply go to that url, authorize the app, then press enter in the console.
84
+
85
+ The rake task will output valid ruby code which you can use to create a client.
86
+
87
+ What differs this from the Cloudpt Ruby SDK?
88
+ --------------------------------------------
89
+
90
+ A few things:
91
+
92
+ * It's using the ruby oauth gem, instead of reinventing the wheel and implementing OAuth communication
93
+ * It treats files and directories as Ruby objects with appropriate classes, on which you can perform operations
94
+
95
+ Consider the following example which takes all files with names like 'test.txt' and copies them with a suffix '.old'
96
+
97
+ This is how it would look using the SDK:
98
+
99
+ ```ruby
100
+ # Because you need the session with the right access token, you need to create one instance per user
101
+ @session = CloudptSession.new(APP_TOKEN, APP_SECRET)
102
+ @session.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
103
+ @client = CloudptClient.new(@session, :app_folder)
104
+ # The result is a hash, so we need to call a method on the client, supplying the right key from the hash
105
+ @client.search('/', 'test.txt').each do |hash|
106
+ @client.file_copy(hash['path'], hash['path'] + ".old")
107
+ end
108
+ ```
109
+
110
+ With Cloudpt::API, you can clean it up, first you put the app token and secret in a config or initializer file:
111
+
112
+ ```ruby
113
+ Cloudpt::API::Config.app_key = APP_TOKEN
114
+ Cloudpt::API::Config.app_secret = APP_SECRET
115
+ ```
116
+
117
+ And when you want to use it, just create a new client object with a specific access token and secret:
118
+
119
+ ```ruby
120
+ # The app token and secret are read from config, that's all you need to have a client ready for one user
121
+ @client = Cloudpt::API::Client.new(:token => ACCESS_TOKEN, :secret => ACCESS_SECRET)
122
+ # The file is a Cloudpt::API::File object, so you can call methods on it!
123
+ @client.search('test.txt').each do |file|
124
+ file.copy(file.path + ".old2")
125
+ end
126
+ ```
127
+
128
+ What differs this from the Cloudpt gem?
129
+ --------------------------------------
130
+
131
+ Cloudpt::API does not extend the Ruby primitives, like the Cloudpt gem:
132
+
133
+ https://github.com/RISCfuture/Cloudpt/tree/master/lib/Cloudpt/extensions
134
+
135
+ Cloudpt::API::Client methods
136
+ ----------------------------
137
+
138
+ ### Cloudpt::API::Client#account
139
+
140
+ Returns a simple object with information about the account:
141
+
142
+ ```ruby
143
+ client.account # => #<Cloudpt::API::Object>
144
+ ```
145
+
146
+ For more info, see [https://www.Cloudpt.com/developers/reference/api#account-info](https://www.Cloudpt.com/developers/reference/api#account-info)
147
+
148
+ ### Cloudpt::API::Client#find
149
+
150
+ When provided a path, returns a single file or directory
151
+
152
+ ```ruby
153
+ client.find 'file.txt' # => #<Cloudpt::API::File>
154
+ ```
155
+
156
+ ### Cloudpt::API::Client#ls
157
+
158
+ When provided a path, returns a list of files or directories within that path
159
+
160
+ By default it's the root path:
161
+
162
+ ```ruby
163
+ client.ls # => [#<Cloudpt::API::File>, #<Cloudpt::API::Dir>]
164
+ ```
165
+
166
+ But you can provide your own path:
167
+
168
+ ```ruby
169
+ client.ls 'somedir' # => [#<Cloudpt::API::File>, #<Cloudpt::API::Dir>]
170
+ ```
171
+
172
+ ### Cloudpt::API::Client#mkdir
173
+
174
+ Creates a new directory and returns a Cloudpt::API::Dir object
175
+
176
+ ```ruby
177
+ client.mkdir 'new_dir' # => #<Cloudpt::API::Dir>
178
+ ```
179
+
180
+ ### Cloudpt::API::Client#upload
181
+
182
+ Stores a file with a provided body under a provided name and returns a Cloudpt::API::File object
183
+
184
+ ```ruby
185
+ client.upload 'file.txt', 'file body' # => #<Cloudpt::API::File>
186
+ ```
187
+
188
+ ### Cloudpt::API::Client#download
189
+
190
+ Downloads a file with a provided name and returns it's content
191
+
192
+ ```ruby
193
+ client.download 'file.txt' # => 'file body'
194
+ ```
195
+
196
+ ### Cloudpt::API::Client#search
197
+
198
+ When provided a pattern, returns a list of files or directories within that path
199
+
200
+ By default it searches the root path:
201
+
202
+ ```ruby
203
+ client.search 'pattern' # => [#<Cloudpt::API::File>, #<Cloudpt::API::Dir>]
204
+ ```
205
+
206
+ However, you can specify your own path:
207
+
208
+ ```ruby
209
+ client.search 'pattern', :path => 'somedir' # => [#<Cloudpt::API::File>, #<Cloudpt::API::Dir>]
210
+ ```
211
+
212
+ ### Cloudpt::API::Client#delta
213
+
214
+ Returns a cursor and a list of files that have changed since the cursor was generated.
215
+
216
+ ```ruby
217
+ delta = client.delta 'abc123'
218
+ delta.cursor # => 'def456'
219
+ delta.entries # => [#<Cloudpt::API::File>, #<Cloudpt::API::Dir>]
220
+ ```
221
+
222
+ When called without a cursor, it returns all the files.
223
+
224
+ ```ruby
225
+ delta = client.delta 'abc123'
226
+ delta.cursor # => 'abc123'
227
+ delta.entries # => [#<Cloudpt::API::File>, #<Cloudpt::API::Dir>]
228
+ ```
229
+
230
+ Cloudpt::API::File and Cloudpt::API::Dir methods
231
+ ----------------------------
232
+
233
+ These methods are shared by Cloudpt::API::File and Cloudpt::API::Dir
234
+
235
+ ### Cloudpt::API::File#copy | Cloudpt::API::Dir#copy
236
+
237
+ Copies a file/directory to a new specified filename
238
+
239
+ ```ruby
240
+ file.copy 'newfilename.txt' # => #<Cloudpt::API::File>
241
+ ```
242
+
243
+ ### Cloudpt::API::File#move | Cloudpt::API::Dir#move
244
+
245
+ Moves a file/directory to a new specified filename
246
+
247
+ ```ruby
248
+ file.move 'newfilename.txt' # => #<Cloudpt::API::File>
249
+ ```
250
+
251
+ ### Cloudpt::API::File#destroy | Cloudpt::API::Dir#destroy
252
+
253
+ Deletes a file/directory
254
+
255
+ ```ruby
256
+ file.destroy 'newfilename.txt' # => #<Cloudpt::API::File>
257
+ ```
258
+
259
+
260
+ Cloudpt::API::File methods
261
+ ----------------------------
262
+
263
+ ### Cloudpt::API::File#revisions
264
+
265
+ Returns an Array of Cloudpt::API::File objects with appropriate rev attribute
266
+
267
+ For more info, see [https://www.Cloudpt.com/developers/reference/api#revisions](https://www.Cloudpt.com/developers/reference/api#revisions)
268
+
269
+ ### Cloudpt::API::File#restore
270
+
271
+ Restores a file to a specific revision
272
+
273
+ For more info, see [https://www.Cloudpt.com/developers/reference/api#restore](https://www.Cloudpt.com/developers/reference/api#restore)
274
+
275
+ ### Cloudpt::API::File#share_url
276
+
277
+ Returns the link to a file page in Cloudpt
278
+
279
+ For more info, see [https://www.Cloudpt.com/developers/reference/api#shares](https://www.Cloudpt.com/developers/reference/api#shares)
280
+
281
+ ### Cloudpt::API::File#direct_url
282
+
283
+ Returns the link to a file in Cloudpt
284
+
285
+ For more info, see [https://www.Cloudpt.com/developers/reference/api#media](https://www.Cloudpt.com/developers/reference/api#media)
286
+
287
+ ### Cloudpt::API::File#thumbnail
288
+
289
+ Returns the thumbnail for an image
290
+
291
+ For more info, see [https://www.Cloudpt.com/developers/reference/api#thumbnail](https://www.Cloudpt.com/developers/reference/api#thumbnail)
292
+
293
+ ### Cloudpt::API::File#download
294
+
295
+ Downloads a file and returns it's content
296
+
297
+ ```ruby
298
+ file.download # => 'file body'
299
+ ```
300
+
301
+ Cloudpt::API::Dir methods
302
+ ----------------------------
303
+
304
+ ### Cloudpt::API::Dir#ls
305
+
306
+ Returns a list of files or directorys within that directory
307
+
308
+ ```ruby
309
+ dir.ls # => [#<Cloudpt::API::File>, #<Cloudpt::API::Dir>]
310
+ ```
311
+
312
+ Testing
313
+ ---------
314
+
315
+ In order to run tests, you need to have an application created and authorized. Put all tokens in spec/connection.yml and you're good to go.
316
+
317
+ Check out spec/connection.sample.yml for an example.
318
+
319
+ Copyright
320
+ ---------
321
+
322
+ Copyright (c) 2011 Marcin Bunsch, Future Simple Inc. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require "cloudpt-api/tasks"
4
+ Cloudpt::API::Tasks.install
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # regards to Marcin Bunsch, creator of dropbox-api gem.
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+ require "cloudpt-api/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "cloudpt-api"
8
+ s.version = Cloudpt::API::VERSION
9
+ s.authors = ["Fabio Batista"]
10
+ s.email = ["fbatista@webreakstuff.com"]
11
+ s.homepage = "http://github.com/fbatista/cloudpt-api"
12
+ s.summary = "A Ruby client for the cloudpt REST API."
13
+ s.description = "To deliver a more Rubyesque experience when using the cloudpt API."
14
+
15
+ s.rubyforge_project = "cloudpt-api"
16
+
17
+ s.add_dependency 'multi_json'
18
+ s.add_dependency 'oauth'
19
+ s.add_dependency 'hashie'
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,22 @@
1
+ require "oauth"
2
+ require "multi_json"
3
+ require "hashie"
4
+
5
+ module Cloudpt
6
+ module API
7
+
8
+ end
9
+ end
10
+
11
+ require "cloudpt-api/version"
12
+ require "cloudpt-api/util/config"
13
+ require "cloudpt-api/util/oauth"
14
+ require "cloudpt-api/util/error"
15
+ require "cloudpt-api/util/util"
16
+ require "cloudpt-api/objects/object"
17
+ require "cloudpt-api/objects/fileops"
18
+ require "cloudpt-api/objects/file"
19
+ require "cloudpt-api/objects/dir"
20
+ require "cloudpt-api/objects/delta"
21
+ require "cloudpt-api/connection"
22
+ require "cloudpt-api/client"
@@ -0,0 +1,72 @@
1
+ require "cloudpt-api/client/raw"
2
+ require "cloudpt-api/client/files"
3
+
4
+ module Cloudpt
5
+ module API
6
+
7
+ class Client
8
+
9
+ attr_accessor :raw, :connection
10
+
11
+ def initialize(options = {})
12
+ @connection = Cloudpt::API::Connection.new(:token => options.delete(:token),
13
+ :secret => options.delete(:secret))
14
+ @raw = Cloudpt::API::Raw.new :connection => @connection
15
+ @options = options
16
+ end
17
+
18
+ include Cloudpt::API::Client::Files
19
+
20
+ def find(filename)
21
+ data = self.raw.metadata(:path => filename)
22
+ data.delete('contents')
23
+ Cloudpt::API::Object.convert(data, self)
24
+ end
25
+
26
+ def list(path = '')
27
+ response = raw.list :path => path
28
+ end
29
+
30
+ def ls(path = '')
31
+ Cloudpt::API::Dir.init({'path' => path}, self).ls
32
+ end
33
+
34
+ def account
35
+ Cloudpt::API::Object.init(self.raw.account, self)
36
+ end
37
+
38
+ def mkdir(path)
39
+ # Remove the characters not allowed by Cloudpt
40
+ path = path.gsub(/[\\\:\?\*\<\>\"\|]+/, '')
41
+ response = raw.create_folder :path => path
42
+ Cloudpt::API::Dir.init(response, self)
43
+ end
44
+
45
+ def search(term, options = {})
46
+ options[:path] ||= ''
47
+ results = raw.search({ :query => term }.merge(options))
48
+ Cloudpt::API::Object.convert(results, self)
49
+ end
50
+
51
+ def delta(cursor=nil)
52
+ entries = []
53
+ has_more = true
54
+ params = cursor ? {:cursor => cursor} : {}
55
+ while has_more
56
+ response = raw.delta(params)
57
+ params[:cursor] = response['cursor']
58
+ has_more = response['has_more']
59
+ entries.push *response['entries']
60
+ end
61
+
62
+ files = entries.map do |entry|
63
+ entry.last || {:is_deleted => true, :path => entry.first}
64
+ end
65
+
66
+ Delta.new(params[:cursor], Cloudpt::API::Object.convert(files, self))
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+ end