1
0
mirror of https://github.com/danbee/mpd-client synced 2025-03-04 08:39:09 +00:00

Add Jsonable mixin to enable recursive generation

This commit is contained in:
Lee Machin 2013-12-11 13:23:22 +00:00
parent 83a7e0eac9
commit c3f6aadd0e
6 changed files with 36 additions and 10 deletions

View File

@ -23,7 +23,7 @@ module MPDClient
set server: 'thin' set server: 'thin'
set :root, File.expand_path('../', __dir__).to_s set :root, File.expand_path('../', __dir__)
set :assets_precompile, %w(app.js app.css *.png *.jpg *.svg *.eot *.ttf *.woff) set :assets_precompile, %w(app.js app.css *.png *.jpg *.svg *.eot *.ttf *.woff)
set :assets_prefix, ['assets'] set :assets_prefix, ['assets']
@ -33,7 +33,6 @@ module MPDClient
MPDClient.connect! MPDClient.connect!
# TODO: Figure out why failing to supplying args breaks stuff
MPDClient.listen do MPDClient.listen do
on :song do on :song do
each_conn do |conn| each_conn do |conn|
@ -51,7 +50,7 @@ module MPDClient
on :playlist do on :playlist do
each_conn do |conn| each_conn do |conn|
json = { type: 'queue', data: Song.queue.map(&:to_h) }.to_json json = { type: 'queue', data: Song.queue }.to_json
conn << "data: #{json}\n\n" conn << "data: #{json}\n\n"
end end
end end
@ -84,7 +83,7 @@ module MPDClient
get '/albums' do get '/albums' do
content_type 'application/json' content_type 'application/json'
if params[:artist] if params[:artist]
Album.by_artist(params[:artist]).sort.map(&:to_h).to_json Album.by_artist(params[:artist]).sort.to_json
else else
Album.all.to_json Album.all.to_json
end end
@ -92,21 +91,21 @@ module MPDClient
get '/artists' do get '/artists' do
content_type 'application/json' content_type 'application/json'
Artist.all.map(&:to_h).to_json Artist.all.to_json
end end
get '/songs' do get '/songs' do
content_type 'application/json' content_type 'application/json'
if query = params.slice(:artist, :album) and !query.empty? if query = params.slice(:artist, :album) and !query.empty?
Song.by(**query).map(&:to_h).to_json Song.by(**query).to_json
else else
Song.all.sort.map(&:to_h).to_json Song.all.sort.to_json
end end
end end
get '/queue' do get '/queue' do
content_type 'application/json' content_type 'application/json'
{ data: Song.queue.map(&:to_h) }.to_json { data: Song.queue }.to_json
end end
put '/control/play' do put '/control/play' do

View File

@ -2,10 +2,11 @@ require 'forwardable'
require 'ruby-mpd' require 'ruby-mpd'
require 'set' require 'set'
require File.expand_path('mpd_client/class_to_proc', __dir__)
module MPDClient module MPDClient
autoload :Jsonable, File.expand_path('mpd_client/jsonable.rb', __dir__)
autoload :ClassToProc, File.expand_path('mpd_client/class_to_proc.rb', __dir__)
autoload :Connection, File.expand_path('mpd_client/connection.rb', __dir__) autoload :Connection, File.expand_path('mpd_client/connection.rb', __dir__)
autoload :Song, File.expand_path('mpd_client/song.rb', __dir__) autoload :Song, File.expand_path('mpd_client/song.rb', __dir__)
autoload :Album, File.expand_path('mpd_client/album.rb', __dir__) autoload :Album, File.expand_path('mpd_client/album.rb', __dir__)

View File

@ -3,6 +3,8 @@ module MPDClient
include ClassToProc include ClassToProc
include Enumerable include Enumerable
include Jsonable
extend Forwardable extend Forwardable
delegate %i(artist genre) => :@first_song delegate %i(artist genre) => :@first_song

View File

@ -2,6 +2,7 @@ module MPDClient
class Artist class Artist
include ClassToProc include ClassToProc
include Jsonable
attr :name attr :name
@ -9,6 +10,12 @@ module MPDClient
@name = name @name = name
end end
def to_h
{
name: name
}
end
class << self class << self
def all def all
MPDClient.conn.artists.sort.map(&self) MPDClient.conn.artists.sort.map(&self)

View File

@ -0,0 +1,15 @@
module MPDClient
module Jsonable
def self.included(receiver)
receiver.send(:include, InstanceMethods)
end
module InstanceMethods
def to_json(*args)
to_h.to_json(*args)
end
end
end
end

View File

@ -3,6 +3,8 @@ module MPDClient
include ClassToProc include ClassToProc
include Comparable include Comparable
include Jsonable
extend Forwardable extend Forwardable
delegate %i(id track artist album title genre date time pos) => :@song delegate %i(id track artist album title genre date time pos) => :@song