class Deluge::Api::Client

Attributes

api_methods[R]
auth_level[R]
namespaces[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/deluge/api/client.rb, line 6
def initialize(options = {})
  @connection = Deluge::Api::Connection.new(options)
  @login = options.fetch(:login)
  @password = options.fetch(:password)

  @namespaces = {}
  @api_methods = []
end

Public Instance Methods

close() click to toggle source
# File lib/deluge/api/client.rb, line 37
def close
  @connection.close
  @auth_level = nil
  @api_methods = []
  @namespaces.each_key do |ns|
    self.singleton_class.send :undef_method, ns
  end
  @namespaces = {}
end
connect() click to toggle source
# File lib/deluge/api/client.rb, line 15
def connect
  @connection.start

  @auth_level = @connection.authenticate(@login, @password)

  register_methods!

  true
end
register_event(event_name, &block) click to toggle source
# File lib/deluge/api/client.rb, line 25
def register_event(event_name, &block)
  raise "Provide block for event" unless block

  if event_name.is_a?(Symbol)
    event_name = "#{event_name}_event"
    # convert to CamelCase
    event_name.gsub!(/(?:_|^)(.)/) { |match| $1.upcase }
  end

  @connection.register_event(event_name, &block)
end

Private Instance Methods

register_method!(namespaces, method) click to toggle source
# File lib/deluge/api/client.rb, line 60
def register_method!(namespaces, method)
  namespace = register_namespace(namespaces)

  namespace.register_method(method)
end
register_methods!() click to toggle source
# File lib/deluge/api/client.rb, line 49
def register_methods!
  methods = @connection.method_list

  methods.each do |method|
    *namespaces, method_name = method.split('.')

    register_method!(namespaces, method_name)
    @api_methods << method
  end
end
register_namespace(namespaces) click to toggle source
# File lib/deluge/api/client.rb, line 66
def register_namespace(namespaces)
  ns = namespaces.shift

  root = @namespaces[ns]

  unless root
    root = Api::Namespace.new(ns, @connection)
    @namespaces[ns] = root

    define_singleton_method(ns.to_sym) do
      @namespaces[ns]
    end
  end

  namespaces.each do |namespace|
    root = root.register_namespace(namespace)
  end

  root
end