Skip to content

Commit

Permalink
ruby horn raises exceptions on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
atomical committed Mar 4, 2013
1 parent 93dc467 commit 1aa8c14
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
@@ -1,3 +1,3 @@
[submodule "felix"]
path = felix
url = git://github.com/variations-on-video/hydrant-felix.git
url = git://github.com/avalonmediasystem/avalon-felix.git
2 changes: 1 addition & 1 deletion Gemfile
@@ -1,7 +1,7 @@
source "http://rubygems.org"

group :development, :test do
gem 'felixwrapper', :git => "git://github.com/avalonmediasystem/felixwrapper.git"
gem 'felixwrapper', :git => "git://github.com/avalonmediasystem/felixwrapper.git"
end

gemspec
Expand Down
2 changes: 2 additions & 0 deletions lib/rubyhorn/matterhorn_client.rb
Expand Up @@ -2,13 +2,15 @@
require 'rubyhorn/rest_client/info'
require 'rubyhorn/rest_client/ingest'
require 'rubyhorn/rest_client/workflow'
require 'rubyhorn/rest_client/exceptions'

module Rubyhorn
class MatterhornClient
include Rubyhorn::RestClient::Common
include Rubyhorn::RestClient::Info
include Rubyhorn::RestClient::Ingest
include Rubyhorn::RestClient::Workflow
include Rubyhorn::RestClient::Exceptions

# repository configuration (see #initialize)
attr_reader :config
Expand Down
45 changes: 24 additions & 21 deletions lib/rubyhorn/rest_client/common.rb
Expand Up @@ -9,33 +9,36 @@ module Common
attr_reader :cookie

def connect
uri = URI.parse(config[:url])
@http = Net::HTTP.new uri.host, uri.port
@http.set_debug_output $stderr if !config[:debug].nil? and config[:debug].downcase == 'true'
login
uri = URI.parse(config[:url])
@http = Net::HTTP.new uri.host, uri.port
@http.set_debug_output $stderr if !config[:debug].nil? and config[:debug].downcase == 'true'
login
end

def login url = "welcome.html"
uri = URI.parse(config[:url] + url)
req = Net::HTTP::Head.new uri.request_uri
res = execute_request(uri, req)
@cookie = res['set-cookie']
uri = URI.parse(config[:url] + url)
req = Net::HTTP::Head.new uri.request_uri
res = execute_request(uri, req)
@cookie = res['set-cookie']
end

def execute_request uri, req
uri.user = config[:user]
uri.password = config[:password]
head = Net::HTTP::Head.new uri.request_uri
head['X-REQUESTED-AUTH'] = 'Digest'
res = @http.request head

# if res.code.to_i != 200
digest_auth = Net::HTTP::DigestAuth.new
auth = digest_auth.auth_header uri, res['www-authenticate'], req.method
req.add_field 'Authorization', auth
res = @http.request req
# end
# res
uri.user = config[:user]
uri.password = config[:password]
head = Net::HTTP::Head.new uri.request_uri
head['X-REQUESTED-AUTH'] = 'Digest'
res = @http.request head
digest_auth = Net::HTTP::DigestAuth.new
auth = digest_auth.auth_header uri, res['www-authenticate'], req.method
req.add_field 'Authorization', auth
res = @http.request req

if res.code.to_i.between?(500,599)
raise Rubyhorn::RestClient::Exceptions::ServerError.new(res.code) if res.code.to_i.between?(500,599)
else
res
end

end

def get url, args = {}
Expand Down
22 changes: 22 additions & 0 deletions lib/rubyhorn/rest_client/exceptions.rb
@@ -0,0 +1,22 @@
module Rubyhorn::RestClient
module Exceptions
class RubyhornException < StandardError
end

class ServerError < RubyhornException
attr_reader :status_code
def initialize(status_code)
@status_code = status_code
super("Server responded with status code: #{status_code}.")
end
end

class MissingRequiredParams < RubyhornException
attr_reader :params
def initialize(params)
@params = params
super("You failed to include #{params.join(', ')} in your request.")
end
end
end
end
13 changes: 5 additions & 8 deletions lib/rubyhorn/rest_client/ingest.rb
Expand Up @@ -10,14 +10,11 @@ def addMediaPackage(file, params)

# Adds a mediapackage and starts ingesting, using an URL as the resource
def addMediaPackageWithUrl(params)
logger.debug "<<<<< IM IN URL MeTHOd >>>>>"

raise "Missing required field url" unless params.include? "url"
raise "Missing required field flavor" unless params.include? "flavor"
raise "Missing required field title" unless params.include? "title"

