uploadcare-ruby 1.0.1.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bd6d3a7c2b5ea89ee8d5f27a4e94768cf71eb2a4
4
+ data.tar.gz: 82b45824c695e1d038a37817555bfdb46cf9d919
5
+ SHA512:
6
+ metadata.gz: 06b23351a97c96ec9b3a4646821c544ebadfd3ef07263d69cf1259ef0c7fd42dec577b7fa492deef0de236de469a6a632cb7ad8a3ffa2090f68a63ee2ddcbf9e
7
+ data.tar.gz: f3467ac3e6b8ecd837af06fe28afa8d3e322addb19980831010257227531807cad02e907543b2cb690c1be1c015882346c74c9730684f0faa1f146732b0e7d5a
Binary file
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in uploadcare-ruby.gemspec
4
+ gemspec
@@ -0,0 +1,42 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ uploadcare-ruby (1.0.0)
5
+ faraday
6
+ faraday_middleware
7
+ mime-types
8
+ multipart-post
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ coderay (1.0.9)
14
+ diff-lcs (1.2.4)
15
+ faraday (0.8.8)
16
+ multipart-post (~> 1.2.0)
17
+ faraday_middleware (0.9.0)
18
+ faraday (>= 0.7.4, < 0.9)
19
+ method_source (0.8.2)
20
+ mime-types (2.0)
21
+ multipart-post (1.2.0)
22
+ pry (0.9.12.2)
23
+ coderay (~> 1.0.5)
24
+ method_source (~> 0.8)
25
+ slop (~> 3.4)
26
+ rspec (2.14.1)
27
+ rspec-core (~> 2.14.0)
28
+ rspec-expectations (~> 2.14.0)
29
+ rspec-mocks (~> 2.14.0)
30
+ rspec-core (2.14.7)
31
+ rspec-expectations (2.14.3)
32
+ diff-lcs (>= 1.1.3, < 2.0)
33
+ rspec-mocks (2.14.4)
34
+ slop (3.4.6)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ pry
41
+ rspec
42
+ uploadcare-ruby!
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Vadim Rastyagaev
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,385 @@
1
+ [![Build Status](https://secure.travis-ci.org/uploadcare/ruby-uploadcare-api.png?branch=master)](http://travis-ci.org/uploadcare/ruby-uploadcare-api)
2
+
3
+ A ruby wrapper for uploadcare.com service.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'uploadcare-ruby'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ ```shell
17
+ $ bundle install
18
+ ```
19
+
20
+ Or install it yourself as:
21
+
22
+ ```shell
23
+ $ gem install uploadcare-ruby
24
+ ```
25
+
26
+ --
27
+
28
+ ## Initalizations
29
+ Just create api object - and you good to go.
30
+
31
+ ```ruby
32
+ @api = Uploadcare::Api.new(CONFIG)
33
+ ```
34
+
35
+ --
36
+
37
+ ## Raw API
38
+ Raw API - it is a simple interface wich allows you to make custom requests to Uploadcare REST API.
39
+ Just in case you want some low-level control over your app.
40
+
41
+ ```ruby
42
+ # any request
43
+ @api.request :get, "/files/", {page: 2}
44
+
45
+ # you allso have the shortcuts for methods
46
+ @api.get '/files', {page: 2}
47
+
48
+ @api.post ...
49
+
50
+ @api.put ...
51
+
52
+ @api.delete ...
53
+
54
+ ```
55
+ All raw API methods returns parsed JSON response or raise an error (from which you should rescue on your own).
56
+
57
+ --
58
+
59
+ ## Basic usage
60
+ Using Uploadcare is pretty easy (which is the essence of the service itself).
61
+
62
+ Create the API object:
63
+
64
+ ```ruby
65
+ @api = Uploadcare::Api.new(CONFIG)
66
+
67
+ ```
68
+
69
+ Upload file
70
+
71
+ ```ruby
72
+ @file_to_upload = File.open("your-file.png")
73
+
74
+ @uc_file = @api.upload(@file_to_upload)
75
+ # => #<Uploadcare::Api::File ...
76
+ ```
77
+
78
+ Use file
79
+
80
+ ```ruby
81
+ # file uuid (you probably want to store it somewhere)
82
+ @uc_file.uuid
83
+ # => "c969be02-9925-4a7e-aa6d-b0730368791c"
84
+
85
+ # url for the file - just paste in your template and you good to go.
86
+ @uc_file.cdn_url
87
+ # => "http://www.ucarecdn.com/c969be02-9925-4a7e-aa6d-b0730368791c/"
88
+ ```
89
+
90
+ Keep or delete file
91
+
92
+ ```ruby
93
+ # store file (if you dont use autostore feature)
94
+ @uc_file.store
95
+ # => #<Uploadcare::Api::File ...
96
+
97
+ # and delete file
98
+ @uc_file.delete
99
+ # => #<Uploadcare::Api::File ...
100
+ ```
101
+ ## Uploading
102
+ You can upload either File object (array of files will also cut it) or custom URL.
103
+
104
+ ### Uploading from URL
105
+ Just throw your URL into api - and you good to go.
106
+
107
+ ```ruby
108
+ # smart upload
109
+ @file = @api.upload "http://your.awesome/avatar.jpg"
110
+ # => #<Uploadcare::Api::File ...
111
+
112
+ # explicitly upload from URl
113
+ @file = @api.upload_from_url "http://your.awesome/avatar.jpg"
114
+ # => #<Uploadcare::Api::File ...
115
+ ```
116
+ Keep in mind that invalid url will rise an ArgumentError.
117
+
118
+ ### Uploading a single file
119
+ Like with URL - just start throwing your file into api
120
+
121
+ ```ruby
122
+
123
+ file = File.open("path/to/your/file.png")
124
+
125
+ @uc_file = @api.upload file
126
+ # => #<Uploadcare::Api::File ...
127
+
128
+ ```
129
+ And thats it.
130
+
131
+ ### Uploading an array of files
132
+ Uploading of an array is just as easy as uploading single files.
133
+ Note, that every object in array must be an instance of File.
134
+
135
+ ```ruby
136
+ file1 = File.open("path/to/your/file.png")
137
+ file2 = File.open("path/to/your/another-file.png")
138
+ files = [file1, file2]
139
+
140
+ @uc_files = @api.upload files
141
+ # => [#<Uploadcare::Api::File uuid="24626d2f-3f23-4464-b190-37115ce7742a">,
142
+ # #<Uploadcare::Api::File uuid="7bb9efa4-05c0-4f36-b0ef-11a4221867f6">]
143
+ ```
144
+ It is returning you an array of Uploadcare files.
145
+
146
+ ```ruby
147
+ @uc_files[0]
148
+ # => #<Uploadcare::Api::File uuid="24626d2f-3f23-4464-b190-37115ce7742a">
149
+
150
+ @uc_files[0].load_data
151
+ # => #<Uploadcare::Api::File uuid="7bb9efa4-05c0-4f36-b0ef-11a4221867f6", original_file_url="http://www.ucarecdn.com/7bb9efa4-05c0-4f36-b0ef-11a4221867f6/view.png", image_info={"width"=>800, "geo_location"=>nil, "datetime_original"=>nil, "height"=>600}, ....>
152
+ ```
153
+
154
+ ## File
155
+ File - is the primary object for Uploadcare API. Basicly it an avatar for file, stored for you ).
156
+ So all the opertations you do - you do it with the file object.
157
+
158
+ *to do:* way to build file from UUID, CDN URL, and uploading
159
+
160
+
161
+ ```ruby
162
+ @file_to_upload = File.open("your-file.png")
163
+
164
+ @uc_file = @api.upload(@file_to_upload)
165
+ # => #<Uploadcare::Api::File ...
166
+
167
+ @uc_file.uuid
168
+ # => "c969be02-9925-4a7e-aa6d-b0730368791c"
169
+
170
+ @uc_file.cdn_url
171
+ # => "http://www.ucarecdn.com/c969be02-9925-4a7e-aa6d-b0730368791c/"
172
+ ```
173
+ There is one issue with files - all data associated with it accesible with separate HTTP request only.
174
+ So if don't *specificaly* need image data (like file name, geolocation data etc) - you could just use :uuid and :cdn_url methods for file output:
175
+
176
+ ```erb
177
+ <img src="#{@file.cdn_url}"/>
178
+ ```
179
+
180
+ And thats it. Saves precious loading time.
181
+
182
+ If you do however need image data - you could do it manualy:
183
+
184
+ ```ruby
185
+ @uc_file.load_data
186
+ ```
187
+
188
+ Then your file object will respond to any method, described in API documentations (it basicaly an OpenStruct, so you know what to do):
189
+
190
+ ```ruby
191
+ @uc_file.original_filename
192
+ # => "logo.png"
193
+
194
+ @uc_file.image_info
195
+ # => {"width"=>397, "geo_location"=>nil, "datetime_original"=>nil, "height"=>81}
196
+ ```
197
+
198
+ You could read more https://uploadcare.com/documentation/rest/#file
199
+
200
+ ### Generating files from stored info
201
+ At this point you probably store your files UUIDs or CDN urls in some kind of storage.
202
+ Then you can created file object by passing them into API:
203
+
204
+ ```ruby
205
+ # file by UUID
206
+ @file = @api.file "c969be02-9925-4a7e-aa6d-b0730368791c"
207
+ # => #<Uploadcare::Api::File uuid="7bb9efa4-05c0-4f36-b0ef-11a4221867f6"
208
+
209
+ # file by CDN url
210
+ @file = @api.file "http://www.ucarecdn.com/a8775cf7-0c2c-44fa-b071-4dd48637ecac/"
211
+ # => #<Uploadcare::Api::File uuid="7bb9efa4-05c0-4f36-b0ef-11a4221867f6"
212
+
213
+ # not that generated files aren't loaded by initializing, you need to load it.
214
+ @file.is_loaded?
215
+ # => false
216
+ ```
217
+
218
+ ### Operations
219
+ Uploadcare presents for you some awesome CDN operations for croping, resizing, rotation, format convertation etc. You could read more at https://uploadcare.com/documentation/cdn/
220
+ Version 1.0.0 of this gem has not specific methods for this kind of operations, we expecting it comes lately in 1.1 releases.
221
+ For now all you files objects can store operations passed by cdn url:
222
+
223
+ ```ruby
224
+ @file = @api.file "http://www.ucarecdn.com/a8775cf7-0c2c-44fa-b071-4dd48637ecac/-/crop/150x150/center/-/format/png/"
225
+ # => #<Uploadcare::Api::File uuid="a8775cf7-0c2c-44fa-b071-4dd48637ecac"
226
+
227
+ @file.operations
228
+ # => ["crop/150x150/center", "format/png"]
229
+
230
+ # note that by default :cdn_url method will return url without any operations:
231
+ @file.cdn_url
232
+ # => "http://www.ucarecdn.com/a8775cf7-0c2c-44fa-b071-4dd48637ecac/""
233
+
234
+ # you can pass true to :cdn_url methods to get url with included operations:
235
+ @file.cdn_url(true)
236
+ # => "http://www.ucarecdn.com/a8775cf7-0c2c-44fa-b071-4dd48637ecac/-/crop/150x150/center/-/format/png/"
237
+
238
+ # or cal specific methods for url with or without them:
239
+ @file.cdn_url_with_operations
240
+ @file.cdn_url_without_operations
241
+ ```
242
+
243
+ Until operations wrapper is released best way for you to manage operation is simply add them to url as a string:
244
+
245
+ ```ruby
246
+ <img src="#{file.cdn_url}-/crop/#{width}x#{height}/center/"/>
247
+ # or something like that
248
+ ```
249
+
250
+ ## Files list and pagination
251
+ File lists - it is a paginated collection of files for you project. You could read more at https://uploadcare.com/documentation/rest/#pagination.
252
+ In our gem file list is a single page containing 20 (by default, value may change) files and some methods for navgiting throug pages.
253
+
254
+ ```ruby
255
+ @list = @api.file_list 1 #page number, 1 by default
256
+ # => #<Uploadcare::Api::FileList ....
257
+
258
+
259
+ # method :resulst will return you an array of files
260
+ @list.results
261
+ # => [#<Uploadcare::Api::File uuid="24626d2f-3f23-4464-b190-37115ce7742a" ...>,
262
+ # ... 20 of them ...
263
+ # #<Uploadcare::Api::File uuid="7bb9efa4-05c0-4f36-b0ef-11a4221867f6" ...>]
264
+
265
+
266
+ # note that every file is already loaded
267
+ @list.results[1].is_loaded?
268
+ # => true
269
+
270
+
271
+ # there is also shortcuts for you
272
+ @list.to_a
273
+ # => [#<Uploadcare::Api::File uuid="24626d2f-3f23-4464-b190-37115ce7742a" ...>,
274
+ # ... 20 of them ...
275
+ # #<Uploadcare::Api::File uuid="7bb9efa4-05c0-4f36-b0ef-11a4221867f6" ...>]
276
+
277
+ @list[3]
278
+ # => #<Uploadcare::Api::File ....
279
+ ```
280
+
281
+ And don't forget navigation throught pages:
282
+
283
+ ```ruby
284
+ @list = @api.files_list 3
285
+
286
+ @list.next_page
287
+ # => #<Uploadcare::Api::FileList page=4 ....
288
+
289
+ @list.previous_page
290
+ # => #<Uploadcare::Api::FileList page=2 ....
291
+
292
+ @list.go_to 5
293
+ # => #<Uploadcare::Api::FileList page=5 ....
294
+
295
+
296
+
297
+ # there is also methods described in API docs avaliable for you:
298
+ # total pages
299
+ @list.pages
300
+ # => 16
301
+
302
+ # current page
303
+ @list.page
304
+ # => 3
305
+
306
+ # files per page
307
+ @list.per_page
308
+ # => 20
309
+
310
+ # total files in project
311
+ @list.total
312
+ # => 308
313
+ ```
314
+
315
+ ## Project
316
+ Project provides basic information about the connecting project.
317
+ Project object is basicly openstruct so every method described in API docs (https://uploadcare.com/documentation/rest/#project) accessible to you:
318
+
319
+ ```ruby
320
+ project = @api.project
321
+ # => #<Uploadcare::Api::Project collaborators=[], name="demo", pub_key="demopublickey", autostore_enabled=true>
322
+
323
+ project.name
324
+ # => "demo"
325
+
326
+ p.collaborators
327
+ # => []
328
+ # more often it should look like
329
+ # [{"email": collaborator@gmail.com, "name": "Collaborator"}, {"email": collaborator@gmail.com, "name": "Collaborator"}]
330
+ ```
331
+
332
+
333
+ ## Groups of files
334
+ Groups of files - https://uploadcare.com/documentation/rest/#group.
335
+ Stores files as group by the single UUID.
336
+ Note that UUID has a bit ~#{files_count} at the end and it is required by API to work properly.
337
+
338
+ ```ruby
339
+ # group can be created eather by array of Uploadcare Files:
340
+ @files_ary = [@file, @file2]
341
+ @files = @api.upload @files_ary
342
+ @group = @api.create_group @files
343
+ # => #<Uploadcare::Api::Group uuid="0d192d66-c7a6-4465-b2cd-46716c5e3df3~2", files_count=2 ...
344
+
345
+ # or by array of strings containing UUIDs
346
+ @uuids_ary = ["c969be02-9925-4a7e-aa6d-b0730368791c", "c969be02-9925-4a7e-aa6d-b0730368791c"]
347
+ @group = @api.create_group @uuids_ary
348
+ # => #<Uploadcare::Api::Group uuid="0d192d66-c7a6-4465-b2cd-46716c5e3df3~2", files_count=2 ...
349
+
350
+ # you can also create group object just by passing group UUID
351
+ @group_uloaded = @api.group "#{uuid}"
352
+ ```
353
+
354
+ As with files, group created by passing just the UUID is not loaded by default - you need to load data manualy, as it requires separate HTTP GET request.
355
+ New groups created by :create_group method is loaded by default.
356
+
357
+ ```ruby
358
+ @group = @api.group "#{uuid}"
359
+
360
+ @group.is_loaded?
361
+ # => false
362
+
363
+ @group.load_data
364
+ # => #<Uploadcare::Api::Group uuid="0d192d66-c7a6-4465-b2cd-46716c5e3df3~2", files_count=2 ...
365
+
366
+ # loaded group has methods described by API docs and more importantly an array of files
367
+ # this files are loaded by default.
368
+ @group.files
369
+ # => [#<Uploadcare::Api::File uuid="24626d2f-3f23-4464-b190-37115ce7742a" ...>,
370
+ # ... #{files_count} of them ...
371
+ # #<Uploadcare::Api::File uuid="7bb9efa4-05c0-4f36-b0ef-11a4221867f6" ...>]
372
+ ```
373
+
374
+ ## Testing
375
+
376
+ Run `bundle exec rspec`.
377
+
378
+ To run tests with your own keys, make a `spec/config.yml` file like this:
379
+
380
+ ```yaml
381
+ public_key: 'PUBLIC KEY'
382
+ private_key: 'PRIVATE KEY'
383
+ ```
384
+
385
+ ## Contributing