Permalink
Browse files

Renamed files, add basic client

  • Loading branch information...
1 parent d2d6b88 commit b95014e30eeeeac60001093d73ddcae3a76179f5 @Sutto committed Jan 8, 2009
Showing with 217 additions and 6 deletions.
  1. +2 −2 Rakefile
  2. +98 −0 lib/SABnzbd.rb
  3. +98 −0 lib/sabnzbd.rb
  4. +8 −4 test/SABnzbd_test.rb
  5. +11 −0 test/sabnzbd_test.rb
View
@@ -7,10 +7,10 @@ begin
require 'jeweler'
Jeweler::Tasks.new do |s|
s.name = "SABnzbd"
- s.summary = "TODO"
+ s.summary = "Ruby interface to the SABnzbd usenet client"
s.email = "sutto@sutto.net"
s.homepage = "http://github.com/Sutto/SABnzbd"
- s.description = "TODO"
+ s.description = "Ruby interface to the SABnzbd usenet client"
s.authors = ["Darcy Laycock"]
end
rescue LoadError
View
@@ -0,0 +1,98 @@
+require 'rubygems'
+require 'httparty'
+
+class SABnzbd
+
+ class Job
+ attr_accessor :id, :left, :done, :msgid, :filename
+ def initialize(id, left, done, msgid, filename)
+ self.id, self.left, self.done, self.msgid, self.filename = id, left, done, msgid, filename
+ end
+ end
+
+ class Status
+ attr_accessor :paused, :queue_size, :jobs, :size_downloaded, :size_left, :time_left,
+ :complete_disk_free, :download_disk_free, :speed, :raw
+
+ def alive?
+ !alive
+ end
+
+ def paused?
+ paused
+ end
+
+ def initialize(hash)
+ self.raw = hash
+ self.paused = hash["paused"]
+ self.jobs = self.class.jobs_from(hash)
+ self.queue_size = hash["noofslots"]
+ self.size_downloaded = hash["mb"]
+ self.size_left = hash["mbleft"]
+ self.time_left = parse_time(hash)
+ self.speed = hash["kbpersec"]
+ # Disk Spaces
+ self.complete_disk_free = hash["diskspace1"]
+ self.download_disk_free = hash["diskspace2"]
+ end
+
+ def self.jobs_from(hash)
+ jobs = []
+ hash["jobs"].each do |j|
+ jobs << Job.new(j["id"], j["mbleft"], j["mb"], j["msgid"], j["filename"])
+ end
+ return jobs
+ end
+
+ private
+
+ def parse_time(hash)
+ t = hash["timeleft"]
+ parts = t.split(":").map { |p| p.to_i }
+ return parts[2] + parts[1] * 60 + parts[0] * 3600
+ end
+
+ end
+
+ include HTTParty
+ base_uri 'localhost:8080'
+
+ def initialize(username, password)
+ login(username, password)
+ end
+
+ def login(username, password)
+ self.class.default_params :ma_username => username, :ma_password => password
+ end
+
+ def status
+ results = api_call(:qstatus, :output => "json")
+ return Status.new(results)
+ end
+
+ def shutdown!
+ verify api_call(:shutdown)
+ end
+
+ def resume!
+ verify api_call(:resume)
+ end
+
+ def pause!
+ verify api_call(:pause)
+ end
+
+ def api_call(mode, opts = {})
+ opts.merge!(:mode => mode.to_s)
+ return self.class.get("/sabnzbd/api", :query => opts)
+ end
+
+ def verify(text)
+ text.strip == "ok"
+ end
+
+ def jobs
+ Status.jobs_from(status)
+ end
+
+end
View
@@ -0,0 +1,98 @@
+require 'rubygems'
+require 'httparty'
+
+class SABnzbd
+
+ class Job
+ attr_accessor :id, :left, :done, :msgid, :filename
+ def initialize(id, left, done, msgid, filename)
+ self.id, self.left, self.done, self.msgid, self.filename = id, left, done, msgid, filename
+ end
+ end
+
+ class Status
+ attr_accessor :paused, :queue_size, :jobs, :size_downloaded, :size_left, :time_left,
+ :complete_disk_free, :download_disk_free, :speed, :raw
+
+ def alive?
+ !alive
+ end
+
+ def paused?
+ paused
+ end
+
+ def initialize(hash)
+ self.raw = hash
+ self.paused = hash["paused"]
+ self.jobs = self.class.jobs_from(hash)
+ self.queue_size = hash["noofslots"]
+ self.size_downloaded = hash["mb"]
+ self.size_left = hash["mbleft"]
+ self.time_left = parse_time(hash)
+ self.speed = hash["kbpersec"]
+ # Disk Spaces
+ self.complete_disk_free = hash["diskspace1"]
+ self.download_disk_free = hash["diskspace2"]
+ end
+
+ def self.jobs_from(hash)
+ jobs = []
+ hash["jobs"].each do |j|
+ jobs << Job.new(j["id"], j["mbleft"], j["mb"], j["msgid"], j["filename"])
+ end
+ return jobs
+ end
+
+ private
+
+ def parse_time(hash)
+ t = hash["timeleft"]
+ parts = t.split(":").map { |p| p.to_i }
+ return parts[2] + parts[1] * 60 + parts[0] * 3600
+ end
+
+ end
+
+ include HTTParty
+ base_uri 'localhost:8080'
+
+ def initialize(username, password)
+ login(username, password)
+ end
+
+ def login(username, password)
+ self.class.default_params :ma_username => username, :ma_password => password
+ end
+
+ def status
+ results = api_call(:qstatus, :output => "json")
+ return Status.new(results)
+ end
+
+ def shutdown!
+ verify api_call(:shutdown)
+ end
+
+ def resume!
+ verify api_call(:resume)
+ end
+
+ def pause!
+ verify api_call(:pause)
+ end
+
+ def api_call(mode, opts = {})
+ opts.merge!(:mode => mode.to_s)
+ return self.class.get("/sabnzbd/api", :query => opts)
+ end
+
+ def verify(text)
+ text.strip == "ok"
+ end
+
+ def jobs
+ Status.jobs_from(status)
+ end
+
+end
View
@@ -1,7 +1,11 @@
require File.dirname(__FILE__) + '/test_helper'
-class SabnzbdTest < Test::Unit::TestCase
- should "probably rename this file and start testing for real" do
- flunk "hey buddy, you should probably rename this file and start testing for real"
- end
+class SABnzbdTest < Test::Unit::TestCase
+
+ should "let return SABnzbd::Status using SABnzbd#status"
+
+ should "let you pause! downloading"
+
+ should "let you resume! downloading"
+
end
View
@@ -0,0 +1,11 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class SABnzbdTest < Test::Unit::TestCase
+
+ should "let return SABnzbd::Status using SABnzbd#status"
+
+ should "let you pause! downloading"
+
+ should "let you resume! downloading"
+
+end

0 comments on commit b95014e

Please sign in to comment.