From c3f6aadd0ecbc9adb525750e2f136a5660d9fb65 Mon Sep 17 00:00:00 2001 From: Lee Machin Date: Wed, 11 Dec 2013 13:23:22 +0000 Subject: [PATCH] Add Jsonable mixin to enable recursive generation --- bin/mpd_client | 15 +++++++-------- lib/mpd_client.rb | 5 +++-- lib/mpd_client/album.rb | 2 ++ lib/mpd_client/artist.rb | 7 +++++++ lib/mpd_client/jsonable.rb | 15 +++++++++++++++ lib/mpd_client/song.rb | 2 ++ 6 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 lib/mpd_client/jsonable.rb diff --git a/bin/mpd_client b/bin/mpd_client index da9e406..d0d1ffd 100755 --- a/bin/mpd_client +++ b/bin/mpd_client @@ -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 diff --git a/lib/mpd_client.rb b/lib/mpd_client.rb index 92e486e..3a07830 100644 --- a/lib/mpd_client.rb +++ b/lib/mpd_client.rb @@ -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__) diff --git a/lib/mpd_client/album.rb b/lib/mpd_client/album.rb index eedf71b..92d019e 100644 --- a/lib/mpd_client/album.rb +++ b/lib/mpd_client/album.rb @@ -3,6 +3,8 @@ module MPDClient include ClassToProc include Enumerable + include Jsonable + extend Forwardable delegate %i(artist genre) => :@first_song diff --git a/lib/mpd_client/artist.rb b/lib/mpd_client/artist.rb index 8b0bb3f..b4d5975 100644 --- a/lib/mpd_client/artist.rb +++ b/lib/mpd_client/artist.rb @@ -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) diff --git a/lib/mpd_client/jsonable.rb b/lib/mpd_client/jsonable.rb new file mode 100644 index 0000000..9502041 --- /dev/null +++ b/lib/mpd_client/jsonable.rb @@ -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 diff --git a/lib/mpd_client/song.rb b/lib/mpd_client/song.rb index cf96a93..41079bf 100644 --- a/lib/mpd_client/song.rb +++ b/lib/mpd_client/song.rb @@ -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