ruby-mpd 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 49bd6de309c1177f46f4ab1c1f049cf5139bebe4
4
- data.tar.gz: 718d011451c59c356bf32260fd2c512dc444e68b
3
+ metadata.gz: 3d1c4e3976d91cea9da096a4d4c16c3c0cc6a5e4
4
+ data.tar.gz: 632f1d4b4928a249c28446b8202aa930dbbce9f5
5
5
  SHA512:
6
- metadata.gz: 4c78a607aa4a0242dae9535c9404800352579c9566bb8a6b4e19445fc32183e524a194fd57bcf7c9d7ca83a2cbbb5167ab19621bee7072d056372002cc801dea
7
- data.tar.gz: d7484a6502644919540a4148782b39e67d85cc4ac78f3cab32b525e52cf5168639273f45b2162202e08195c37377fc183066a45409924a0edb3df673b063800e
6
+ metadata.gz: d30486b01ba069e82431f03b66743f48a6d8c3c943a1a2794c9dcc3543f2d17da5c939717101ccc3595982ebe15850e4011b56cffdd00c01c10e5dec5e13915e
7
+ data.tar.gz: 70d07b44d2c8b10d41f249822135d8f74ae638143ab2294affe08ed54db6127b2563f5bb300482e24f1fe31a8ba0d3d8927c6a077eee0d8351390a548a5ae315
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ cache: bundler
5
+ before_install:
6
+ - gem install bundler
data/Gemfile CHANGED
@@ -3,4 +3,6 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in ruby-mpd.gemspec
4
4
  gemspec
5
5
 
6
- gem 'pry'
6
+ gem 'pry-byebug'
7
+ gem 'pry-stack_explorer'
8
+ gem 'simplecov', :require => false, :group => :tes
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # ruby-mpd
2
2
 
3
+ [![Build Status](https://travis-ci.org/archSeer/ruby-mpd.svg?branch=master)](https://travis-ci.org/archSeer/ruby-mpd)
4
+ [![Code Climate](https://codeclimate.com/github/archSeer/ruby-mpd/badges/gpa.svg)](https://codeclimate.com/github/archSeer/ruby-mpd)
5
+
3
6
  ruby-mpd is a powerful object-oriented client for the [Music Player Daemon](http://www.musicpd.org), forked from librmpd. librmpd is as of writing outdated by 6 years! This library tries to act as a successor, originally using librmpd as a base, however almost all of the codebase was rewritten. ruby-mpd supports all "modern" MPD features as well as callbacks.
4
7
 
5
8
  ## MPD Protocol
@@ -45,7 +48,7 @@ mpd.disconnect
45
48
  Once connected, you can issue commands to talk to the server.
46
49
 
47
50
  ```ruby
48
- mpd.connect
51
+ mpd.connect
49
52
  mpd.play if mpd.stopped?
50
53
  song = mpd.current_song
51
54
  puts "Current Song: #{song.artist} - #{song.title}"
@@ -178,7 +181,7 @@ end
178
181
  ```
179
182
 
180
183
  One can also use separate methods or Procs and whatnot, just pass them in as a parameter.
181
-
184
+
182
185
  ```ruby
183
186
  # Using a Proc
184
187
  proc = Proc.new { |volume| puts “Volume was set to #{volume}.” }
@@ -207,7 +210,7 @@ ruby-mpd supports callbacks for any of the keys returned by `MPD#status`, as wel
207
210
  * *songid*: playlist songid of the current song stopped on or playing.
208
211
  * *nextsong*: playlist song number of the next song to be played.
209
212
  * *nextsongid*: playlist songid of the next song to be played.
210
- * *time*: Returns two variables, `total` and `elapsed`, Integers representing seconds.
213
+ * *time*: Returns two integers, `elapsed` and `total`, Integers representing seconds.
211
214
  * *elapsed*: Float, representing total time elapsed within the current song, but with higher accuracy.
212
215
  * *bitrate*: instantaneous bitrate in kbps.
213
216
  * *xfade*: crossfade in seconds
@@ -218,7 +221,7 @@ ruby-mpd supports callbacks for any of the keys returned by `MPD#status`, as wel
218
221
  * *error*: if there is an error, returns message here
219
222
 
220
223
  * *connection*: Are we connected to the daemon? true or false
221
-
224
+
222
225
  Note that if the callback returns more than one value, the callback needs more arguments in order to recieve those values:
223
226
 
224
227
  ```ruby
@@ -266,7 +269,13 @@ Idle seems like a possible way to reimplement callbacks; make a separate connect
266
269
 
267
270
  ### Tests
268
271
 
269
- Tests fail at the moment, as they are 6 years old. The entire MPD server mock class either needs to be rewritten, or a `mpd.conf` along with a sample database and instructions for a controlled environment needs to be written.
272
+ There is the beginings of a test suite, that can be ran with:
273
+
274
+ ```
275
+ $ bin/rspec spec/
276
+ ```
277
+
278
+ The entire MPD server mock class either needs to be rewritten, or a `mpd.conf` along with a sample database and instructions for a controlled environment needs to be written.
270
279
 
271
280
  ## TODO list
272
281
 
data/Rakefile CHANGED
@@ -5,6 +5,12 @@ Rake::TestTask.new(:test) do |test|
5
5
  test.verbose = true
6
6
  end
7
7
 
8
+ begin
9
+ require 'rspec/core/rake_task'
10
+ RSpec::Core::RakeTask.new(:spec)
11
+ rescue LoadError
12
+ end
13
+
8
14
  desc "Open an irb session preloaded with this API"
9
15
  task :console do
10
16
  require 'ruby-mpd'
@@ -13,4 +19,4 @@ task :console do
13
19
  IRB.start
14
20
  end
15
21
 
16
- task :default => :test
22
+ task :default => [:test, :spec]
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rspec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rspec-core', 'rspec')
@@ -186,11 +186,11 @@ class MPD
186
186
  # @return (see #handle_server_response)
187
187
  # @raise [MPDError] if the command failed.
188
188
  def send_command(command, *args)
189
- raise ConnectionError, "Not connected to the server!" unless @socket
189
+ raise ConnectionError, "Not connected to the server!" unless socket
190
190
 
191
191
  @mutex.synchronize do
192
192
  begin
193
- @socket.puts convert_command(command, *args)
193
+ socket.puts convert_command(command, *args)
194
194
  response = handle_server_response
195
195
  return parse_response(command, response)
196
196
  rescue Errno::EPIPE
@@ -253,7 +253,7 @@ private
253
253
  def handle_server_response
254
254
  msg = ''
255
255
  while true
256
- case line = @socket.gets
256
+ case line = socket.gets
257
257
  when "OK\n", nil
258
258
  break
259
259
  when /^ACK/
@@ -4,7 +4,7 @@ class MPD
4
4
  # Parser module, being able to parse messages to and from the MPD daemon format.
5
5
  # @todo There are several parser hacks. Time is an array in status and a normal
6
6
  # string in MPD::Song, so we do`@time = options.delete(:time) { [nil] }.first`
7
- # to hack the array return. Playlist names are strings, whilst in status it's
7
+ # to hack the array return. Playlist names are strings, whilst in status it's
8
8
  # and int, so we parse it as an int if it's parsed as non-zero (if it's 0 it's a string)
9
9
  # and to fix numeric name playlists (123.m3u), we convert the name to_s inside
10
10
  # MPD::Playlist too.
@@ -73,7 +73,13 @@ class MPD
73
73
  Time.at(value.to_i)
74
74
  elsif key == :"last-modified"
75
75
  Time.iso8601(value)
76
- elsif [:time, :audio].include? key
76
+ elsif key == :time
77
+ if value.include?(':')
78
+ value.split(':').map(&:to_i)
79
+ else
80
+ [nil, value.to_i]
81
+ end
82
+ elsif key == :audio
77
83
  value.split(':').map(&:to_i)
78
84
  else
79
85
  value.force_encoding('UTF-8')
@@ -114,7 +120,7 @@ class MPD
114
120
 
115
121
  # Remove lines which we don't want.
116
122
  def filter_lines(string, filter)
117
- string.lines.reject {|line| line =~ /(#{filter.join('|')}):/}.join
123
+ string.lines.reject {|line| line =~ /(#{filter.join('|')}):/i}.join
118
124
  end
119
125
 
120
126
  # Make chunks from string.
@@ -25,14 +25,16 @@ class MPD
25
25
  # @return [Array<MPD::Song>] songs in the playlist.
26
26
  def songs
27
27
  result = @mpd.send_command(:listplaylistinfo, @name)
28
- if result.to_s =~ URI::regexp
29
- Song.new(@mpd, {:file => result, :time => 0})
30
- else
31
- result.map {|hash| Song.new(@mpd, hash) }
28
+ result.map do |hash|
29
+ if hash[:file] && !hash[:file].match(/^(https?:\/\/)?/)[0].empty?
30
+ Song.new(@mpd, {:file => hash[:file], :time => [0]})
31
+ else
32
+ Song.new(@mpd, hash)
33
+ end
32
34
  end
33
35
  rescue TypeError
34
36
  puts "Files inside Playlist '#{@name}' do not exist!"
35
- return nil
37
+ return []
36
38
  rescue NotFound
37
39
  return [] # we rescue in the case the playlist doesn't exist.
38
40
  end
@@ -21,7 +21,7 @@ class MPD
21
21
  def play(pos = nil)
22
22
  if pos.is_a?(Hash)
23
23
  if pos[:id]
24
- send_command :playid, priority, pos[:id]
24
+ send_command :playid, pos[:id]
25
25
  else
26
26
  raise ArgumentError, 'Only :id key is allowed!'
27
27
  end
@@ -91,7 +91,7 @@ class MPD
91
91
  else
92
92
  command = options[:strict] ? :find : :search
93
93
  end
94
-
94
+
95
95
  response = send_command(command, params)
96
96
  if response == true
97
97
  return true
@@ -10,7 +10,7 @@ class MPD::Song
10
10
  def initialize(mpd, options)
11
11
  @mpd = mpd
12
12
  @data = {} # allowed fields are @types + :file
13
- @time = options.delete(:time) { [nil] }.first # HAXX for array return
13
+ @time = options.delete(:time) # an array of 2 items where last is time
14
14
  @file = options.delete(:file)
15
15
  @title = options.delete(:title)
16
16
  @artist = options.delete(:artist)
@@ -19,23 +19,42 @@ class MPD::Song
19
19
  @data.merge! options
20
20
  end
21
21
 
22
- # Two songs are the same when they are the same file.
22
+ # Two songs are the same when they share the same hash.
23
23
  def ==(another)
24
- self.class == another.class && self.file == another.file
24
+ to_h == another.to_h
25
+ end
26
+
27
+ def to_h
28
+ {
29
+ time: @time,
30
+ file: @file,
31
+ title: @title,
32
+ artist: @artist,
33
+ album: @album,
34
+ albumartist: @albumartist
35
+ }.merge(@data)
36
+ end
37
+
38
+ def elapsed
39
+ @time.first
40
+ end
41
+
42
+ def track_length
43
+ @time.last
25
44
  end
26
45
 
27
46
  # @return [String] A formatted representation of the song length ("1:02")
28
47
  def length
29
- return '--:--' if @time.nil?
30
- "#{@time / 60}:#{"%02d" % (@time % 60)}"
48
+ return '--:--' if track_length.nil?
49
+ "#{track_length / 60}:#{"%02d" % (track_length % 60)}"
31
50
  end
32
-
51
+
33
52
  # Retrieve "comments" metadata from a file and cache it in the object.
34
53
  #
35
54
  # @return [Hash] Key value pairs from "comments" metadata on a file.
36
55
  # @return [Boolean] True if comments are empty
37
56
  def comments
38
- @comments ||= @mpd.send_command :readcomments, @file
57
+ @comments ||= @mpd.send_command :readcomments, @file
39
58
  end
40
59
 
41
60
  # Pass any unknown calls over to the data hash.
@@ -49,4 +68,6 @@ class MPD::Song
49
68
  raise NoMethodError, "#{m}"
50
69
  end
51
70
  end
71
+
72
+ alias :eql? :==
52
73
  end
@@ -1,3 +1,3 @@
1
1
  class MPD
2
- VERSION = '0.3.2'
2
+ VERSION = '0.3.3'
3
3
  end
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
7
7
  spec.name = "ruby-mpd"
8
8
  spec.version = MPD::VERSION
9
9
  spec.authors = ["Blaž Hrastnik"]
10
- spec.email = ["speed.the.bboy@gmail.com"]
10
+ spec.email = ["blaz@mxxn.io"]
11
11
 
12
12
  spec.summary = "Modern client library for MPD"
13
13
  spec.description = "A powerful, modern and feature complete library for the Music Player Daemon"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-mpd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blaž Hrastnik
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-05-28 00:00:00.000000000 Z
11
+ date: 2015-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,18 +69,21 @@ dependencies:
69
69
  description: A powerful, modern and feature complete library for the Music Player
70
70
  Daemon
71
71
  email:
72
- - speed.the.bboy@gmail.com
72
+ - blaz@mxxn.io
73
73
  executables: []
74
74
  extensions: []
75
75
  extra_rdoc_files: []
76
76
  files:
77
77
  - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
78
80
  - COPYING
79
81
  - Gemfile
80
82
  - LICENSE.txt
81
83
  - README.md
82
84
  - Rakefile
83
85
  - bin/console
86
+ - bin/rspec
84
87
  - bin/setup
85
88
  - lib/ruby-mpd.rb
86
89
  - lib/ruby-mpd/exceptions.rb