xbmc-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ .bundle
2
+
3
+ ## MAC OS
4
+ .DS_Store
5
+
6
+ ## TEXTMATE
7
+ *.tmproj
8
+ tmtags
9
+
10
+ ## EMACS
11
+ *~
12
+ \#*
13
+ .\#*
14
+
15
+ ## VIM
16
+ *.swp
17
+
18
+ ## PROJECT::GENERAL
19
+ coverage
20
+ rdoc
21
+ pkg
22
+
23
+ ## PROJECT::SPECIFIC
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+
7
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ xbmc-client (0.1.0)
5
+ activesupport (>= 3.0.0)
6
+ httparty (>= 0.6.0)
7
+ i18n
8
+ json (>= 1.4.5)
9
+
10
+ GEM
11
+ remote: http://rubygems.org/
12
+ specs:
13
+ activesupport (3.0.3)
14
+ crack (0.1.8)
15
+ httparty (0.6.1)
16
+ crack (= 0.1.8)
17
+ i18n (0.4.2)
18
+ json (1.4.6)
19
+ shoulda (2.10.3)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ activesupport (>= 3.0.0)
26
+ httparty (>= 0.6.0)
27
+ i18n
28
+ json (>= 1.4.5)
29
+ shoulda (= 2.10.3)
30
+ xbmc-client!
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Christoph Olszowka
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,79 @@
1
+ = XBMC-Client
2
+
3
+ This is a simple Ruby client for the XBMC (XBox Media Center) JSON-RPC API.
4
+
5
+ It does not define all the API methods explicitly, but rather loads and defines them
6
+ on the fly by pulling the available namespaces and methods from the JSONRPC.Introspect
7
+ call baked into the XBMC Api and thus has all methods described there available via
8
+ Xbmc::NAMESPACE.method_name_in_underscore_writing, so for example the JSON RPC call
9
+ `AudioPlayer.PlayPause` becomes `Xbmc::AudioPlayer.play_pause`.
10
+
11
+ Parameters can be passed in to all methods via the optional first argument to each
12
+ method call, which is expected to be a hash: `Xbmc::AudioLibrary.get_songs(:albumid => 1)`
13
+
14
+ Note that this is a very early release and is considered experimental. There will
15
+ be bugs.
16
+
17
+ The client is being developed against Ruby 1.9.2, but should work with Ruby 1.8 as well.
18
+
19
+ == Install
20
+
21
+ gem install xbmc-client
22
+
23
+ == Usage
24
+
25
+ require 'pp'
26
+ require 'xbmc-client'
27
+
28
+ # Set up the url and auth credentials
29
+ Xbmc.base_uri "http://localhost:8435"
30
+ Xbmc.basic_auth "xbmc", "xbmc"
31
+ Xbmc.load_api! # This will call JSONRPC.Introspect and create all subclasses and methods dynamically
32
+
33
+ pp Xbmc::AudioLibrary.get_artists
34
+ # > [{"artistid"=>1,
35
+ # "fanart"=>"special://masterprofile/Thumbnails/Music/Fanart/c03803be.tbn",
36
+ # "label"=>"The Weakerthans"}, ... ]
37
+
38
+ pp Xbmc::AudioLibrary.get_songs(:albumid => 1)
39
+ # [{"fanart"=>"special://masterprofile/Thumbnails/Music/Fanart/c03803be.tbn",
40
+ # "file"=>
41
+ # "/some/path/to/a/file.mp3",
42
+ # "label"=>"Elegy for Elsabet",
43
+ # "songid"=>20}, ...]
44
+
45
+ # You can interact with the raw api with:
46
+ Xbmc.invoke_method('JSONRPC.Introspect', :getdescriptions => 'true') # Will return the raw response
47
+ Xbmc.invoke_and_process('JSONRPC.Introspect, :getdescriptions => 'true') # Will return the JSON-parsed response body's result subcollection
48
+
49
+
50
+ As you'll notice, it tries to automatically pull the correct collection for `get_xyz`
51
+ calls, which means that you don't have to go to the subcollection [:result][:artists] in the
52
+ above example like you would if the response would be returned unprocessed.
53
+
54
+ See the examples directory in the repository for further usage examples!
55
+
56
+ Also, be sure to check out the API docs at the XBMC wiki:
57
+ http://wiki.xbmc.org/index.php?title=JSON_RPC
58
+
59
+ == Issues
60
+
61
+ * No unit tests
62
+ * No parameter validation
63
+ * Fields (i.e. album details etc.) should be automatically requested by the api, but this would
64
+ currently require the JSONRPC.Introspect Api method to return a collection of available fields
65
+ for each method.
66
+
67
+ == Note on Patches/Pull Requests
68
+
69
+ * Fork the project.
70
+ * Make your feature addition or bug fix.
71
+ * Add tests for it. This is important so I don't break it in a
72
+ future version unintentionally.
73
+ * Commit, do not mess with rakefile, version, or history.
74
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
75
+ * Send me a pull request. Bonus points for topic branches.
76
+
77
+ == Copyright
78
+
79
+ Copyright (c) 2010 Christoph Olszowka. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "xbmc-client"
8
+ gem.summary = %Q{A simple API client for the XBMC JSON-RPC API}
9
+ gem.description = %Q{A simple API client for the XBMC JSON-RPC API}
10
+ gem.email = "christoph at olszowka de"
11
+ gem.homepage = "http://github.com/colszowka/xbmc-client"
12
+
13
+ gem.add_dependency 'httparty', ">= 0.6.0"
14
+ gem.add_dependency 'json', ">= 1.4.5"
15
+ gem.add_dependency 'activesupport', ">= 3.0.0"
16
+ gem.add_dependency 'i18n'
17
+
18
+ gem.authors = ["Christoph Olszowka"]
19
+ gem.add_development_dependency "shoulda", "2.10.3"
20
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
21
+ end
22
+ Jeweler::GemcutterTasks.new
23
+ rescue LoadError
24
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
25
+ end
26
+
27
+ require 'rake/testtask'
28
+ Rake::TestTask.new(:test) do |test|
29
+ test.libs << 'lib' << 'test'
30
+ test.pattern = 'test/**/test_*.rb'
31
+ test.verbose = true
32
+ end
33
+
34
+ begin
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ end
41
+ rescue LoadError
42
+ task :rcov do
43
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
44
+ end
45
+ end
46
+
47
+ task :test => :check_dependencies
48
+
49
+ task :default => :test
50
+
51
+ require 'rake/rdoctask'
52
+ Rake::RDocTask.new do |rdoc|
53
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
54
+
55
+ rdoc.rdoc_dir = 'rdoc'
56
+ rdoc.title = "xbmc-client #{version}"
57
+ rdoc.rdoc_files.include('README*')
58
+ rdoc.rdoc_files.include('lib/**/*.rb')
59
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,15 @@
1
+ require 'pp'
2
+ require './' + File.join(File.dirname(__FILE__), '../lib', 'xbmc-client')
3
+
4
+ Xbmc.base_uri "http://localhost:8435"
5
+ Xbmc.basic_auth "xbmc", "xbmc"
6
+
7
+ Xbmc.load_api!
8
+
9
+ pp Xbmc::AudioLibrary.get_artists
10
+ puts "="*60
11
+ pp Xbmc::AudioLibrary.get_albums(:artistid => Xbmc::AudioLibrary.get_artists.first[:artistid])
12
+ puts "="*60
13
+ pp Xbmc::AudioLibrary.get_songs(:albumid => 1)
14
+
15
+ pp Xbmc::AudioPlayer.play_pause
data/lib/ruby_ext.rb ADDED
@@ -0,0 +1,7 @@
1
+ class Object
2
+ def metaclass
3
+ class << self
4
+ self
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.require(:default)
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'ruby_ext'
8
+ require 'active_support/core_ext'
9
+
10
+ # A simple XBMC JSON RPC API Client. See README for details.
11
+ class Xbmc
12
+ # Error class for indicating trouble with authentication against the XBMC Api
13
+ class UnauthorizedError < StandardError; end;
14
+
15
+ include HTTParty
16
+
17
+ class << self
18
+ # API interaction: Invokes the given method with given params, parses the JSON response body, maps it to
19
+ # a HashWithIndifferentAccess and returns the :result subcollection
20
+ def invoke_and_process(method, params={})
21
+ JSON.parse(invoke_method(method, params).body).with_indifferent_access[:result]
22
+ end
23
+
24
+ # Raw API interaction: Invoke the given JSON RPC Api call and return the raw response (which is an instance of
25
+ # HTTParty::Response)
26
+ def invoke_method(method, params={})
27
+ response = post('/jsonrpc', :body => {"jsonrpc" => "2.0", "params" => params, "id" => "1", "method" => method}.to_json)
28
+ 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
29
+ response
30
+
31
+ # Capture connection errors and send them out with a custom message
32
+ rescue Errno::ECONNREFUSED, SocketError, HTTParty::UnsupportedURIScheme => err
33
+ raise err.class, err.message + ". Did you configure the url and port for XBMC properly using Xbmc.base_uri 'http://localhost:1234'?"
34
+ end
35
+
36
+ # Returns an array of available api commands instantiated as Xbmc::Command objects
37
+ def commands
38
+ @commands ||= invoke_and_process("JSONRPC.Introspect", :getdescriptions => true)[:commands].map {|c| Xbmc::Command.new(c[:command])}
39
+ end
40
+
41
+ # Loads the available commands via JSONRPC.Introspect and defines the namespace classes and corresponding methods
42
+ # in the ruby namespace
43
+ def load_api!
44
+ return false if @api_loaded
45
+ commands.each do |command|
46
+ command.send :define_method!
47
+ end
48
+ @api_loaded = true
49
+ end
50
+ end
51
+ end
52
+
53
+ require 'xbmc/command'
@@ -0,0 +1,49 @@
1
+ # Representation of a XBMC JSON RPC method
2
+ class Xbmc::Command
3
+ attr_reader :command, :namespace, :method_name, :original_method
4
+
5
+ def initialize(command)
6
+ @command = command
7
+ parse_command!
8
+ end
9
+
10
+ # Invokes this command and processes the result
11
+ def invoke(params={})
12
+ process_result(Xbmc.invoke_and_process(command, params))
13
+ end
14
+
15
+ private
16
+
17
+ # Extract the namespace and method names from the given JSON RPC raw command name
18
+ def parse_command!
19
+ @namespace, @original_method = command.split('.')
20
+ @method_name = original_method.underscore
21
+ end
22
+
23
+ # Will create the corresponding class for namespace if not defined yet
24
+ # and also define the given method
25
+ def define_method!
26
+ klass_name = "Xbmc::#{namespace}"
27
+ begin
28
+ klass = klass_name.constantize
29
+ rescue NameError => err
30
+ eval("#{klass_name} = Class.new")
31
+ klass = klass_name.constantize
32
+ end
33
+
34
+ command_object = self
35
+ klass.metaclass.send(:define_method, method_name.to_sym) do |*args|
36
+ command_object.invoke(*args)
37
+ end
38
+ end
39
+
40
+ # Will return the subcollection "xyz" from the response for "get_xyz" commands when
41
+ # the collection is present, or just the result collection otherwise.
42
+ def process_result(result)
43
+ if method_name =~ /^get_(.+)/ and (collection = result[$1.gsub('_', '')]).present?
44
+ return collection
45
+ else
46
+ return result
47
+ end
48
+ end
49
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'xbmc-client'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestXbmcClient < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,73 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{xbmc-client}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Christoph Olszowka"]
12
+ s.date = %q{2010-11-25}
13
+ s.description = %q{A simple API client for the XBMC JSON-RPC API}
14
+ s.email = %q{christoph at olszowka de}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ ".rvmrc",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "examples/audio_library.rb",
30
+ "lib/ruby_ext.rb",
31
+ "lib/xbmc-client.rb",
32
+ "lib/xbmc/command.rb",
33
+ "test/helper.rb",
34
+ "test/test_xbmc-client.rb",
35
+ "xbmc-client.gemspec"
36
+ ]
37
+ s.homepage = %q{http://github.com/colszowka/xbmc-client}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.7}
41
+ s.summary = %q{A simple API client for the XBMC JSON-RPC API}
42
+ s.test_files = [
43
+ "test/helper.rb",
44
+ "test/test_xbmc-client.rb",
45
+ "examples/audio_library.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
+ s.add_runtime_dependency(%q<httparty>, [">= 0.6.0"])
54
+ s.add_runtime_dependency(%q<json>, [">= 1.4.5"])
55
+ s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0"])
56
+ s.add_runtime_dependency(%q<i18n>, [">= 0"])
57
+ s.add_development_dependency(%q<shoulda>, ["= 2.10.3"])
58
+ else
59
+ s.add_dependency(%q<httparty>, [">= 0.6.0"])
60
+ s.add_dependency(%q<json>, [">= 1.4.5"])
61
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
62
+ s.add_dependency(%q<i18n>, [">= 0"])
63
+ s.add_dependency(%q<shoulda>, ["= 2.10.3"])
64
+ end
65
+ else
66
+ s.add_dependency(%q<httparty>, [">= 0.6.0"])
67
+ s.add_dependency(%q<json>, [">= 1.4.5"])
68
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
69
+ s.add_dependency(%q<i18n>, [">= 0"])
70
+ s.add_dependency(%q<shoulda>, ["= 2.10.3"])
71
+ end
72
+ end
73
+
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xbmc-client
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Christoph Olszowka
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-25 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: httparty
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 6
31
+ - 0
32
+ version: 0.6.0
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: json
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 1
45
+ - 4
46
+ - 5
47
+ version: 1.4.5
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: activesupport
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 3
60
+ - 0
61
+ - 0
62
+ version: 3.0.0
63
+ type: :runtime
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: i18n
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ type: :runtime
77
+ version_requirements: *id004
78
+ - !ruby/object:Gem::Dependency
79
+ name: shoulda
80
+ prerelease: false
81
+ requirement: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - "="
85
+ - !ruby/object:Gem::Version
86
+ segments:
87
+ - 2
88
+ - 10
89
+ - 3
90
+ version: 2.10.3
91
+ type: :development
92
+ version_requirements: *id005
93
+ description: A simple API client for the XBMC JSON-RPC API
94
+ email: christoph at olszowka de
95
+ executables: []
96
+
97
+ extensions: []
98
+
99
+ extra_rdoc_files:
100
+ - LICENSE
101
+ - README.rdoc
102
+ files:
103
+ - .document
104
+ - .gitignore
105
+ - .rvmrc
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - LICENSE
109
+ - README.rdoc
110
+ - Rakefile
111
+ - VERSION
112
+ - examples/audio_library.rb
113
+ - lib/ruby_ext.rb
114
+ - lib/xbmc-client.rb
115
+ - lib/xbmc/command.rb
116
+ - test/helper.rb
117
+ - test/test_xbmc-client.rb
118
+ - xbmc-client.gemspec
119
+ has_rdoc: true
120
+ homepage: http://github.com/colszowka/xbmc-client
121
+ licenses: []
122
+
123
+ post_install_message:
124
+ rdoc_options:
125
+ - --charset=UTF-8
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ segments:
134
+ - 0
135
+ version: "0"
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ segments:
142
+ - 0
143
+ version: "0"
144
+ requirements: []
145
+
146
+ rubyforge_project:
147
+ rubygems_version: 1.3.7
148
+ signing_key:
149
+ specification_version: 3
150
+ summary: A simple API client for the XBMC JSON-RPC API
151
+ test_files:
152
+ - test/helper.rb
153
+ - test/test_xbmc-client.rb
154
+ - examples/audio_library.rb