Skip to content

Instantly share code, notes, and snippets.

@ys
Created January 28, 2014 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ys/8666242 to your computer and use it in GitHub Desktop.
Save ys/8666242 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
require 'transmission_api'
require 'pp'
class TorrentCli
def initialize(conf_file = "#{ENV['HOME']}/.torrent.conf")
@conf_file = conf_file
unless File.exist?(conf_file)
$stderr.puts "Config file does not exists. Default is : ~/.torrent.conf"
exit 1
end
end
def run(args)
check_args(args)
command = args.shift
params = args
send(command, params)
end
def add(params = [])
client.create params.shift
end
def check_args(args)
unless args.count == 2
$stderr.puts "Usage: torrent add <torrent_url>"
exit 1
end
end
def check_config(config)
unless config[:username] && config[:password] && config[:url]
$stderr.puts "Config file should contains: username, password and url in json format"
exit 1
end
end
def client
@client ||= TransmissionApi::Client.new(configuration)
end
def configuration
@conf ||= JSON.parse(File.read(@conf_file)).inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}.tap do |conf|
check_config(conf)
end
end
end
TorrentCli.new.run(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment