summaryrefslogtreecommitdiff
path: root/libmpd/database.rb
blob: 4249f5ebb341dcd771be99c673ae841f62b352ca (plain) (blame)
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
#!/usr/bin/env ruby
#
#--
# Copyright 2009-2014 David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
# Distributed under the terms of the GNU General Public License v3.
# See http://www.gnu.org/licenses/gpl.txt for the full license text.
#++
#
# *Author*:: David Vazgenovich Shakaryan
# *License*:: GNU General Public License v3

# Collection of methods related to the database.
module MPDDatabase
  # Returns all directories.
  # If an argument is specified, list all subdirectories of that directory.
  def directories(uri = nil)
    command = 'lsinfo'
    command << ' "%s"' % uri if uri

    if uri
      path = Regexp.escape(uri + '/')
    else
      path = String.new
    end

    return send_request(command).scan(/^directory: (#{path}.+?)\n/).flatten
  end

  # Counts the number of songs in the database where _field_ is _value_, as
  # well as their total playtime.
  def count(field, value)
    return generate_hash(send_request('count %s "%s"' % [field, value]))
  end

  # Finds all songs in the database where _field_ is _value_.
  #
  # Possible field names: album, artist, title.
  def find(field, value)
    return split_and_hash(send_request('find %s "%s"' % [field, value]))
  end

  # Returns all files.
  # If an argument is specified, list all files in that directory.
  def files(uri = nil)
    command = 'lsinfo'
    command << ' "%s"' % uri if uri

    if uri
      path = Regexp.escape(uri + '/')
    else
      path = String.new
    end

    files = send_request(command)
    files = files.gsub(/^directory: .+?\n/, '').sub(/^playlist: .+/m, '')

    return split_and_hash(files)
  end

  # Finds all songs in the database where _field_ contains _value_.
  # Matching is not case-sensitive.
  #
  # Possible field names: album, artist, filename, title.
  def search(field, value)
    return split_and_hash(send_request('search %s "%s"' % [field, value]))
  end

  # Updates the database.
  # If an argument is given, update that particular file or directory.
  def update(uri = nil)
    command = 'update'
    command << ' "%s"' % uri if uri

    return send_request(command)
  end
end