ruby-mpd-client 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +36 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/mpd/client.rb +34 -0
- data/lib/mpd/client/commands.rb +10 -0
- data/lib/mpd/client/commands/playback_control.rb +36 -0
- data/lib/mpd/client/commands/playback_options.rb +36 -0
- data/lib/mpd/client/connection.rb +39 -0
- data/lib/mpd/client/server_response.rb +46 -0
- data/lib/mpd/errors.rb +4 -0
- data/lib/mpd/version.rb +3 -0
- data/lib/ruby-mpd-client.rb +3 -0
- data/ruby-mpd-client.gemspec +26 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0a5f269daaee86e7aae52ba8aaf7563405297cdd
|
4
|
+
data.tar.gz: c67bfa95cb8bbd8d0098ca1a60138f27916f993a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ca6a6b9608feec8ff4d23724589f3b03942b4ed0274a3e2ec2eb61e30761923c76547060891fa5e6501fd756134482506bc27559f29af830a128a08d5ab203a4
|
7
|
+
data.tar.gz: 63f2c58e74605215f01940d367ecde94ee93c1909cd3111b10a3d72cc3773668ef9da3bdc85b80b89c7fd1a68e2b7da7bd2d16f308a404e334d07c1d6ea07222
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Dmitriy Non
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Ruby MPD client
|
2
|
+
|
3
|
+
Yet another ruby mpd client.
|
4
|
+
|
5
|
+
This gem is for my own usage (practicing my programming skills). But
|
6
|
+
I would be glad to know if you found it useful.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'ruby-mpd-client'
|
12
|
+
```
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
It does not do much
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
require 'ruby-mpd-client'
|
20
|
+
|
21
|
+
mpd = MPD::Client.new(host: 'localhost')
|
22
|
+
mpd.volume(50)
|
23
|
+
mpd.random(true)
|
24
|
+
mpd.next
|
25
|
+
# and so on
|
26
|
+
```
|
27
|
+
|
28
|
+
## Contributing
|
29
|
+
|
30
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Nondv/ruby-mpd-client.
|
31
|
+
|
32
|
+
|
33
|
+
## License
|
34
|
+
|
35
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
36
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require 'ruby-mpd-client'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/mpd/client.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'mpd/client/connection'
|
2
|
+
require 'mpd/client/server_response'
|
3
|
+
require 'mpd/client/commands'
|
4
|
+
|
5
|
+
module MPD
|
6
|
+
class Client
|
7
|
+
attr_reader :connection
|
8
|
+
|
9
|
+
def initialize(host: 'localhost', port: 6600)
|
10
|
+
@connection = Connection.new(host: host, port: port)
|
11
|
+
end
|
12
|
+
|
13
|
+
def status
|
14
|
+
response = execute('status')
|
15
|
+
response.to_h
|
16
|
+
end
|
17
|
+
|
18
|
+
def execute(command)
|
19
|
+
ensure_connection_established
|
20
|
+
connection.puts(command)
|
21
|
+
response = ServerResponse.from_connection(connection)
|
22
|
+
raise(MpdError, response.status) if response.error?
|
23
|
+
response
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def ensure_connection_established
|
29
|
+
return if connection.connected?
|
30
|
+
connection.connect
|
31
|
+
connection.gets
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module MPD
|
2
|
+
class Client
|
3
|
+
module Commands
|
4
|
+
# https://www.musicpd.org/doc/protocol/playback_commands.html
|
5
|
+
module PlaybackControl
|
6
|
+
def play(position: nil, id: nil)
|
7
|
+
raise(ArgumentError, 'too many arguments') if position && id
|
8
|
+
|
9
|
+
if position
|
10
|
+
execute "play #{position}"
|
11
|
+
elsif id
|
12
|
+
execute "playid #{id}"
|
13
|
+
else
|
14
|
+
execute 'pause 0'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def pause
|
19
|
+
execute 'pause 1'
|
20
|
+
end
|
21
|
+
|
22
|
+
def stop
|
23
|
+
execute 'stop'
|
24
|
+
end
|
25
|
+
|
26
|
+
def previous
|
27
|
+
execute 'previous'
|
28
|
+
end
|
29
|
+
|
30
|
+
def next
|
31
|
+
execute 'next'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module MPD
|
2
|
+
class Client
|
3
|
+
module Commands
|
4
|
+
# https://www.musicpd.org/doc/protocol/playback_option_commands.html
|
5
|
+
module PlaybackOptions
|
6
|
+
def consume(state)
|
7
|
+
execute "consume #{state ? 1 : 0}"
|
8
|
+
end
|
9
|
+
|
10
|
+
def crossfade(state)
|
11
|
+
execute "crossfade #{state ? 1 : 0}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def random(state)
|
15
|
+
execute "random #{state ? 1 : 0}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def repeat(state)
|
19
|
+
execute "repeat #{state ? 1 : 0}"
|
20
|
+
end
|
21
|
+
|
22
|
+
def single(state)
|
23
|
+
execute "single #{state ? 1 : 0}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def volume(val)
|
27
|
+
execute "setvol #{val}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def change_volume_by(val)
|
31
|
+
execute("volume #{val}")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'socket'
|
2
|
+
|
3
|
+
module MPD
|
4
|
+
class Client
|
5
|
+
#
|
6
|
+
# It's `Client`'s internal class. Don't use it outside `Client`
|
7
|
+
#
|
8
|
+
class Connection
|
9
|
+
def initialize(host:, port:)
|
10
|
+
@host = host
|
11
|
+
@port = port
|
12
|
+
end
|
13
|
+
|
14
|
+
def connect
|
15
|
+
@socket = TCPSocket.open(host, port)
|
16
|
+
end
|
17
|
+
|
18
|
+
def puts(text)
|
19
|
+
raise ConnectionError unless connected?
|
20
|
+
socket.puts(text)
|
21
|
+
end
|
22
|
+
|
23
|
+
def gets
|
24
|
+
raise ConnectionError unless connected?
|
25
|
+
socket.gets
|
26
|
+
end
|
27
|
+
|
28
|
+
def connected?
|
29
|
+
socket && !socket.closed?
|
30
|
+
rescue
|
31
|
+
false
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
attr_reader :socket, :host, :port
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module MPD
|
2
|
+
class Client
|
3
|
+
#
|
4
|
+
# Just a frozen string with some useful methods
|
5
|
+
#
|
6
|
+
class ServerResponse < String
|
7
|
+
def self.from_connection(conn)
|
8
|
+
line = ''
|
9
|
+
buffer = []
|
10
|
+
while line != "OK\n" && !line.start_with?('ACK')
|
11
|
+
line = conn.gets
|
12
|
+
raise(ConnectionError) if line.nil?
|
13
|
+
buffer << line
|
14
|
+
end
|
15
|
+
|
16
|
+
new(buffer.join)
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(response_text)
|
20
|
+
super
|
21
|
+
freeze
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_h
|
25
|
+
key_value_pairs.to_h
|
26
|
+
end
|
27
|
+
|
28
|
+
def key_value_pairs
|
29
|
+
content.split("\n").map { |line| line.split.map(&:strip) }
|
30
|
+
end
|
31
|
+
|
32
|
+
# text without status
|
33
|
+
def content
|
34
|
+
lines[0..-2].join
|
35
|
+
end
|
36
|
+
|
37
|
+
def status
|
38
|
+
lines.last.chomp
|
39
|
+
end
|
40
|
+
|
41
|
+
def error?
|
42
|
+
status.start_with?('ACK')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/mpd/errors.rb
ADDED
data/lib/mpd/version.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mpd/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ruby-mpd-client"
|
8
|
+
spec.version = MPD::VERSION
|
9
|
+
spec.authors = ["Dmitriy Non"]
|
10
|
+
spec.email = ["non.dmitriy@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = 'yet another MPD client on Ruby'
|
13
|
+
spec.homepage = 'https://github.com/Nondv/ruby-mpd-client'
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-mpd-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dmitriy Non
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-07-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- non.dmitriy@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- bin/console
|
54
|
+
- bin/setup
|
55
|
+
- lib/mpd/client.rb
|
56
|
+
- lib/mpd/client/commands.rb
|
57
|
+
- lib/mpd/client/commands/playback_control.rb
|
58
|
+
- lib/mpd/client/commands/playback_options.rb
|
59
|
+
- lib/mpd/client/connection.rb
|
60
|
+
- lib/mpd/client/server_response.rb
|
61
|
+
- lib/mpd/errors.rb
|
62
|
+
- lib/mpd/version.rb
|
63
|
+
- lib/ruby-mpd-client.rb
|
64
|
+
- ruby-mpd-client.gemspec
|
65
|
+
homepage: https://github.com/Nondv/ruby-mpd-client
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata: {}
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 2.6.12
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: yet another MPD client on Ruby
|
89
|
+
test_files: []
|