Sha256: 82b2029d8da7318a0addd1f3fdff27428ff4ed665eaeeb59932462a416fb2b85

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

require 'zipruby'
require 'net/http/post/multipart'

module Transcriptic::Command

  # upload and run a protocol
  #
  class Run < Base

    # transcriptic run [FILENAME|DIRECTORY]
    #
    # upload FILENAME or DIRECTORY and launch it
    #
    def index
      path = args.shift
      upload_protocol(path)
    end

    def upload_protocol(path)
      begin
        stat = File.stat(path)
      rescue
        puts "No such path: #{path}"
        return
      end
      if stat.directory?
        files = Pathname.glob("#{path}/**/**")
        file_count = files.reject(&:directory?).length
        dir_count = files.reject(&:file?).length
        puts "Package detected, compressing #{file_count} files (#{dir_count} directories)..."
        upfile = Tempfile.new('protocol')
        Zip::Archive.open(upfile.path, Zip::CREATE) do |ar|
          ar.add_dir(path)
          Dir.glob("#{path}/**/**").each do |path|
            if File.directory?(path)
              ar.add_dir(path)
            else
              ar.add_file(path, path)
            end
          end
        end
      else
        upfile = path
      end
      puts "Uploading `#{path}` to Transcriptic..."
      url = URI.parse('http://client.transcriptic.com/receive')
      File.open(upfile) do |zf|
        req = Net::HTTP::Post::Multipart.new url.path,
          "file" => UploadIO.new(zf, "application/zip", "protocol.zip")
        res = Net::HTTP.start(url.host, url.port) do |http|
          http.request(req)
        end
        if res.code != 200
          puts "HTTP Error: #{res.message} (#{res.code})"
        end
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
transcriptic-0.1.0 lib/transcriptic/command/run.rb