Sha256: 46498949ff853f1455ee6b38ebe4170550d60e5b3e12fd818985d5ca115ca9e1

Contents?: true

Size: 1015 Bytes

Versions: 1

Compression:

Stored size: 1015 Bytes

Contents

require 'httparty'
require 'sabnzbd/slot'

class Sabnzbd
  attr_accessor :api_key, :base_uri
  attr_reader :slots
  include HTTParty
  
  def initialize(args={})
    @api_key  = args[:api_key]  || (raise ApiKeyMissing.new, "Api key missing")
    @base_uri = args[:base_uri] || "localhost:8080"
    self.class.base_uri @base_uri
  end

  def simple_queue
    make_request
  end

  def paused?
    make_request["paused"]
  end

  def slots
    @slots ||= initialize_slots
  end

  private

  def make_request(url = "/api?mode=qstatus&output=json&apikey=#{@api_key}")
    verify_response( self.class.get(url).parsed_response )
  end

  def verify_response response
    raise ApiKeyInvalid.new, "Api key invalid #{@api_key}" if response["status"] == false && response["error"]

    response
  end

  def initialize_slots
    jobs = make_request["jobs"]

    jobs.each.inject([]) do |arr, job|
      arr << Sabnzbd::Slot.new(job)
    end
  end
end

class ApiKeyMissing < Exception;end
class ApiKeyInvalid < Exception;end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sabnzbd-0.1.0 lib/sabnzbd.rb