Sha256: 19277e265a9bcedc8c3de277f39b53f84feab4a3657b9a4bb48b15122395012e

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

require "public/version"
require "fileutils"
require "clipboard"

GEM_DIRECTORY = File.dirname(__FILE__)
CONFIG = "#{ Dir.home }/.public_gem"

class Public
  def self.config
    file = setup
    get_id(file)
  end

  def self.setup
    if File.file?(CONFIG)
      file = File.read(CONFIG)
      get_id(file)
      if @user_id == "CHANGE_ME"
        change_me
      else
        puts "Already configured with a Dropbox ID of #{ @user_id }"
        puts "Try copying a file with 'public <FILE>'"
      end
    else
      not_configured
    end
    file
  end

  def self.not_configured
    FileUtils.cp "#{ GEM_DIRECTORY }/.public_gem", CONFIG
    file = File.read(CONFIG)
    change_me
    file
  end

  def self.get_id(file)
    @user_id = file.split(": ").last.chomp unless file.nil?
  end

  def self.process(file)
    unless File.file?(CONFIG)
      not_configured
    else
      if File.file?(file)
        move_file(file)
      else
        puts "That file doesn't exist!"
      end
    end
  end

  def self.move_file(file)
    dropbox_location = "#{ Dir.home }/Dropbox/Public/"
    FileUtils.cp file, dropbox_location
    copy_link(file)
  end

  def self.copy_link(file)
    get_id(File.read(CONFIG))
    if @user_id == "CHANGE_ME"
      change_me
    else
      dropbox_url = "https://dl.dropboxusercontent.com/u/#{ @user_id }/#{ file }"
      Clipboard.copy(dropbox_url)
      puts dropbox_url
      puts "Copied to clipboard."
    end
  end

  def self.change_me
    puts "Your config file is located at '~/.public_gem'"
    puts "Please replace the default value with your Dropbox ID."
    puts "(You can find this in a Dropbox public URL: "
    puts "dl.dropboxusercontent.com/u/YOUR_ID/foobar.zip)"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
public-0.0.4 lib/public.rb