Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- Added readable synonyms for main action by the rule media_*action*

- Added "media_list" request
- Added one common error, message handler for all actions
- Added unified actions output
- Added few actions in actions list
- Added 2 stupid forehead tests (you can replace it by your own cool one, I don't mind)
  • Loading branch information...
commit a4f15aedca16980fbb8efdcf0003f581f4f5cf37 1 parent 53f4cfe
Anton authored
Showing with 120 additions and 39 deletions.
  1. +83 −39 lib/encoding_wrapper/queue.rb
  2. +18 −0 test/test.rb
  3. +19 −0 test/test_list.rb
View
122 lib/encoding_wrapper/queue.rb
@@ -8,21 +8,24 @@
module EncodingWrapper
module Actions
- ADD_MEDIA = "AddMedia"
- UPDATE_MEDIA = "UpdateMedia"
- CANCEL_MEDIA = "CancelMedia"
- GET_STATUS = "GetStatus"
+ ADD_MEDIA = "AddMedia"
+ UPDATE_MEDIA = "UpdateMedia"
+ CANCEL_MEDIA = "CancelMedia"
+ GET_STATUS = "GetStatus"
+ GET_MEDIA_LIST = "GetMediaList"
+ GET_MEDIA_INFO = "GetMediaInfo"
end
module StatusType
- NEW = "New"
- WAITING = "Waiting for encoder"
- PROCESSING = "Processing"
- SAVING = "Saving"
- FINISHED = "Finished"
- ERROR = "Error"
- DELETED = "Deleted"
+ NEW = "New"
+ WAITING = "Waiting for encoder"
+ PROCESSING = "Processing"
+ SAVING = "Saving"
+ FINISHED = "Finished"
+ ERROR = "Error"
+ DELETED = "Deleted"
+ DOWNLOADING_NEW = "DownloadingNew"
end
class Queue
@@ -87,19 +90,21 @@ def request_status(media_id)
response = request_send(xml)
- status = response.css('status').text
- progress = response.css('progress').text.to_i
+ response[:status] = false
+ response[:progress] = false
+
+ if response[:errors].length == 0
+ response[:status] = response[:xml].css('status').text
+ response[:progress] = response[:xml].css('progress').text.to_i
# there is a bug where the progress reports
# as 100% if the status is 'Waiting for encoder'
- if (status == EncodingWrapper::StatusType::WAITING)
- progress = 0
+ if (response[:status] == EncodingWrapper::StatusType::WAITING)
+ response[:progress] = 0
+ end
end
- {
- :message => status,
- :progress => progress
- }
+ response
end
@@ -114,16 +119,6 @@ def cancel_media(media_id)
end.to_xml
response = request_send(xml)
-
- if response.css('errors error').length != 0
- message = response.css('errors error').text
- else
- message = response.css('response message').text
- end
-
- {
- :message => message,
- }
end
@@ -151,18 +146,54 @@ def add_media(source=nil, notify_url=nil)
response = request_send(xml)
- if response.css('errors error').length != 0
- message = response.css('errors error').text
- else
- message = response.css('response message').text
- media_id = response.css('MediaID').text
+ response[:media_id] = false
+
+ if response[:errors].length == 0
+ response[:media_id] = response[:xml].css('MediaID').text
end
- {
- :message => message,
- :media_id => media_id
- }
+ response
+ end
+ def media_add(source=nil, notify_url=nil)
+ add_media(source, notify_url)
+ end
+
+ def media_cancel(media_id)
+ cancel_media(media_id)
+ end
+
+ def media_status(media_id)
+ request_status(media_id)
+ end
+
+ def media_list
+ xml = Nokogiri::XML::Builder.new do |q|
+ q.query {
+ q.userid @user_id
+ q.userkey @user_key
+ q.action EncodingWrapper::Actions::GET_MEDIA_LIST
+ }
+ end.to_xml
+
+ response = request_send(xml)
+
+ response[:list] = []
+
+ media_list = response[':xml'].css('response media')
+
+ media_list.each do |media|
+ obj = {}
+
+ fields = media.css('*')
+ fields.each do |field|
+ obj[field.name.to_sym] = field.text
+ end
+
+ response[:list] << obj
+ end
+
+ response
end
@@ -188,7 +219,20 @@ def request_send(xml)
http.request(request)
}
- Nokogiri::XML(response.body)
+ output = {:errors => [], :status => false, :xml => '', :message => ''}
+ output[:xml] = Nokogiri::XML(response.body)
+
+ if response.css('errors error').length != 0
+ response.css('errors error').each { |error| output[:errors] << error.text }
+ else
+ output[:status] = true
+ end
+
+ if output[:xml].css('response message').length == 1
+ output[:message] = output[:xml].css('response message').text
+ end
+
+ output
end
end
View
18 test/test.rb
@@ -0,0 +1,18 @@
+require File.join('.', File.dirname(__FILE__), *%w[.. lib encoding_wrapper.rb])
+
+video_source = 'http://media.railscasts.com/assets/episodes/videos/272-markdown-with-redcarpet.mp4'
+
+instance = EncodingWrapper::Queue.new(6663, '9cae7f2202ad0ace51c8e4cde70dd733')
+
+result = instance.add_media video_source do |f|
+ f.output 'flv'
+end
+puts result
+
+sleep 2
+
+puts instance.request_status(result[:media_id])
+
+sleep 2
+
+puts instance.cancel_media(result[:media_id])
View
19 test/test_list.rb
@@ -0,0 +1,19 @@
+require File.join('.', File.dirname(__FILE__), *%w[.. lib encoding_wrapper.rb])
+
+video_source1 = 'http://media.railscasts.com/assets/episodes/videos/272-markdown-with-redcarpet.mp4'
+video_source2 = 'http://media.railscasts.com/assets/episodes/videos/270-authentication-in-rails-3-1.m4v'
+
+instance = EncodingWrapper::Queue.new(6663, '9cae7f2202ad0ace51c8e4cde70dd733')
+
+result = instance.add_media video_source1 do |f|
+ f.output 'flv'
+end
+
+result2 = instance.add_media video_source2 do |f|
+ f.output 'flv'
+end
+
+puts result
+puts result2
+
+puts instance.media_list
Please sign in to comment.
Something went wrong with that request. Please try again.