Class: Panda::Video
- Inherits:
-
Object
- Object
- Panda::Video
- Defined in:
- lib/panda/video.rb
Instance Attribute Summary collapse
-
#vals ⇒ Object
Returns the value of attribute vals.
Class Method Summary collapse
- .create ⇒ Object
- .delete(token) ⇒ Object
- .file_uploaded(token, filename, original_filename) ⇒ Object
- .find(token) ⇒ Object
-
.handle_response(response) ⇒ Object
Handles response and error codes from remote service.
- .new_with_attrs(vals) ⇒ Object
-
.request(method, path, params = {}) ⇒ Object
Makes request to remote service.
- .videos ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#vals ⇒ Object
Returns the value of attribute vals.
16 17 18 |
# File 'lib/panda/video.rb', line 16 def vals @vals end |
Class Method Details
.create ⇒ Object
55 56 57 58 59 |
# File 'lib/panda/video.rb', line 55 def self.create response = request(:post, "/videos") p = self.new_with_attrs(response[:video]) return p end |
.delete(token) ⇒ Object
67 68 69 70 |
# File 'lib/panda/video.rb', line 67 def self.delete(token) response = request(:post, "/videos/#{token}",{'_method' => 'delete'}) return response[:videos].map {|v| self.new_with_attrs(v[:video]) } end |
.file_uploaded(token, filename, original_filename) ⇒ Object
61 62 63 64 65 |
# File 'lib/panda/video.rb', line 61 def self.file_uploaded(token, filename, original_filename) response = request(:get, "/videos/#{token}/file_uploaded", {'filename' => filename, 'original_filename' => original_filename}) p = self.new_with_attrs(response[:video]) return p end |
.find(token) ⇒ Object
49 50 51 52 53 |
# File 'lib/panda/video.rb', line 49 def self.find(token) response = request(:get, "/videos/#{token}") p = self.new_with_attrs(response[:video]) return p end |
.handle_response(response) ⇒ Object
Handles response and error codes from remote service.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/panda/video.rb', line 100 def self.handle_response(response) case response.code.to_i when 200...400 YAML.load(response.body) when 401 raise(Panda::UnauthorizedAccess.new(response)) when 404 raise(Panda::VideoNotFound.new(response)) when 500...600 raise(Panda::ServerError.new(response)) else raise(Panda::PandaError.new(response, "Unknown response code: #{response.code}")) end end |
.new_with_attrs(vals) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/panda/video.rb', line 31 def self.new_with_attrs(vals) video = new video.vals = {} vals.each do |k,v| video.vals[k] = v class_eval "def video.#{k}; @vals[:#{k}]; end" end # If this is a parent video, turn the encodings into Panda::Video objects if vals[:status] == "original" vals[:encodings].map! do |e| self.new_with_attrs(e[:video]) end end return video end |
.request(method, path, params = {}) ⇒ Object
Makes request to remote service.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/panda/video.rb', line 78 def self.request(method, path, params={}) raise Panda::AccountKeyNotSet if Panda.account_key.nil? params[:account_key] = Panda.account_key path += ".yaml" http = Net::HTTP.new(Panda.api_domain, Panda.api_port) case method when :get response = http.request_get("#{path}?" + params.map {|k,v| "#{k}=#{v}"}.join('&')) when :post req = Net::HTTP::Post.new(path) req.form_data = params response = http.request(req) end puts "--> #{response.code} #{response.} (#{response.body.length})" puts response.body handle_response(response) end |
.videos ⇒ Object
72 73 74 75 |
# File 'lib/panda/video.rb', line 72 def self.videos response = request(:get, "/videos") return response[:videos].map {|v| self.new_with_attrs(v[:video]) } end |
Instance Method Details
#encoding_for_profile(profile_title) ⇒ Object
25 26 27 28 29 |
# File 'lib/panda/video.rb', line 25 def encoding_for_profile(profile_title) self.encodings.find { |e| e.profile_title == profile_title } end |
#find_encoding(profile_title) ⇒ Object
Deprecated. Use encoding_for_profile instead and check for status == ‘success’ manually.
19 20 21 22 23 |
# File 'lib/panda/video.rb', line 19 def find_encoding(profile_title) self.encodings.find { |e| e.profile_title == profile_title and e.status == 'success' } end |