lib/rivendell/api/xport.rb in rivendell-api-0.0.2 vs lib/rivendell/api/xport.rb in rivendell-api-0.0.3

- old
+ new

@@ -1,9 +1,9 @@ module Rivendell::API class Xport include HTTMultiParty - base_uri 'http://localhost/rd-bin/rdxport.cgi' + # debug_output $stderr format :xml COMMAND_EXPORT = 1 COMMAND_IMPORT = 2 @@ -24,26 +24,48 @@ def logger Rivendell::API.logger end + def initialize(options = {}) + options.each { |k,v| send "#{k}=", v } + end + + attr_accessor :login_name, :password, :host + + def host + @host ||= "localhost" + end + + def login_name + @login_name ||= "user" + end + + def password + @password ||= "" + end + + def rdxport_uri + "http://#{host}/rd-bin/rdxport.cgi" + end + def query(command, attributes = {}) attributes = { :command => command, - :login_name => "user", - :password => "" + :login_name => login_name, + :password => password }.merge(attributes) attributes.inject({}) do |map, (key, value)| map[key.to_s.upcase] = value map end end def post(command, attributes = {}, options = {}) logger.debug "Post #{command} #{attributes.inspect}" - self.class.post '', :query => query(command, attributes) + self.class.post rdxport_uri, :query => query(command, attributes) end def list_groups post COMMAND_LISTGROUPS end @@ -68,10 +90,35 @@ def remove_cart(cart_number) post COMMAND_REMOVECART, :cart_number => cart_number end + def list_cuts(cart_number) + response = post COMMAND_LISTCUTS, :cart_number => cart_number + case cuts_xml = response["cutList"]["cut"] + when Array + cuts_xml.collect do |cut_xml| + Rivendell::API::Cut.new(cut_xml) + end + when nil + [] + else + [ Rivendell::API::Cut.new(cuts_xml) ] + end + end + + def remove_cut(cart_number, cut_number) + post COMMAND_REMOVECUT, :cart_number => cart_number, :cut_number => cut_number + end + + # Extension + def clear_cuts(cart_number) + list_cuts(cart_number).map(&:number).each do |cut_number| + remove_cut cart_number, cut_number + end + end + def import(cart_number, cut_number, file, options = {}) arguments = { :channels => 2, :normalization_level => -13, :autotrim_level => -30, @@ -89,6 +136,7 @@ response = post COMMAND_ADDCUT, :cart_number => cart_number Rivendell::API::Cut.new(response["cutAdd"]["cut"]) end end + end