uri = "ingest/addMediaPackage" + (params["workflow"].nil? ? "" : "/#{params["workflow"]}")
return Rubyhorn::Workflow.from_xml post(uri, params)
missing_params = ['url','flavor','title','filename','workflow'].collect{|field| field if ! params.include?(field) }.compact
raise Rubyhorn::RestClient::Exceptions::MissingRequiredParams.new(missing_params) if missing_params.present?
uri = "ingest/addMediaPackage"
uri += "/#{params["workflow"]}" unless params["workflow"].nil?
Rubyhorn::Workflow.from_xml post(uri, params)
end
end
end
21 changes: 17 additions & 4 deletions spec/lib/ingest_spec.rb
Expand Up @@ -11,25 +11,38 @@
after(:all) do
@ids.each {|id| @client.stop id}
end
describe '#addMediaPackageWithUrl' do
it 'throws an exception when one of the required arguments is not passed' do
expect {
@client.addMediaPackageWithUrl({"title" => "hydrant:13", "workflow" => "avalon"}) # missing flavor, url, filename
}.to raise_error Rubyhorn::RestClient::Exceptions::MissingRequiredParams
end

it 'throws an exception when the server returns an http error status code' do
expect {
@client.addMediaPackageWithUrl({'title' => 'hydrant:13', 'flavor' => 'presenter/source', 'workflow' => "hydrant", 'url'=> 'http://localhost:8080', 'filename'=>'hi'})
}.to raise_error Rubyhorn::RestClient::Exceptions::ServerError
end
end

describe "addMediaPackage" do
it "should return a Workflow object after call to ingest" do
video = File.new "spec/fixtures/dance_practice.ogx"
workflow_doc = @client.addMediaPackage(video, {"title" => "hydrant:13", "flavor" => "presenter/source", "workflow" => "hydrant"})
workflow_doc = @client.addMediaPackage(video, {"title" => "hydrant:13", "flavor" => "presenter/source", "workflow" => "avalon"})
workflow_doc.should be_an_instance_of Rubyhorn::Workflow
workflow = workflow_doc.workflow
@ids << workflow.id[0]
workflow.template[0].should eql "hydrant"
workflow.template[0].should eql "avalon"
workflow.mediapackage.title[0].should eql "hydrant:13"
workflow.mediapackage.media.track.type[0].should eql "presenter/source"
end
it "should be able to rename file uploaded" do
video = File.new "spec/fixtures/dance_practice.ogx"
workflow_doc = @client.addMediaPackage(video, {"title" => "hydrant:13", "flavor" => "presenter/source", "workflow" => "hydrant", "filename" => "video.ogx"})
workflow_doc = @client.addMediaPackage(video, {"title" => "hydrant:13", "flavor" => "presenter/source", "workflow" => "avalon", "filename" => "video.ogx"})
workflow_doc.should be_an_instance_of Rubyhorn::Workflow
workflow = workflow_doc.workflow
@ids << workflow.id[0]
workflow.template[0].should eql "hydrant"
workflow.template[0].should eql "avalon"
workflow.mediapackage.title[0].should eql "hydrant:13"
workflow.mediapackage.media.track.type[0].should eql "presenter/source"
workflow.mediapackage.media.track.url[0].should match "video.ogx"
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/workflow_spec.rb
Expand Up @@ -5,7 +5,7 @@
Rubyhorn.init
@client = Rubyhorn.client
video = File.new "spec/fixtures/dance_practice.ogx"
workflow = @client.addMediaPackage(video, {"title" => "hydrant:13", "flavor" => "presenter/source", "workflow" => "hydrant"})
workflow = @client.addMediaPackage(video, {"title" => "hydrant:13", "flavor" => "presenter/source", "workflow" => "avalon"})
@id = workflow.id[0]
# puts "Created media package with workflow #{@id}: #{workflow.to_xml}"
end
Expand All @@ -19,7 +19,7 @@
workflow_doc = @client.instance_xml @id
workflow_doc.should be_an_instance_of Rubyhorn::Workflow
workflow = workflow_doc.workflow
workflow.template[0].should eql "hydrant"
workflow.template[0].should eql "avalon"
workflow.mediapackage.title[0].should eql "hydrant:13"
workflow.mediapackage.media.track.type[0].should eql "presenter/source"
end
Expand All @@ -28,7 +28,7 @@
describe "instances_json" do
it "should return a JSON doc with a list of instances that have the state RUNNING" do
video = File.new "spec/fixtures/dance_practice.ogx"
workflow = @client.addMediaPackage(video, {"title" => "hydrant:13", "flavor" => "presenter/source", "workflow" => "hydrant"})
workflow = @client.addMediaPackage(video, {"title" => "hydrant:13", "flavor" => "presenter/source", "workflow" => "avalon"})
sleep 5
json = @client.instances_json({"state" => "running"})
json["workflows"]["totalCount"].to_i.should > 1
Expand Down

0 comments on commit 1aa8c14

Please sign in to comment.