#!/usr/bin/env ruby require 'rubygems' require 'active_support' # TODO: make it work w/o this require 'optparse' require File.dirname(__FILE__) + '/../lib/swivel' require 'yaml' #require 'ruby-debug' #Debugger.start options = Hash.new config = Swivel::Connection::Config.new config.load config.save ARGV.options do |opts| opts.on '-h', '--host=name', String, "Swivel hostname or IP address.", "Default: #{options[:host]}" do |v| options[:host] = v end opts.on '-p', '--port=number', String, "Swivel host's port number.", "Default: #{options[:port]}" do |v| options[:port] = v end opts.on '-f', '--file=file', String, "File to upload, append, or replace." do |v| options[:filename] = v end opts.on_tail '-r', '--raw=path', String, "Perform a raw call and print the XML response." do |v| puts Swivel::Connection.new(options).call(v, options) exit end opts.on_tail '-k', '--key=api-key', "Set your API key." do |v| config = Swivel::Connection::Config.new config.config[:old_api_key] = config.config[:api_key] config.config[:api_key] = v config.save exit end opts.on_tail '-?', '--help', "Show this help message." do puts opts; exit end opts.parse! if ARGV.empty? puts opts exit end end class SwivelHelper def initialize options = Hash.new @swivel = Swivel::Connection.new options end def show resource, id resource = resource.pluralize response = @swivel.call "/rest/#{resource}/#{id}" end def list resource, options = Hash.new resource = resource.pluralize response = @swivel.call "/rest/#{resource}", options end def upload name, options = Hash.new filename = options[:filename] data_set = @swivel.upload :original_asset_name => filename, :original_asset_path => filename, :auto_estimate => true, :data => read(filename), :name => name, :citation => $0, :display_tags => 'swivel' puts "uploaded #{data_set.id}" end def append id, options = Hash.new filename = options[:filename] data_set = @swivel.append :id => id, :original_asset_name => filename, :original_asset_path => filename, :auto_estimate => true, :data => read(filename) puts "appended #{data_set.id}" end def replace id, options = Hash.new filename = options[:filename] data_set = @swivel.replace :id => id, :original_asset_name => filename, :original_asset_path => filename, :auto_estimate => true, :data => read(filename) puts "replaced #{data_set.id}" end private def read filename = nil if filename open filename, 'r' do |f| f.readlines.join end else readlines.join end end end helper = SwivelHelper.new options action = ARGV.shift resource = ARGV.shift id = ARGV.shift case action.downcase when 'list' helper.list resource, options when 'show' helper.show resource, id when 'upload' name = resource helper.upload name, options when 'append', 'replace' id = resource helper.send action.to_sym, id, options else puts "#{action} not supported" exit end