faraday-panda 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source :rubygems
2
+
3
+ gem "ruby-hmac",">= 0.3.2"
4
+ gem "shin-faraday", :require => 'faraday'
5
+ gem "json"
6
+ gem "yajl-ruby"
7
+
8
+ gem "jeweler"
9
+ gem "rake"
10
+ gem "rspec"
11
+ gem "webmock"
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 New Bamboo Web Development Ltd
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,496 @@
1
+ # Panda
2
+
3
+ Panda gem provides an interface to access the [Panda](http://pandastream.com) API from Ruby.
4
+
5
+ * [Visit panda\_gem on Github](http://github.com/newbamboo/panda_gem)
6
+
7
+ ## Installation
8
+
9
+ gem install panda
10
+
11
+ ## How to use it
12
+
13
+ require 'rubygems'
14
+ require 'panda'
15
+
16
+ ### Creating an instance of the client
17
+
18
+ Panda.configure do |config|
19
+ config.access_key = "panda_access_key"
20
+ config.secret_key = "panda_secret_key"
21
+ config.cloud_id = "panda_cloud_id"
22
+ end
23
+
24
+ or Panda.configure({:access_key => ....})
25
+
26
+ ### Creating an instance of the client for EU
27
+
28
+ Panda.configure do |config|
29
+ config.access_key = "panda_access_key"
30
+ config.secret_key = "panda_secret_key"
31
+ config.cloud_id = "panda_cloud_id"
32
+ config.api_host = "api.eu.pandastream.com"
33
+ end
34
+
35
+ ### Creating an instance using Heroku
36
+
37
+ Panda.configure(ENV['PANDASTREAM_URL'])
38
+
39
+ ### Inside a Rails app with a main account or using Heroku
40
+
41
+ Config is stored in `config/panda.yml` or you must set an the PANDASTREAM_URL environment variable in your `/.bashrc` file (see the [Heroku config variable docs](http://docs.heroku.com/config-vars)).
42
+
43
+ Use the following in your `config/initializers/panda.rb`:
44
+
45
+ Panda.configure((ENV['PANDASTREAM_URL'] || YAML::load_file(File.join(File.dirname(__FILE__),"..", "panda.yml"))[RAILS_ENV]))
46
+
47
+ See the [Rails How-to](http://www.pandastream.com/docs/integrate_with_rails) for more details.
48
+
49
+ ### Typical usage
50
+
51
+ In most cases you will have used the [panda\_uploader](http://github.com/newbamboo/panda_uploader) jQuery plugin to upload the video (more details about this are in the [Integrating Panda with Ruby on Rails](http://pandastream.com/docs/integrate_with_rails) tutorial). Then you will want to get the video and screenshots urls of your encoding to display to your users.
52
+
53
+ The name of the profile can be found in your [Panda account](http://pandastream.com/encoders) when editing an encoding cloud's profiles.
54
+
55
+ encodings = Panda::Video.find("1234").encodings
56
+ => [...]
57
+
58
+ mp4_encoding = encodings.find_by_profile_name("h264")
59
+ ogg_encoding = encodings.find_by_profile_name("ogg")
60
+
61
+ mp4_encoding.url
62
+ => "http://s3.amazonaws.com/my_panda_bucket/4567.mp4"
63
+
64
+ mp4_encoding.screenshots[4]
65
+ => "http://s3.amazonaws.com/my_panda_bucket/4567_4.jpg"
66
+
67
+ ogg_encoding.url
68
+ => "http://s3.amazonaws.com/my_panda_bucket/9876.ogg"
69
+
70
+ ogg_encoding.screenshots[4]
71
+ => "http://s3.amazonaws.com/my_panda_bucket/9876_4.jpg"
72
+
73
+ ## Complete functionality
74
+
75
+ ### Videos
76
+
77
+ #### Find a video
78
+
79
+ video = Panda::Video.find("1234")
80
+ video.attributes
81
+ => {
82
+ "id"=>"1234",
83
+ "original_filename"=>"panda.mp4",
84
+ "source_url"=>"http://www.example.com/original_video.mp4",
85
+ "extname"=>".mp4",
86
+ "duration"=>14010,
87
+ "audio_codec"=>"aac",
88
+ "video_codec"=>"h264",
89
+ "file_size" => "44000",
90
+ "width"=>300,
91
+ "height"=>240,
92
+ "fps"=>29,
93
+ "status"=>"success",
94
+ "created_at"=>"2010/01/13 16:45:29 +0000",
95
+ "updated_at"=>"2010/01/13 16:45:35 +0000"
96
+ }
97
+
98
+ video.id
99
+ => "1234"
100
+
101
+ video.created_at
102
+ =>"2010/01/13 16:45:29 +0000"
103
+
104
+ video = Panda::Video.first
105
+
106
+ video = Panda::Video.find("fake_id")
107
+ => raise: RecordNotFound: Couldn't find Video with ID=fake_id
108
+
109
+ video.to_json
110
+ =>"{\"duration\":14010,\"created_at\":\"2010/01/13 16:45:29 +0000\",\"original_filename\":\"panda.mp4\"....}"
111
+
112
+ video.processing?
113
+ => false
114
+
115
+ video.fail?
116
+ => false
117
+
118
+ video.success?
119
+ => true
120
+
121
+ video.reload
122
+ => <Panda::Video:0x1036fd660 ...>
123
+
124
+ ##### Find encodings of a video
125
+
126
+ video = Panda::Video.find("1234")
127
+ video.encodings
128
+ => [...]
129
+
130
+ video.encodings.profile("3456")
131
+ => [...]
132
+
133
+ or
134
+
135
+ video.encodings.all(:profile_id => "3456")
136
+ => [...]
137
+
138
+ ##### Find all videos
139
+
140
+ videos = Panda::Video.all
141
+ => [...]
142
+
143
+ videos.first.id
144
+ => "3456"
145
+
146
+ videos = Panda::Video.page(2).per_page(20)
147
+
148
+ or
149
+
150
+ videos = Panda::Video.all(:page => 2, :per_page => 20)
151
+ videos.size
152
+ => 20
153
+
154
+ ##### Find all success videos
155
+
156
+ video = Panda::Video.status("success")
157
+ => [...]
158
+
159
+ or
160
+
161
+ videos = Panda::Video.all(:status => "success")
162
+ => [...]
163
+
164
+ status: success | processing | fail
165
+
166
+ #### Create a new video
167
+
168
+ from a source
169
+
170
+ video = Panda::Video.create(:source_url => "http://mywebsite.com/myvideo.mp4")
171
+
172
+ or
173
+
174
+ video = Panda::Video.new(:source_url => "http://mywebsite.com/myvideo.mp4")
175
+ video.create
176
+ => true
177
+
178
+ # Note that you will need a movie file to test this.
179
+ # You can grab http://panda-test-harness-videos.s3.amazonaws.com/panda.mp4
180
+
181
+ from a local file
182
+
183
+ video = Panda::Video.create(:file => File.new("/home/me/panda.mp4"))
184
+
185
+ or
186
+
187
+ video = Panda::Video.new(:file => File.new("/home/me/panda.mp4"))
188
+ video.create
189
+ => true
190
+
191
+ #### Delete a video
192
+
193
+ Panda::Video.delete("1234")
194
+
195
+ or
196
+
197
+ video = Panda::Video.find("1234")
198
+ video.delete
199
+ => true
200
+
201
+ ### Encodings
202
+
203
+ ##### Find an encoding
204
+
205
+ encoding = Panda::Encoding.find("4567")
206
+ encoding.attributes
207
+ => {
208
+ "id"=>"4567",
209
+ "video_id"=>"1234",
210
+ "extname"=>".mp4",
211
+ "encoding_progress"=>60,
212
+ "encoding_time"=>3,
213
+ "file_size" => "25000",
214
+ "width"=>300,
215
+ "height"=>240,
216
+ "profile_id"=>"6789",
217
+ "status"=>"success",
218
+ "started_encoding_at"=>"2010/01/13 16:47:35 +0000",
219
+ "created_at"=>"2010/01/13 16:45:30 +0000",
220
+ "updated_at"=>"2010/01/13 16:47:40 +0000"
221
+ }
222
+
223
+ encoding.encoding_progress
224
+ => 60
225
+
226
+ encoding.processing?
227
+ => false
228
+
229
+ encoding.fail?
230
+ => false
231
+
232
+ encoding.success?
233
+ => true
234
+
235
+ encoding.video.original_filename
236
+ => "panda.mp4"
237
+
238
+ encoding = Panda::Encoding.first
239
+
240
+ ##### Find all encodings of a video
241
+
242
+ encodings = Panda::Encoding.page(4)
243
+
244
+ or
245
+
246
+ encodings = Panda::Encoding.all(:page => 4)
247
+ => [...]
248
+
249
+ encodings = Panda::Encoding.video(video_id)
250
+ => [...]
251
+
252
+ encoding = Panda::Encoding.video(video_id).profile_name("h264").first
253
+
254
+ or
255
+
256
+ encoding = Panda::Encoding.find_by :video_id => "video_id", :profile_name => "h264"
257
+ encoding.encoding_time
258
+ => 3
259
+
260
+ profile = encodings.first.profile
261
+ profile.title
262
+ => "H264 profile"
263
+
264
+ ##### Find all success encodings
265
+
266
+ encodings = Panda::Encoding.video("1234").status("success")
267
+ or
268
+ encodings = Panda::Encoding.all(:video_id => "1234", :status => "success")
269
+ => [...]
270
+
271
+ or
272
+
273
+ video = Panda::Video.find("1234")
274
+ video.encodings.status("success")
275
+
276
+ status: success | processing | fail
277
+
278
+ ##### Retrieve the encoding
279
+
280
+ encoding = Panda::Encoding.find("4567")
281
+ encoding.url
282
+ => "http://s3.amazonaws.com/my_panda_bucket/4567.mp4"
283
+
284
+ encoding.screenshots[0]
285
+ => "http://s3.amazonaws.com/my_panda_bucket/4567_1.jpg"
286
+
287
+ ##### Create a new encoding
288
+
289
+ encoding = Panda::Encoding.create(:video_id => 1234, :profile_id => 6789)
290
+
291
+ or
292
+
293
+ video = Panda::Video.find("123")
294
+ encoding = video.encodings.create(:profile => "profile_id")
295
+
296
+ ##### Delete an encoding
297
+
298
+ Panda::Encoding.delete("4567")
299
+
300
+ or
301
+
302
+ encoding = Panda::Encoding.find("4567")
303
+ encoding.delete
304
+ => true
305
+
306
+ ### Profiles
307
+
308
+ ##### Find a profile
309
+
310
+ profile = Panda::Profile.find("6789")
311
+
312
+ ##### Find all profiles
313
+
314
+ profiles = Panda::Profile.all
315
+
316
+ ##### Create a profile
317
+
318
+ profile = Panda::Profile.create(:preset_name => "h264")
319
+ profile = Panda::Profile.create(:command => "ffmpeg -i $input_file$ -y $output_file$", ....)
320
+
321
+ ##### Update a profile
322
+
323
+ profile = Panda::Profile.find("6789")
324
+ profile.width = 320
325
+ profile.height = 280
326
+ profile.save
327
+ => true
328
+
329
+ profile.id = "fake_profile_id"
330
+ profile.save
331
+ => false
332
+
333
+ profile.errors.last.to_s
334
+ => RecordNotFound: Couldn't find Profile with ID=fake_profile_id
335
+
336
+ ##### Delete a profile
337
+
338
+ Panda::Profile.delete("4567")
339
+
340
+ or
341
+
342
+ profile = Panda::Profile.find("6789")
343
+ profile.delete
344
+ => true
345
+
346
+ ##### All encoding of a profile
347
+
348
+ profile = Panda::Profile.find("6789")
349
+ profile.encodings
350
+ => [...]
351
+
352
+ profile.encodings.status("success")
353
+ or
354
+ profile.encodings.all(:status => "success")
355
+ => [...]
356
+
357
+ ### Using multiple clouds
358
+
359
+ By default Cloud.id uses options defined with: Panda.configure do .. end
360
+
361
+ cloud_one = Panda::Cloud.find("cloud_id_1")
362
+ cloud_two = Panda::Cloud.find("cloud_id_2")
363
+
364
+ cloud_one.profiles
365
+ cloud_two.profiles.find("profile_id")
366
+
367
+ cloud_two.videos
368
+ cloud_two.videos.status("success")
369
+ cloud_two.videos.all(:status => "success")
370
+ cloud_two.videos.all(:page => 2)
371
+
372
+ cloud_one.videos.find("video_id_1")
373
+ cloud_two.videos.find("video_id_2")
374
+
375
+ cloud_two.profiles
376
+ cloud_two.profiles.create(:preset_name => "h264")
377
+ cloud_one.videos.create(:command => "ffmpeg -i $input_file$ -y $output_file$", ....)
378
+
379
+ You can also connect directly using Cloud.find
380
+
381
+ cloud = Panda::Cloud.find("cloud_id_1", {:access_key => ..., :secret_key => ... })
382
+
383
+
384
+ ## Generating signatures
385
+
386
+ All requests to your Panda cloud are signed using HMAC-SHA256, based on a timestamp and your Panda secret key. This is handled transparently. However, sometimes you will want to generate only this signature, in order to make a request by means other than this library. This is the case when using the [JavaScript panda_uploader](http://github.com/newbamboo/panda_uploader).
387
+
388
+ To do this, a method `signed_params()` is supported:
389
+
390
+ Panda.signed_params('POST', '/videos.json')
391
+ # => {'access_key' => '8df50af4-074f-11df-b278-1231350015b1',
392
+ # 'cloud_id' => 'your-cloud-id',
393
+ # 'signature' => 'LejCdm0O83+jk6/Q5SfGmk14WTO1pB6Sh6Z5eA2w5C0=',
394
+ # 'timestamp' => '2010-02-26T15:01:46.221513'}
395
+
396
+ Panda.signed_params('GET', '/videos.json', {'some_params' => 'some_value'})
397
+ # => {'access_key' => '8df50af4-074f-11df-b278-1231350015b1',
398
+ # 'cloud_id' => 'your-cloud-id',
399
+ # 'signature' => 'uHnGZ+kI9mT3C4vW71Iop9z2N7UKCv38v2l2dvREUIQ=',
400
+ # 'some_param' => 'some_value',
401
+ # 'timestamp' => '2010-02-26T15:04:27.039620'}
402
+
403
+ ## Old Panda way, still works
404
+
405
+ ### Creating an instance of the client
406
+
407
+ Panda.connect!({
408
+ :cloud_id => 'cloud_id',
409
+ :access_key => 'access_key',
410
+ :secret_key => 'secret_key',
411
+ :api_host => 'api.pandastream.com' # This may change depending on the region
412
+ })
413
+
414
+ ### Posting a video
415
+
416
+ Panda.post('/videos.json', {:file => File.new("panda.mp4")}) # Note that you will need a movie file to test this. You can grab http://panda-test-harness-videos.s3.amazonaws.com/panda.mp4
417
+
418
+ Panda.post('/videos.json', {:source_url => 'http://www.example.com/original_video.mp4'})
419
+ =>{"duration"=>nil,
420
+ "created_at"=>"2010/01/15 14:48:42 +0000",
421
+ "original_filename"=>"panda.mp4",
422
+ "updated_at"=>"2010/01/15 14:48:42 +0000",
423
+ "source_url"=>"http://www.example.com/original_video.mp4",
424
+ "id"=>"12fce296-01e5-11df-ae37-12313902cc92",
425
+ "extname"=>".mp4",
426
+ "audio_codec"=>nil,
427
+ "height"=>nil,
428
+ "upload_redirect_url"=>nil,
429
+ "fps"=>nil,
430
+ "video_codec"=>nil,
431
+ "status"=>"processing",
432
+ "width"=>nil}
433
+
434
+ ### Getting all videos
435
+
436
+ Panda.get('/videos.json')
437
+ => [{"duration"=>14010,
438
+ "created_at"=>"2010/01/13 16:45:29 +0000",
439
+ "original_filename"=>"panda.mp4",
440
+ "updated_at"=>"2010/01/13 16:45:35 +0000",
441
+ "source_url"=>"http://www.example.com/original_video.mp4",
442
+ "id"=>"0ee6b656-0063-11df-a433-1231390041c1",
443
+ "extname"=>".mp4",
444
+ "audio_codec"=>"aac",
445
+ "height"=>240,
446
+ "upload_redirect_url"=>nil,
447
+ "fps"=>29,
448
+ "video_codec"=>"h264",
449
+ "status"=>"success",
450
+ "width"=>300}]
451
+
452
+ ### Getting video encodings
453
+
454
+ Panda.get('/videos/0ee6b656-0063-11df-a433-1231390041c1/encodings.json')
455
+ => [{"encoder_id"=>nil,
456
+ "created_at"=>"2010/01/13 16:45:30 +0000",
457
+ "video_id"=>"0ee6b656-0063-11df-a433-1231390041c1",
458
+ "video_url"=>
459
+ "http://s3.amazonaws.com/panda-videos/0f815986-0063-11df-a433-1231390041c1.flv",
460
+ "started_encoding_at"=>"2010/01/13 16:47:35 +0000",
461
+ "updated_at"=>"2010/01/13 16:47:40 +0000",
462
+ "extname"=>".flv",
463
+ "encoding_progress"=>87,
464
+ "encoding_time"=>3,
465
+ "id"=>"0f815986-0063-11df-a433-1231390041c1",
466
+ "height"=>240,
467
+ "status"=>"success",
468
+ "profile_id"=>"00182830-0063-11df-8c8a-1231390041c1",
469
+ "width"=>300}]
470
+
471
+ ### Deleting an encoding
472
+
473
+ Panda.delete('/encodings/0f815986-0063-11df-a433-1231390041c1.json')
474
+
475
+ ### Deleting a video
476
+
477
+ Panda.delete('/videos/0ee6b656-0063-11df-a433-1231390041c1.json')
478
+
479
+ ## Hash or JSON
480
+ Since Panda 0.6, PandaGem returns a Hash by default. If you want PandaGem to return JSON do the following:
481
+
482
+ Panda.connect!({
483
+ :cloud_id => 'cloud_id',
484
+ :access_key => 'access_key',
485
+ :secret_key => 'secret_key',
486
+ :format => 'json'
487
+ })
488
+
489
+
490
+ ## Use bundler to setup the test environment (1.0)
491
+
492
+ bundler install
493
+ rake spec
494
+
495
+
496
+ Copyright (c) 2009-2010 New Bamboo. See LICENSE for details.