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 :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_prefix, ['assets']
@ -33,7 +33,6 @@ module MPDClient
MPDClient.connect!
# TODO: Figure out why failing to supplying args breaks stuff
MPDClient.listen do
on :song do
each_conn do |conn|
@ -51,7 +50,7 @@ module MPDClient
on :playlist do
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"
end
end
@ -84,7 +83,7 @@ module MPDClient
get '/albums' do
content_type 'application/json'
if params[:artist]
Album.by_artist(params[:artist]).sort.map(&:to_h).to_json
Album.by_artist(params[:artist]).sort.to_json
else
Album.all.to_json
end
@ -92,21 +91,21 @@ module MPDClient
get '/artists' do
content_type 'application/json'
Artist.all.map(&:to_h).to_json
Artist.all.to_json
end
get '/songs' do
content_type 'application/json'
if query = params.slice(:artist, :album) and !query.empty?
Song.by(**query).map(&:to_h).to_json
Song.by(**query).to_json
else
Song.all.sort.map(&:to_h).to_json
Song.all.sort.to_json
end
end
get '/queue' do
content_type 'application/json'
{ data: Song.queue.map(&:to_h) }.to_json
{ data: Song.queue }.to_json
end
put '/control/play' do

View File

@ -2,10 +2,11 @@ require 'forwardable'
require 'ruby-mpd'
require 'set'
require File.expand_path('mpd_client/class_to_proc', __dir__)
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 :Song, File.expand_path('mpd_client/song.rb', __dir__)
autoload :Album, File.expand_path('mpd_client/album.rb', __dir__)

View File

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

View File

@ -2,6 +2,7 @@ module MPDClient
class Artist
include ClassToProc
include Jsonable
attr :name
@ -9,6 +10,12 @@ module MPDClient
@name = name
end
def to_h
{
name: name
}
end
class << self
def all
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 Comparable
include Jsonable
extend Forwardable
delegate %i(id track artist album title genre date time pos) => :@song