vzaar 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Changelog +5 -0
- data/LICENSE +0 -0
- data/README +12 -0
- data/demo.rb +37 -0
- data/lib/rails/controllers/vzaar_controller.rb +37 -0
- data/lib/rails/views/view_helpers.rb +177 -0
- data/lib/vzaar/account_type.rb +31 -0
- data/lib/vzaar/base.rb +455 -0
- data/lib/vzaar/errors.rb +6 -0
- data/lib/vzaar/signature.rb +31 -0
- data/lib/vzaar/user.rb +28 -0
- data/lib/vzaar/video.rb +38 -0
- data/lib/vzaar/video_details.rb +33 -0
- data/lib/vzaar.rb +67 -0
- data/rails_generators/vzaar_uploader/USAGE +1 -0
- data/rails_generators/vzaar_uploader/templates/flash/swfupload.swf +0 -0
- data/rails_generators/vzaar_uploader/templates/flash/swfupload_fp9.swf +0 -0
- data/rails_generators/vzaar_uploader/templates/images/cancelbutton.gif +0 -0
- data/rails_generators/vzaar_uploader/templates/javascripts/fileprogress.js +197 -0
- data/rails_generators/vzaar_uploader/templates/javascripts/handlers.js +259 -0
- data/rails_generators/vzaar_uploader/templates/javascripts/json_parse.js +345 -0
- data/rails_generators/vzaar_uploader/templates/javascripts/swfupload.js +1132 -0
- data/rails_generators/vzaar_uploader/templates/javascripts/swfupload.queue.js +98 -0
- data/rails_generators/vzaar_uploader/templates/stylesheets/swfupload.css +335 -0
- data/rails_generators/vzaar_uploader/templates/views/uploader.html.erb +15 -0
- data/rails_generators/vzaar_uploader/vzaar_uploader_generator.rb +52 -0
- metadata +114 -0
data/lib/vzaar/base.rb
ADDED
@@ -0,0 +1,455 @@
|
|
1
|
+
module Vzaar
|
2
|
+
|
3
|
+
# You can use Vzaar::Base class for accessing and managing your resources on vzaar.
|
4
|
+
class Base
|
5
|
+
|
6
|
+
# When creating a Vzaar::Base instance you can (but don't have to) specify
|
7
|
+
# login and application_token. However if you don't specify them you
|
8
|
+
# won't be able to perform authenticated calls.
|
9
|
+
#
|
10
|
+
# You can also specify server different from live server, e.g. sandbox.vzaar.com,
|
11
|
+
# if you're just doing testing. Additionally you can pass your logger as the
|
12
|
+
# :logger option. By default the log/debug info is written to the standard output.
|
13
|
+
#
|
14
|
+
# The options can be read from environment variables. Just set VZAAR_LOGIN,
|
15
|
+
# VZAAR_APPLICATION_TOKEN and/or VZAAR_SERVER and you don't have to worry about
|
16
|
+
# passing the options to the initializer.
|
17
|
+
#
|
18
|
+
# Usage:
|
19
|
+
# * vzaar = Vzaar::Base.new
|
20
|
+
# * vzaar = Vzaar::Base.new :login => 'your_vzaar_login', :application_token => 'your_very_long_application_token'
|
21
|
+
# * vzaar = Vzaar::Base.new :login => 'your_vzaar_login', :application_token => 'your_app_token', :server => 'sandbox.vzaar.com'
|
22
|
+
# * vzaar = Vzaar::Base.new :server => 'sandbox.vzaar.com', :logger => your_logger
|
23
|
+
# * vzaar = Vzaar::Base.new :logger => your_logger
|
24
|
+
def initialize(options= {})
|
25
|
+
login = options[:login] || ENV['VZAAR_LOGIN'] || ''
|
26
|
+
application_token = options[:application_token] || ENV['VZAAR_APPLICATION_TOKEN'] || ''
|
27
|
+
server = options[:server] || ENV['VZAAR_SERVER'] || VZAAR_LIVE_SERVER
|
28
|
+
@logger = options[:logger] || Logger.new(STDOUT)
|
29
|
+
|
30
|
+
server.gsub! 'http://', ''
|
31
|
+
server.gsub! 'https://', ''
|
32
|
+
consumer = OAuth::Consumer.new '', '', { :site => "http://#{server}" }
|
33
|
+
@public_connection = OAuth::AccessToken.new consumer, '', ''
|
34
|
+
consumer = OAuth::Consumer.new '', '', { :site => "https://#{server}" }
|
35
|
+
if login.length > 0 and application_token.length > 0
|
36
|
+
@auth_connection = OAuth::AccessToken.new consumer, login, application_token
|
37
|
+
else
|
38
|
+
# Authenticated requests won't be possible
|
39
|
+
@auth_connection = nil
|
40
|
+
log_info "Authenticated calls won't be possible"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Test method for authentication. Returns a login of an authenticated user.
|
45
|
+
#
|
46
|
+
# Usage:
|
47
|
+
# * my_login = vzaar.whoami
|
48
|
+
def whoami
|
49
|
+
result = nil
|
50
|
+
auth_connection(HTTP_GET, '/api/test/whoami') do |xml|
|
51
|
+
doc = REXML::Document.new xml
|
52
|
+
result = doc.elements['vzaar-api/test/login'].text
|
53
|
+
end
|
54
|
+
result
|
55
|
+
end
|
56
|
+
|
57
|
+
# Gets the details of an account type.
|
58
|
+
#
|
59
|
+
# Usage:
|
60
|
+
# * account_type = vzaar.account_type 1
|
61
|
+
# * title = vzaar.account_type(1).title
|
62
|
+
# * bandwidth = vzaar.account_type(1).bandwidth
|
63
|
+
def account_type(account_type_id)
|
64
|
+
result = nil
|
65
|
+
public_connection(HTTP_GET, "/api/accounts/#{account_type_id}.xml") do |xml|
|
66
|
+
result = AccountType.new xml
|
67
|
+
end
|
68
|
+
result
|
69
|
+
end
|
70
|
+
|
71
|
+
# Gets a user public details. Whitelabel users can retrive their details by
|
72
|
+
# using the method with 'authenticated' option on.
|
73
|
+
#
|
74
|
+
# Usage:
|
75
|
+
# * me = vzaar.user_details 'some_login' (this works only if 'some_login' is not a protected resource)
|
76
|
+
# * me = vzaar.user_details 'your_login', true ('your_login' must be the same as the one provided for Vzaar::Base.new method in order to authorize on server.)
|
77
|
+
#
|
78
|
+
# Note: even if you created an authorized instance of Vzaar::Base class
|
79
|
+
# (by specifying login and application token in Vzaar::Base.new), you
|
80
|
+
# need to set the 'authenticated' param to true in order to perform
|
81
|
+
# authenticated call.
|
82
|
+
def user_details(login, authenticated = false)
|
83
|
+
result = nil
|
84
|
+
if authenticated
|
85
|
+
auth_connection(HTTP_GET, "/api/users/#{login}.xml") do |xml|
|
86
|
+
result = User.new xml
|
87
|
+
end
|
88
|
+
else
|
89
|
+
public_connection(HTTP_GET, "/api/users/#{login}.xml") do |xml|
|
90
|
+
result = User.new xml
|
91
|
+
end
|
92
|
+
end
|
93
|
+
result
|
94
|
+
end
|
95
|
+
|
96
|
+
# Gets a list of a user's active videos along with it's relevant metadata.
|
97
|
+
# Set 'authenticated' option to true to retrieve private videos.
|
98
|
+
#
|
99
|
+
# Usage:
|
100
|
+
# * videos = vzaar.video_list 'your_login' (gets your public videos)
|
101
|
+
# * videos = vzaar.video_list 'some_other_login' (gets public videos of some_other_login)
|
102
|
+
# * videos = vzaar.video_list 'your_login', true (gets all your videos, provided your_login is the one you provided for Vzaar::Base initializer)
|
103
|
+
# * videos = vzaar.video_list 'some_other_login', true (gets public videos of some_other_login - you cannot access other users' private videos)
|
104
|
+
#
|
105
|
+
# Note: even if you created an authorized instance of Vzaar::Base class
|
106
|
+
# if you don't set the 'authenticated' param to true you will receive
|
107
|
+
# only public videos.
|
108
|
+
def video_list(login, authenticated = false)
|
109
|
+
result = []
|
110
|
+
response = nil
|
111
|
+
if authenticated
|
112
|
+
response = auth_connection(HTTP_GET, "/api/#{login}/videos.xml")
|
113
|
+
else
|
114
|
+
response = public_connection(HTTP_GET, "/api/#{login}/videos.xml")
|
115
|
+
end
|
116
|
+
if response and response.body
|
117
|
+
doc = REXML::Document.new response.body
|
118
|
+
videos = doc.elements['videos']
|
119
|
+
videos.elements.each('video') do |video|
|
120
|
+
result << Video.new(video.to_s)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
result
|
124
|
+
end
|
125
|
+
|
126
|
+
# Gets video details, inlcuding embed code. Use 'authenticated' option to
|
127
|
+
# retrieve details of private video.
|
128
|
+
#
|
129
|
+
# Usage:
|
130
|
+
# * video = vzaar.video_details 1234 (1234 must be a public video)
|
131
|
+
# * video = vzaar.video_details 1234, true (1234 can be a private video but you must the owner)
|
132
|
+
#
|
133
|
+
# Note: even if you create an authorized instance of Vzaar::Base class
|
134
|
+
# if you don't set the 'authenticated' param to true you will not be able
|
135
|
+
# to retrieve data for private video.
|
136
|
+
def video_details(video_id, authenticated = false)
|
137
|
+
result = nil
|
138
|
+
if authenticated
|
139
|
+
auth_connection(HTTP_GET, "/api/videos/#{video_id}.xml") do |xml|
|
140
|
+
result = VideoDetails.new xml
|
141
|
+
end
|
142
|
+
else
|
143
|
+
public_connection(HTTP_GET, "/api/videos/#{video_id}.xml") do |xml|
|
144
|
+
result = VideoDetails.new xml
|
145
|
+
end
|
146
|
+
end
|
147
|
+
result
|
148
|
+
end
|
149
|
+
|
150
|
+
# Deletes a video from a users account. Use either 'DELETE' or 'POST' method.
|
151
|
+
# You must be the owner of the video in order to authorize on the server.
|
152
|
+
#
|
153
|
+
# Usage:
|
154
|
+
# * vzaar.delete_video 1234 (uses 'DELETE' method)
|
155
|
+
# * vzaar.delete_video 1234, 'POST'
|
156
|
+
def delete_video(video_id, method = HTTP_DELETE)
|
157
|
+
if method == HTTP_DELETE
|
158
|
+
auth_connection method, "/api/videos/#{video_id}.xml"
|
159
|
+
else
|
160
|
+
request_xml = %{
|
161
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
162
|
+
<vzaar-api>
|
163
|
+
<_method>delete</_method>
|
164
|
+
</vzaar-api>
|
165
|
+
}
|
166
|
+
auth_connection method, "/api/videos/#{video_id}.xml", request_xml
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
# Edits a video title and description. Use either 'PUT' or 'POST' method.
|
171
|
+
#
|
172
|
+
# Usage:
|
173
|
+
# * vzaar.edit_video 1234, 'new title', 'new desc' (uses 'PUT' method)
|
174
|
+
# * vzaar.edit_video 1234, 'new title', 'new desc', 'POST'
|
175
|
+
def edit_video(video_id, title, description, method = HTTP_PUT)
|
176
|
+
request_xml = %{
|
177
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
178
|
+
<vzaar-api>
|
179
|
+
}
|
180
|
+
request_xml += %{<_method>put</_method>} if method != HTTP_PUT
|
181
|
+
request_xml += %{
|
182
|
+
<video>
|
183
|
+
<title>#{title}</title>
|
184
|
+
<description>#{description}</description >
|
185
|
+
</video>
|
186
|
+
</vzaar-api>
|
187
|
+
}
|
188
|
+
auth_connection HTTP_PUT, "/api/videos/#{video_id}.xml", request_xml
|
189
|
+
end
|
190
|
+
|
191
|
+
# Provides a signature which is required to upload a video directly to S3 bucket.
|
192
|
+
# Options:
|
193
|
+
# * success_action_redirect - when sending files to S3 you can be redirected to a given url on success. You'll need to specify the url when requesting a signature. Vzaar API server will attach a guid to it and return full url in the response. You'll need to specify the full url later when uploading a video in order to get authorized on S3.
|
194
|
+
# * include_metadata - if you set the param to true, then when uploading a video you can and have to(!) send metadata to S3 along with your video. The names of the metadata must be: 'x-amz-meta-title' and 'x-amz-meta-profile'. None of them can be omitted even if empty. Vzaar doesn't restric the values of the metadata in any way. If include_metadata is false, which is the default behaviour, no metadata can be send to S3.
|
195
|
+
# * flash_request - adds flash specific params to the signature
|
196
|
+
#
|
197
|
+
# Usage:
|
198
|
+
# * vzaar.signature
|
199
|
+
# * vzaar.signature :success_action_redirect => 'http://my.domain.com/using_vzaar'
|
200
|
+
# * vzaar.signature :success_action_redirect => 'http://my.domain.com/using_vzaar', :include_metadata => true
|
201
|
+
# * vzaar.signature :include_metadata => true
|
202
|
+
# * vzaar.signature :flash_request => true
|
203
|
+
def signature(options = {})
|
204
|
+
signature = nil
|
205
|
+
url = '/api/videos/signature'
|
206
|
+
if options[:success_action_redirect]
|
207
|
+
url += "?success_action_redirect=#{options[:success_action_redirect]}"
|
208
|
+
end
|
209
|
+
if options[:include_metadata]
|
210
|
+
url += url.include?('?') ? '&' : '?'
|
211
|
+
url += "include_metadata=yes"
|
212
|
+
end
|
213
|
+
if options[:flash_request]
|
214
|
+
url += url.include?('?') ? '&' : '?'
|
215
|
+
url += "flash_request=yes"
|
216
|
+
end
|
217
|
+
auth_connection HTTP_GET, url do |xml|
|
218
|
+
signature = Signature.new xml
|
219
|
+
end
|
220
|
+
signature
|
221
|
+
end
|
222
|
+
|
223
|
+
# Tells vzaar that you have uploaded a video to S3 and now you want to
|
224
|
+
# register it in vzaar. This method is called automatically from within
|
225
|
+
# the upload_video method.
|
226
|
+
#
|
227
|
+
# Usage:
|
228
|
+
# * vzaar.process_video :guid => signature.guid, :title => 'Some title', :description => 'Some description', :profile => 1
|
229
|
+
def process_video(options = {})
|
230
|
+
request_xml = %{
|
231
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
232
|
+
<vzaar-api>
|
233
|
+
<video>
|
234
|
+
<guid>#{options[:guid]}</guid>
|
235
|
+
<title>#{options[:title]}</title>
|
236
|
+
<description>#{options[:description]}</description>
|
237
|
+
<profile>#{options[:profile]}</profile>
|
238
|
+
</video>
|
239
|
+
</vzaar-api>
|
240
|
+
}
|
241
|
+
auth_connection HTTP_POST, '/api/videos', request_xml
|
242
|
+
end
|
243
|
+
|
244
|
+
# Uploads a video to vzaar.
|
245
|
+
#
|
246
|
+
# Usage:
|
247
|
+
# * vzaar.upload_video '/home/me/video.mp4', 'some title', 'some desc', '1'
|
248
|
+
def upload_video(path, title, description, profile)
|
249
|
+
# Get signature
|
250
|
+
sig = signature
|
251
|
+
@logger.debug "Uploading..."
|
252
|
+
# Upload to S3
|
253
|
+
res = upload_to_s3 sig.acl, sig.bucket, sig.policy, sig.aws_access_key,
|
254
|
+
sig.signature, sig.key, path
|
255
|
+
if res
|
256
|
+
@logger.debug "Upload complete"
|
257
|
+
# And process in vzaar
|
258
|
+
process_video :guid => sig.guid, :title => title,
|
259
|
+
:description => description, :profile => profile
|
260
|
+
else
|
261
|
+
@logger.debug "Upload to s3 failed"
|
262
|
+
return nil
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
private
|
267
|
+
|
268
|
+
# Performs the public connection
|
269
|
+
def public_connection(method, url, xml = '', &block)
|
270
|
+
res = nil
|
271
|
+
begin
|
272
|
+
case method
|
273
|
+
when "GET"
|
274
|
+
res = @public_connection.get url
|
275
|
+
when "POST"
|
276
|
+
if xml and xml.length > 0
|
277
|
+
res = @public_connection.post url, xml,
|
278
|
+
{ 'Content-Type' => 'application/xml' }
|
279
|
+
else
|
280
|
+
res = @public_connection.post url
|
281
|
+
end
|
282
|
+
when "PUT"
|
283
|
+
if xml and xml.length > 0
|
284
|
+
res = @public_connection.put url, xml,
|
285
|
+
{ 'Content-Type' => 'application/xml' }
|
286
|
+
else
|
287
|
+
res = @public_connection.put url
|
288
|
+
end
|
289
|
+
when "DELETE"
|
290
|
+
if xml and xml.length > 0
|
291
|
+
res = @public_connection.delete url, xml,
|
292
|
+
{ 'Content-Type' => 'application/xml' }
|
293
|
+
else
|
294
|
+
res = @public_connection.delete url
|
295
|
+
end
|
296
|
+
else
|
297
|
+
handle_exception 'unknown_method'
|
298
|
+
end
|
299
|
+
case res.code
|
300
|
+
when HTTP_OK
|
301
|
+
yield res.body if block_given?
|
302
|
+
when HTTP_CREATED
|
303
|
+
yield res.body if block_given?
|
304
|
+
when HTTP_FORBIDDEN
|
305
|
+
handle_exception 'protected_resource'
|
306
|
+
when HTTP_NOT_FOUND
|
307
|
+
handle_exception 'resource_not_found'
|
308
|
+
when HTTP_BAD_GATEWAY
|
309
|
+
handle_exception 'server_not_responding'
|
310
|
+
else
|
311
|
+
handle_exception 'unknown'
|
312
|
+
end
|
313
|
+
rescue Exception => e
|
314
|
+
raise e if e.is_a? VzaarError
|
315
|
+
handle_exception 'unknown', e.message
|
316
|
+
end
|
317
|
+
res
|
318
|
+
end
|
319
|
+
|
320
|
+
# Performs the authenticated connection
|
321
|
+
def auth_connection(method, url, xml = '', &block)
|
322
|
+
res = nil
|
323
|
+
begin
|
324
|
+
if @auth_connection
|
325
|
+
case method
|
326
|
+
when "GET"
|
327
|
+
res = @auth_connection.get url
|
328
|
+
when "POST"
|
329
|
+
if xml and xml.length > 0
|
330
|
+
res = @auth_connection.post url, xml,
|
331
|
+
{ 'Content-Type' => 'application/xml' }
|
332
|
+
else
|
333
|
+
res = @auth_connection.post url
|
334
|
+
end
|
335
|
+
when "PUT"
|
336
|
+
if xml and xml.length > 0
|
337
|
+
res = @auth_connection.put url, xml,
|
338
|
+
{ 'Content-Type' => 'application/xml' }
|
339
|
+
else
|
340
|
+
res = @auth_connection.put url
|
341
|
+
end
|
342
|
+
when "DELETE"
|
343
|
+
if xml and xml.length > 0
|
344
|
+
res = @auth_connection.delete url, xml,
|
345
|
+
{ 'Content-Type' => 'application/xml' }
|
346
|
+
else
|
347
|
+
res = @auth_connection.delete url
|
348
|
+
end
|
349
|
+
else
|
350
|
+
unknown_method
|
351
|
+
end
|
352
|
+
case res.code
|
353
|
+
when HTTP_OK
|
354
|
+
yield res.body if block_given?
|
355
|
+
when HTTP_CREATED
|
356
|
+
yield res.body if block_given?
|
357
|
+
when HTTP_BAD_GATEWAY
|
358
|
+
handle_exception 'server_not_responding'
|
359
|
+
else
|
360
|
+
handle_exception 'not_authorized'
|
361
|
+
end
|
362
|
+
else
|
363
|
+
handle_exception 'authorization_info_not_provided'
|
364
|
+
end
|
365
|
+
rescue Exception => e
|
366
|
+
raise e if e.is_a? VzaarError
|
367
|
+
handle_exception 'unknown', e.message
|
368
|
+
end
|
369
|
+
res
|
370
|
+
end
|
371
|
+
|
372
|
+
def upload_to_s3(acl, bucket, policy, aws_access_key, signature, key, file_path)
|
373
|
+
client = HTTPClient.new
|
374
|
+
url = "https://#{bucket}.s3.amazonaws.com/"
|
375
|
+
begin
|
376
|
+
file = File.open file_path
|
377
|
+
res = client.post url, [
|
378
|
+
['acl', acl],
|
379
|
+
['bucket', bucket],
|
380
|
+
['success_action_status', '201'],
|
381
|
+
['policy', policy],
|
382
|
+
['AWSAccessKeyId', aws_access_key],
|
383
|
+
['signature', signature],
|
384
|
+
['key', key],
|
385
|
+
['file', file]
|
386
|
+
]
|
387
|
+
rescue Exception => e
|
388
|
+
file.close if file
|
389
|
+
handle_exception 'unknown', e.message
|
390
|
+
end
|
391
|
+
file.close if file
|
392
|
+
if res.status_code == 201
|
393
|
+
return true
|
394
|
+
else
|
395
|
+
return false
|
396
|
+
end
|
397
|
+
end
|
398
|
+
|
399
|
+
def upload_to_s3_curl(acl, bucket, policy, aws_access_key, signature, key, file_path)
|
400
|
+
require 'curb'
|
401
|
+
acl_field = Curl::PostField.content 'acl', acl
|
402
|
+
bucket_field = Curl::PostField.content 'bucket', bucket
|
403
|
+
success_action_status_field = Curl::PostField.content 'success_action_status',
|
404
|
+
'201'
|
405
|
+
policy_field = Curl::PostField.content 'policy', policy
|
406
|
+
aws_access_key_field = Curl::PostField.content 'AWSAccessKeyId', aws_access_key
|
407
|
+
signature_field = Curl::PostField.content 'signature', signature
|
408
|
+
key_field = Curl::PostField.content 'key', key
|
409
|
+
file_field = Curl::PostField.file 'file', file_path
|
410
|
+
curl = Curl::Easy.new "https://#{bucket}.s3.amazonaws.com/"
|
411
|
+
curl.multipart_form_post = true
|
412
|
+
begin
|
413
|
+
curl.http_post acl_field, bucket_field, success_action_status_field, policy_field,
|
414
|
+
aws_access_key_field, signature_field, key_field, file_field
|
415
|
+
rescue Exception => e
|
416
|
+
handle_exception 'unknown', e.message
|
417
|
+
end
|
418
|
+
if curl.response_code == 201
|
419
|
+
return true
|
420
|
+
else
|
421
|
+
return false
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
def log_info(message)
|
426
|
+
@logger.info message
|
427
|
+
end
|
428
|
+
|
429
|
+
def handle_exception(type, message = '')
|
430
|
+
case type
|
431
|
+
when 'not_authorized':
|
432
|
+
message = "You have not been authorized on the server. " +
|
433
|
+
"Please check your login and application token."
|
434
|
+
when 'authorization_info_not_provided':
|
435
|
+
message = 'You need to provide login and application token to perform ' +
|
436
|
+
'to perform this action.'
|
437
|
+
when 'server_not_responding':
|
438
|
+
message = "The server you're trying to connect to is not responding."
|
439
|
+
when 'protected_resource':
|
440
|
+
message = "The resource is protected and you have not been authorized " +
|
441
|
+
"to access it."
|
442
|
+
when 'resource_not_found':
|
443
|
+
message = "The resource has not been found on the server."
|
444
|
+
when 'unknown_method':
|
445
|
+
message = "The method used for connecting is not a proper HTTP method."
|
446
|
+
else
|
447
|
+
message = "Unknown error occured when accessing the server: " + message
|
448
|
+
end
|
449
|
+
@logger.error message
|
450
|
+
raise VzaarError.new message
|
451
|
+
end
|
452
|
+
|
453
|
+
end
|
454
|
+
|
455
|
+
end
|
data/lib/vzaar/errors.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module Vzaar
|
2
|
+
|
3
|
+
class Signature
|
4
|
+
|
5
|
+
attr_accessor :xml, :acl, :bucket, :policy, :key, :aws_access_key, :guid, :signature,
|
6
|
+
:success_action_redirect, :title, :profile
|
7
|
+
|
8
|
+
def initialize(xml)
|
9
|
+
@xml = xml
|
10
|
+
doc = REXML::Document.new xml
|
11
|
+
@acl = doc.elements['vzaar-api/acl'] ? doc.elements['vzaar-api/acl'].text : ''
|
12
|
+
@bucket = doc.elements['vzaar-api/bucket'] ?
|
13
|
+
doc.elements['vzaar-api/bucket'].text : ''
|
14
|
+
@policy = doc.elements['vzaar-api/policy'] ?
|
15
|
+
doc.elements['vzaar-api/policy'].text : ''
|
16
|
+
@key = doc.elements['vzaar-api/key'] ?
|
17
|
+
doc.elements['vzaar-api/key'].text : ''
|
18
|
+
@aws_access_key = doc.elements['vzaar-api/accesskeyid'] ?
|
19
|
+
doc.elements['vzaar-api/accesskeyid'].text : ''
|
20
|
+
@guid = doc.elements['vzaar-api/guid'] ?
|
21
|
+
doc.elements['vzaar-api/guid'].text : ''
|
22
|
+
@signature = doc.elements['vzaar-api/signature'] ?
|
23
|
+
doc.elements['vzaar-api/signature'].text : ''
|
24
|
+
@success_action_redirect = doc.elements['vzaar-api/success_action_redirect'] ?
|
25
|
+
doc.elements['vzaar-api/success_action_redirect'].text : nil
|
26
|
+
@title = doc.elements['vzaar-api/title'] ? '' : nil
|
27
|
+
@profile = doc.elements['vzaar-api/profile'] ? '' : nil
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
data/lib/vzaar/user.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Vzaar
|
2
|
+
|
3
|
+
class User
|
4
|
+
|
5
|
+
attr_accessor :xml, :version, :id, :name, :url, :account_type_id, :created_at,
|
6
|
+
:video_count, :play_count
|
7
|
+
|
8
|
+
def initialize(xml)
|
9
|
+
@xml = xml
|
10
|
+
doc = REXML::Document.new xml
|
11
|
+
@version = doc.elements['user/version'] ? doc.elements['user/version'].text : ''
|
12
|
+
@id = doc.elements['user/author_id'] ? doc.elements['user/author_id'].text : ''
|
13
|
+
@name = doc.elements['user/author_name'] ?
|
14
|
+
doc.elements['user/author_name'].text : ''
|
15
|
+
@url = doc.elements['user/author_url'] ? doc.elements['user/author_url'].text : ''
|
16
|
+
@account_type_id = doc.elements['user/author_account'] ?
|
17
|
+
doc.elements['user/author_account'].text : ''
|
18
|
+
@created_at = doc.elements['user/created_at'] ?
|
19
|
+
doc.elements['user/created_at'].text : ''
|
20
|
+
@video_count = doc.elements['user/video_count'] ?
|
21
|
+
doc.elements['user/video_count'].text : ''
|
22
|
+
@play_count = doc.elements['user/play_count'] ?
|
23
|
+
doc.elements['user/play_count'].text : ''
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
data/lib/vzaar/video.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
module Vzaar
|
2
|
+
|
3
|
+
class Video
|
4
|
+
|
5
|
+
attr_accessor :xml, :version, :id, :title, :description, :created_at, :url,
|
6
|
+
:thumbnail_url, :play_count, :author_name, :author_url, :author_account_type_id,
|
7
|
+
:video_count, :duration
|
8
|
+
|
9
|
+
def initialize(xml)
|
10
|
+
@xml = xml
|
11
|
+
doc = REXML::Document.new xml
|
12
|
+
@version = doc.elements['video/version'] ? doc.elements['video/version'].text : ''
|
13
|
+
@id = doc.elements['video/id'] ? doc.elements['video/id'].text : ''
|
14
|
+
@title = doc.elements['video/title'] ? doc.elements['video/title'].text : ''
|
15
|
+
@description = doc.elements['video/description'] ?
|
16
|
+
doc.elements['video/description'].text : ''
|
17
|
+
@create_at = doc.elements['video/created_at'] ?
|
18
|
+
doc.elements['video/created_at'].text : ''
|
19
|
+
@url = doc.elements['video/url'] ? doc.elements['video/url'].text : ''
|
20
|
+
@thumbnail_url = doc.elements['video/thumbnail_url'] ?
|
21
|
+
doc.elements['video/thumbnail_url'].text : ''
|
22
|
+
@play_count = doc.elements['video/play_count'] ?
|
23
|
+
doc.elements['video/play_count'].text : ''
|
24
|
+
@author_name = doc.elements['video/user/author_name'] ?
|
25
|
+
doc.elements['video/user/author_name'].text : ''
|
26
|
+
@author_url = doc.elements['video/user/author_url'] ?
|
27
|
+
doc.elements['video/user/author_url'].text : ''
|
28
|
+
@author_account_type_id = doc.elements['video/user/author_account'] ?
|
29
|
+
doc.elements['video/user/author_account'].text : ''
|
30
|
+
@video_count = doc.elements['video/user/video_count'] ?
|
31
|
+
doc.elements['video/user/video_count'].text : ''
|
32
|
+
@duration = doc.elements['video/duration'] ?
|
33
|
+
doc.elements['video/duration'].text : ''
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Vzaar
|
2
|
+
|
3
|
+
class VideoDetails
|
4
|
+
|
5
|
+
attr_accessor :xml, :version, :type, :title, :author_name, :author_url,
|
6
|
+
:author_account_type_id, :provider_name, :provider_url, :html, :height,
|
7
|
+
:width, :borderless, :duration
|
8
|
+
|
9
|
+
def initialize(xml)
|
10
|
+
@xml = xml
|
11
|
+
doc = REXML::Document.new xml
|
12
|
+
@version = doc.elements['oembed/version'] ?
|
13
|
+
doc.elements['oembed/version'].text : ''
|
14
|
+
@type = doc.elements['oembed/type'] ? doc.elements['oembed/type'].text : ''
|
15
|
+
@title = doc.elements['oembed/title'] ? doc.elements['oembed/title'].text : ''
|
16
|
+
@author_name = doc.elements['oembed/author_name'] ?
|
17
|
+
doc.elements['oembed/author_name'].text : ''
|
18
|
+
@author_url = doc.elements['oembed/author_url'] ?
|
19
|
+
doc.elements['oembed/author_url'].text : ''
|
20
|
+
@author_account_type_id = doc.elements['oembed/author_account'] ?
|
21
|
+
doc.elements['oembed/author_account'].text : ''
|
22
|
+
@provider_name = doc.elements['oembed/provider_name'] ?
|
23
|
+
doc.elements['oembed/provider_name'].text : ''
|
24
|
+
@provider_url = doc.elements['oembed/provider_url'] ?
|
25
|
+
doc.elements['oembed/provider_url'].text : ''
|
26
|
+
@html = doc.elements['oembed/html'] ? doc.elements['oembed/html'].texts[1] : ''
|
27
|
+
@width = doc.elements['oembed/width'] ? doc.elements['oembed/width'].text : ''
|
28
|
+
@height = doc.elements['oembed/height'] ? doc.elements['oembed/height'].text : ''
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
data/lib/vzaar.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# == Vzaar API gem
|
2
|
+
# The Vzaar API provides means to access and manage resources on http://vzaar.com
|
3
|
+
#
|
4
|
+
# See README file for installation details.
|
5
|
+
#
|
6
|
+
# Author:: Mariusz Lusiak <mailto:mariusz@applicake.com>
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'httpclient'
|
10
|
+
require 'logger'
|
11
|
+
require 'oauth/consumer'
|
12
|
+
require 'rexml/document'
|
13
|
+
require 'vzaar/account_type'
|
14
|
+
require 'vzaar/base'
|
15
|
+
require 'vzaar/errors'
|
16
|
+
require 'vzaar/user'
|
17
|
+
require 'vzaar/signature'
|
18
|
+
require 'vzaar/video'
|
19
|
+
require 'vzaar/video_details'
|
20
|
+
|
21
|
+
module Vzaar
|
22
|
+
|
23
|
+
VZAAR_LIVE_SERVER = 'vzaar.com'
|
24
|
+
|
25
|
+
HTTP_GET = 'GET'
|
26
|
+
HTTP_POST = 'POST'
|
27
|
+
HTTP_DELETE = 'DELETE'
|
28
|
+
HTTP_PUT = 'PUT'
|
29
|
+
|
30
|
+
HTTP_OK = "200"
|
31
|
+
HTTP_CREATED = "201"
|
32
|
+
|
33
|
+
HTTP_FORBIDDEN = "403"
|
34
|
+
HTTP_NOT_FOUND = "404"
|
35
|
+
|
36
|
+
HTTP_BAD_GATEWAY = "502"
|
37
|
+
|
38
|
+
class << self
|
39
|
+
|
40
|
+
attr_accessor :connection
|
41
|
+
|
42
|
+
@connection = nil
|
43
|
+
|
44
|
+
# Use the method to create global connection to vzaar.
|
45
|
+
#
|
46
|
+
# Usage:
|
47
|
+
# * Vzaar.connect! :login => 'Your vzaar login', :application_token => 'Your vzaar application token', :server => 'The vzaar server (vzaar.com by default)'
|
48
|
+
def connect!(options = {})
|
49
|
+
@connection = Base.new options
|
50
|
+
end
|
51
|
+
|
52
|
+
# Enables Rails specifc views and controllers used by vzaar uploader.
|
53
|
+
def enable_uploader
|
54
|
+
return if ActionView::Base.instance_methods.include? 'vzaar_basic_uploader'
|
55
|
+
require 'rails/views/view_helpers'
|
56
|
+
ActionView::Base.send :include, Vzaar::ViewHelpers
|
57
|
+
controllers_path = "#{File.dirname(__FILE__)}/rails/controllers"
|
58
|
+
ActiveSupport::Dependencies.load_paths << controllers_path
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
if defined?(Rails) and defined?(ActionController) and defined?(ActiveSupport)
|
66
|
+
Vzaar.enable_uploader
|
67
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
In your RAILS_ROOT dir type: "./script/generate vzaar_uploader".
|
Binary file
|
Binary file
|
Binary file
|