#!/usr/bin/env ruby require 'rubygems' require 'open-uri' require 'active_support' require 'optparse' require File.dirname(__FILE__) + '/../lib/swivel2' COLUMN_TYPE_HELP = <<-EOS Valid types: currency: Currency values, including the currency symbol. date: Date values that don't include timestamps. datetime: Date and time values. float: Floating point values. Swivel groks scientific notation! integer: Integer values. ignore: Columns of this type will not be uploaded. percent: Percent values without the "%" symbol. A value of 0.03 will appear as 3% in Swivel. string: String values. EOS SWIVELRC = <<-EOS Your API key is read from ~/.swivelrc: You need a ~/.swivelrc file before you can use this program! The contents of your swivelrc file should look like this: --- !ruby/struct:Swivel2::Config site: https://x:@api.swivel.com timeout_read: 500 timeout_write: 1_000 extra_params: { noviews: true } EOS EXAMPLES = <<-EOS Examples: Upload a data set $ cat data.csv | #{$0} upload 1234567 Append data to data set with id 1234567 $ cat more_data.csv | #{$0} append 1234567 Replace underlying data for data set with id 1234567 $ cat new_data.csv | #{$0} replace 1234567 EOS options = Hash.new config = Swivel2::Config.load ARGV.options do |opts| opts.banner = 'Usage: swivel [options]' opts.separator '' opts.separator 'Options:' opts.on '-f', '--file=', String, 'File to upload, append, or replace.', 'Your actual data is read from this file.', 'If omitted, data is read from STDIN.', 'OPTIONAL' do |v| options[:filename] = v end opts.separator '' opts.on '-c', '--headings=', String, 'Column headings as a comma separated list.', 'Example: "Year,Revenue"', 'OPTIONAL' do |v| options[:headings] = v end opts.separator '' opts.on *['-t', '--types=', String, 'Column types as a comma separated list.', 'Example: "date,currency"', COLUMN_TYPE_HELP.split("\n"), 'OPTIONAL'].flatten do |v| options[:types] = v end opts.separator '' opts.on '-s', '--sep=', String, 'Column separator.', 'Example: "," or "\t"', 'OPTIONAL' do |v| options[:sep] = v end opts.separator '' 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 config @swivel = Swivel2::Connection.new config end def upload name, options = Hash.new opts = { 'data_set[name]' => name, 'data_set[citation]' => $0, 'data_set[public]' => 0, 'file[data]' => read(options[:filename]) } opts.merge! 'file[types]' => options[:types] if options[:types] opts.merge! 'file[headings]' => options[:headings] if options[:headings] opts.merge! 'file[sep]' => options[:sep] if options[:sep] data_set = @swivel.post '/data_sets', opts puts "uploaded #{data_set.id}" end def append id, options = Hash.new opts = { 'file[mode]' => 'append', 'file[data]' => read(options[:filename]) } data_set = update id, opts puts "appended #{data_set.id}" end def replace id, options = Hash.new opts = { 'file[mode]' => 'replace', 'file[data]' => read(options[:filename]) } data_set = update id, opts puts "replaced #{data_set.id}" end private def update id, options @swivel.put "/data_sets/#{id}", options end def read filename = nil if filename open filename do |f| f.readlines.join end else readlines.join end end end helper = SwivelHelper.new config action = ARGV.shift resource = ARGV.shift id = ARGV.shift case action.downcase when /upload/i name = resource helper.upload name, options when /append/i, /replace/i id = resource helper.send action.to_sym, id, options else puts "#{action} not supported" exit end