Yet another Music Player Daemon (MPD) client library written entirely in Ruby.
mpd_client
is a Ruby port of the python-mpd library.
Add this line to your application Gemfile
:
gem 'mpd_client'
And then execute:
bundle
Or install it yourself as:
gem install mpd_client
All functionality is contained in the MPD::Client
class. Creating an instance of this class is as simple as:
require 'mpd_client'
client = MPD::Client.new
Once you have an instance of the MPD::Client
class, start by connecting to the server:
client.connect('localhost', 6600)
or Unix domain socket
client.connect('/var/run/mpd/socket')
The client library can be used as follows:
puts client.mpd_version # print the mpd version
puts client.search('title', 'ruby') # print the result of the command 'search title ruby'
client.close # send the close command
client.disconect # disconnect from the server
Command lists are also supported using command_list_ok_begin
and command_list_end
:
client.command_list_ok_begin # start a command list
client.update # insert the update command into the list
client.status # insert the status command into the list
client.command_list_end # result will be a Array with the results
Some commands can return binary data.
require 'mpd_client'
client = MPD::Client.new
client.connect('localhost', 6600)
if (current_song = client.currentsong)
data, io = client.readpicture(current_song['file'])
io # StringIO
data # => {"size"=>"322860", "type"=>"image/jpeg", "binary"=>"3372"}
File.write('cover.jpg', io.string)
end
The above will locate album art for the current song and save image to cover.jpg
file.
Some commands(e.g. move
, delete
, load
, shuffle
, playlistinfo
) support integer ranges([START:END]
) as argument. This is done in mpd_client
by using two element array:
# move the first three songs after the fifth number in the playlist
client.move([0, 3], 5)
Second element can be omitted. MPD will assumes the biggest possible number then:
# delete all songs from the current playlist, except for the firts ten
client.delete([10,])
Default logger for all MPD::Client instances:
require 'logger'
require 'mpd_client'
MPD::Client.log = Logger.new($stderr)
client = MPD::Client.new
Sets the logger used by this instance of MPD::Client:
require 'logger'
require 'mpd_client'
client = MPD::Client.new
client.log = Logger.new($stderr)
For more information about logging configuration, see Logger
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
git checkout -b my-new-feature
) git commit -am 'Added some feature'
) git push origin my-new-feature
) Copyright (c) 2012-2022 by Anton Maminov
This library is distributed under the MIT license. Please see the LICENSE file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
# MPD::Client
[![Build Status](https://badgen.net/travis/mamantoha/mpd_client)](https://travis-ci.org/mamantoha/mpd_client)
[![Gem Version](https://badge.fury.io/rb/mpd_client.svg)](https://badge.fury.io/rb/mpd_client)
Yet another Music Player Daemon (MPD) client library written entirely in Ruby.
`mpd_client` is a Ruby port of the [python-mpd](https://github.com/Mic92/python-mpd2) library.
## Installation
Add this line to your application `Gemfile`:
```ruby
gem 'mpd_client'
```
And then execute:
```console
bundle
```
Or install it yourself as:
```console
gem install mpd_client
```
## Usage
All functionality is contained in the `MPD::Client` class. Creating an instance of this class is as simple as:
```ruby
require 'mpd_client'
client = MPD::Client.new
```
Once you have an instance of the `MPD::Client` class, start by connecting to the server:
```ruby
client.connect('localhost', 6600)
```
or Unix domain socket
```ruby
client.connect('/var/run/mpd/socket')
```
The client library can be used as follows:
```ruby
puts client.mpd_version # print the mpd version
puts client.search('title', 'ruby') # print the result of the command 'search title ruby'
client.close # send the close command
client.disconect # disconnect from the server
```
Command lists are also supported using `command_list_ok_begin` and `command_list_end`:
```ruby
client.command_list_ok_begin # start a command list
client.update # insert the update command into the list
client.status # insert the status command into the list
client.command_list_end # result will be a Array with the results
```
### Binary responses
Some commands can return binary data.
```ruby
require 'mpd_client'
client = MPD::Client.new
client.connect('localhost', 6600)
if (current_song = client.currentsong)
data, io = client.readpicture(current_song['file'])
io # StringIO
data # => {"size"=>"322860", "type"=>"image/jpeg", "binary"=>"3372"}
File.write('cover.jpg', io.string)
end
```
The above will locate album art for the current song and save image to `cover.jpg` file.
### Ranges
Some commands(e.g. `move`, `delete`, `load`, `shuffle`, `playlistinfo`) support integer ranges(`[START:END]`) as argument. This is done in `mpd_client` by using two element array:
```ruby
# move the first three songs after the fifth number in the playlist
client.move([0, 3], 5)
```
Second element can be omitted. MPD will assumes the biggest possible number then:
```ruby
# delete all songs from the current playlist, except for the firts ten
client.delete([10,])
```
### Logging
Default logger for all MPD::Client instances:
```ruby
require 'logger'
require 'mpd_client'
MPD::Client.log = Logger.new($stderr)
client = MPD::Client.new
```
Sets the logger used by this instance of MPD::Client:
```ruby
require 'logger'
require 'mpd_client'
client = MPD::Client.new
client.log = Logger.new($stderr)
```
For more information about logging configuration, see [Logger](https://ruby-doc.org/stdlib-2.5.1/libdoc/logger/rdoc/Logger.html)
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
## License and Author
Copyright (c) 2012-2022 by Anton Maminov
This library is distributed under the MIT license. Please see the LICENSE file.