Class: Xbmc

Inherits:
Object show all
Includes:
HTTParty
Defined in:
lib/xbmc-client.rb

Overview

A simple XBMC JSON RPC API Client. See README for details.

Defined Under Namespace

Classes: Command, UnauthorizedError

Class Method Summary collapse

Class Method Details

.commandsObject

Returns an array of available api commands instantiated as Xbmc::Command objects



38
39
40
# File 'lib/xbmc-client.rb', line 38

def commands
  @commands ||= invoke_and_process("JSONRPC.Introspect", :getdescriptions => true)[:commands].map {|c| Xbmc::Command.new(c)}
end

.invoke_and_process(method, params = {}) ⇒ Object

API interaction: Invokes the given method with given params, parses the JSON response body, maps it to a HashWithIndifferentAccess and returns the :result subcollection



21
22
23
# File 'lib/xbmc-client.rb', line 21

def invoke_and_process(method, params={})
  JSON.parse(invoke_method(method, params).body).with_indifferent_access[:result]
end

.invoke_method(method, params = {}) ⇒ Object

Raw API interaction: Invoke the given JSON RPC Api call and return the raw response (which is an instance of HTTParty::Response)



27
28
29
30
31
32
33
34
35
# File 'lib/xbmc-client.rb', line 27

def invoke_method(method, params={})
  response = post('/jsonrpc', :body => {"jsonrpc" => "2.0", "params" => params, "id" => "1", "method" => method}.to_json)
  raise Xbmc::UnauthorizedError, "Could not authorize with XBMC. Did you set up your credentials for basic_auth using Xbmc.basic_auth 'user', 'pass'?" if response.response.class == Net::HTTPUnauthorized
  response
  
# Capture connection errors and send them out with a custom message
rescue Errno::ECONNREFUSED, SocketError, HTTParty::UnsupportedURIScheme => err
  raise err.class, err.message + ". Did you configure the url and port for XBMC properly using Xbmc.base_uri 'http://localhost:1234'?"
end

.load_api!Object

Loads the available commands via JSONRPC.Introspect and defines the namespace classes and corresponding methods in the ruby namespace



44
45
46
47
48
49
50
# File 'lib/xbmc-client.rb', line 44

def load_api!
  return false if @api_loaded
  commands.each do |command|
    command.send :define_method!
  end
  @api_loaded = true
end