{"action":"created","comment":{"url":"https://api.github.com/repos/archSeer/ruby-mpd/pulls/comments/57851708","id":57851708,"diff_hunk":"@@ -150,17 +160,39 @@ def parse_response(command, string)\n       build_response(command, string)\n     end\n \n+    def parse_command_list(commands, string)\n+      [].tap do |results|\n+        string.split(\"list_OK\\n\").each do |str|\n+          command = commands.shift\n+          results << parse_response(command, str) unless str.empty?\n+        end\n+      end\n+    end\n+\n     # Parses the response into appropriate objects (either a single object,\n     # or an array of objects or an array of hashes).\n     #\n     # @return [Array<Hash>, Array<String>, String, Integer] Parsed response.\n-    def build_response(command, string)\n+    def build_response(command, string, force_hash=nil)\n       chunks = make_chunks(string)\n-      # if there are any new lines (more than one data piece), it's a hash, else an object.\n-      is_hash = chunks.any? { |chunk| chunk.include? \"\\n\" }\n+\n+      make_song  = SONG_COMMANDS.include?(command)\n+      make_plist = PLAYLIST_COMMANDS.include?(command)\n+      make_hash  = force_hash || make_song || make_plist || chunks.any?{ |chunk| chunk.include? \"\\n\" }\n \n       list = chunks.inject([]) do |result, chunk|\n-        result << (is_hash ? build_hash(chunk) : parse_line(chunk)[1]) # parse_line(chunk)[1] is object\n+        result << (make_hash ? build_hash(chunk) : parse_line(chunk)[1]) # parse_line(chunk)[1] is object\n+      end\n+\n+      if make_song","path":"lib/ruby-mpd/parser.rb","position":96,"original_position":96,"commit_id":"9d8c1878dddee51ed202e0586a5323921411e2e8","original_commit_id":"9d8c1878dddee51ed202e0586a5323921411e2e8","user":{"login":"archSeer","id":1372918,"avatar_url":"https://avatars.githubusercontent.com/u/1372918?v=3","gravatar_id":"","url":"https://api.github.com/users/archSeer","html_url":"https://github.com/archSeer","followers_url":"https://api.github.com/users/archSeer/followers","following_url":"https://api.github.com/users/archSeer/following{/other_user}","gists_url":"https://api.github.com/users/archSeer/gists{/gist_id}","starred_url":"https://api.github.com/users/archSeer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/archSeer/subscriptions","organizations_url":"https://api.github.com/users/archSeer/orgs","repos_url":"https://api.github.com/users/archSeer/repos","events_url":"https://api.github.com/users/archSeer/events{/privacy}","received_events_url":"https://api.github.com/users/archSeer/received_events","type":"User","site_admin":false},"body":"You can use a splat with a when statement:\r\n```\r\ncase command\r\nwhen *SONG_COMMANDS\r\nwhen *PLAYLIST_COMMANDS\r\nend\r\n```","created_at":"2016-03-30T08:21:41Z","updated_at":"2016-03-30T08:21:41Z","html_url":"https://github.com/archSeer/ruby-mpd/pull/76#discussion_r57851708","pull_request_url":"https://api.github.com/repos/archSeer/ruby-mpd/pulls/76","_links":{"self":{"href":"https://api.github.com/repos/archSeer/ruby-mpd/pulls/comments/57851708"},"html":{"href":"https://github.com/archSeer/ruby-mpd/pull/76#discussion_r57851708"},"pull_request":{"href":"https://api.github.com/repos/archSeer/ruby-mpd/pulls/76"}}},"pull_request":{"url":"https://api.github.com/repos/archSeer/ruby-mpd/pulls/76","id":62024484,"html_url":"https://github.com/archSeer/ruby-mpd/pull/76","diff_url":"https://github.com/archSeer/ruby-mpd/pull/76.diff","patch_url":"https://github.com/archSeer/ruby-mpd/pull/76.patch","issue_url":"https://api.github.com/repos/archSeer/ruby-mpd/issues/76","number":76,"state":"open","locked":false,"title":"Command Lists (Automatic Result Types)","user":{"login":"Phrogz","id":14778,"avatar_url":"https://avatars.githubusercontent.com/u/14778?v=3","gravatar_id":"","url":"https://api.github.com/users/Phrogz","html_url":"https://github.com/Phrogz","followers_url":"https://api.github.com/users/Phrogz/followers","following_url":"https://api.github.com/users/Phrogz/following{/other_user}","gists_url":"https://api.github.com/users/Phrogz/gists{/gist_id}","starred_url":"https://api.github.com/users/Phrogz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Phrogz/subscriptions","organizations_url":"https://api.github.com/users/Phrogz/orgs","repos_url":"https://api.github.com/users/Phrogz/repos","events_url":"https://api.github.com/users/Phrogz/events{/privacy}","received_events_url":"https://api.github.com/users/Phrogz/received_events","type":"User","site_admin":false},"body":"Implements command lists in a manner that (a) allows various results to be interleaved and still return the correct types, and (b) allows playlist objects to be used directly in a command list.\r\n\r\n```ruby\r\n# Do not parse response (for speed)\r\n@mpd.command_list do\r\n  add song\r\n  volume 50\r\n  shuffle\r\nend\r\n\r\n# Array of values from each response line\r\nids = @mpd.command_list(results:true) do\r\n  addid song1\r\n  addid song2\r\nend\r\n#=> [113,114]\r\n\r\n# Arrays of Songs\r\nsongs = @mpd.command_list(results:true) do\r\n  where genre:'electronic'\r\n  where genre:'trance'\r\nend\r\n#=> [\r\n#=>    [ <MPD::Song>, <MPD::Song>, … ],\r\n#=>    [ <MPD::Song>, <MPD::Song>, … ]\r\n#=> ]\r\n\r\n# Array of Playlists\r\nresults = @mpd.command_list(results:true) do\r\n  clear\r\n  where({genre:'electronic'},{add:true})\r\n  save 'new-playlist'\r\n  playlists\r\nend\r\n#=> [\r\n#=>    [ <MPD::Playlist>, <MPD::Playlist>, … ]\r\n#=> ]\r\n\r\n# Using Playlist inside Command List\r\ndef shuffle_playlist( playlist )\r\n  song_count = playlist.songs.length\r\n  @mpd.command_list do\r\n    (song_count-1).downto(1){ |i| playlist.move i, rand(i+1) }\r\n  end\r\nend\r\n```\r\n\r\nThis has a [small class](https://github.com/Phrogz/ruby-mpd/blob/4a885a619bdd24df7b4f77aa5ddc7cfdb1e102ac/lib/ruby-mpd/plugins/command_list.rb#L82) used for the DSL, but that is not strictly necessary. Though it doesn't look as nice, the following code works:\r\n\r\n```ruby\r\n@mpd.command_list do\r\n  @mpd.clear\r\n  @mpd.addid my_song\r\n  @mpd.songs\r\nend\r\n```\r\n\r\n…because starting a `command_list` causes all other commands to ignore the response, but to record the commands used for later parsing of the command_list results.","created_at":"2016-03-08T04:51:44Z","updated_at":"2016-03-30T08:21:41Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ef6c5fa4fcf6705ab63bd17fd4946d37e67702af","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/archSeer/ruby-mpd/pulls/76/commits","review_comments_url":"https://api.github.com/repos/archSeer/ruby-mpd/pulls/76/comments","review_comment_url":"https://api.github.com/repos/archSeer/ruby-mpd/pulls/comments{/number}","comments_url":"https://api.github.com/repos/archSeer/ruby-mpd/issues/76/comments","statuses_url":"https://api.github.com/repos/archSeer/ruby-mpd/statuses/9d8c1878dddee51ed202e0586a5323921411e2e8","head":{"label":"Phrogz:command-lists-magic","ref":"command-lists-magic","sha":"9d8c1878dddee51ed202e0586a5323921411e2e8","user":{"login":"Phrogz","id":14778,"avatar_url":"https://avatars.githubusercontent.com/u/14778?v=3","gravatar_id":"","url":"https://api.github.com/users/Phrogz","html_url":"https://github.com/Phrogz","followers_url":"https://api.github.com/users/Phrogz/followers","following_url":"https://api.github.com/users/Phrogz/following{/other_user}","gists_url":"https://api.github.com/users/Phrogz/gists{/gist_id}","starred_url":"https://api.github.com/users/Phrogz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Phrogz/subscriptions","organizations_url":"https://api.github.com/users/Phrogz/orgs","repos_url":"https://api.github.com/users/Phrogz/repos","events_url":"https://api.github.com/users/Phrogz/events{/privacy}","received_events_url":"https://api.github.com/users/Phrogz/received_events","type":"User","site_admin":false},"repo":{"id":45793720,"name":"ruby-mpd","full_name":"Phrogz/ruby-mpd","owner":{"login":"Phrogz","id":14778,"avatar_url":"https://avatars.githubusercontent.com/u/14778?v=3","gravatar_id":"","url":"https://api.github.com/users/Phrogz","html_url":"https://github.com/Phrogz","followers_url":"https://api.github.com/users/Phrogz/followers","following_url":"https://api.github.com/users/Phrogz/following{/other_user}","gists_url":"https://api.github.com/users/Phrogz/gists{/gist_id}","starred_url":"https://api.github.com/users/Phrogz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Phrogz/subscriptions","organizations_url":"https://api.github.com/users/Phrogz/orgs","repos_url":"https://api.github.com/users/Phrogz/repos","events_url":"https://api.github.com/users/Phrogz/events{/privacy}","received_events_url":"https://api.github.com/users/Phrogz/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Phrogz/ruby-mpd","description":"ruby-mpd is a powerful object-oriented Music Player Daemon library, forked from librmpd.","fork":true,"url":"https://api.github.com/repos/Phrogz/ruby-mpd","forks_url":"https://api.github.com/repos/Phrogz/ruby-mpd/forks","keys_url":"https://api.github.com/repos/Phrogz/ruby-mpd/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Phrogz/ruby-mpd/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Phrogz/ruby-mpd/teams","hooks_url":"https://api.github.com/repos/Phrogz/ruby-mpd/hooks","issue_events_url":"https://api.github.com/repos/Phrogz/ruby-mpd/issues/events{/number}","events_url":"https://api.github.com/repos/Phrogz/ruby-mpd/events","assignees_url":"https://api.github.com/repos/Phrogz/ruby-mpd/assignees{/user}","branches_url":"https://api.github.com/repos/Phrogz/ruby-mpd/branches{/branch}","tags_url":"https://api.github.com/repos/Phrogz/ruby-mpd/tags","blobs_url":"https://api.github.com/repos/Phrogz/ruby-mpd/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Phrogz/ruby-mpd/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Phrogz/ruby-mpd/git/refs{/sha}","trees_url":"https://api.github.com/repos/Phrogz/ruby-mpd/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Phrogz/ruby-mpd/statuses/{sha}","languages_url":"https://api.github.com/repos/Phrogz/ruby-mpd/languages","stargazers_url":"https://api.github.com/repos/Phrogz/ruby-mpd/stargazers","contributors_url":"https://api.github.com/repos/Phrogz/ruby-mpd/contributors","subscribers_url":"https://api.github.com/repos/Phrogz/ruby-mpd/subscribers","subscription_url":"https://api.github.com/repos/Phrogz/ruby-mpd/subscription","commits_url":"https://api.github.com/repos/Phrogz/ruby-mpd/commits{/sha}","git_commits_url":"https://api.github.com/repos/Phrogz/ruby-mpd/git/commits{/sha}","comments_url":"https://api.github.com/repos/Phrogz/ruby-mpd/comments{/number}","issue_comment_url":"https://api.github.com/repos/Phrogz/ruby-mpd/issues/comments{/number}","contents_url":"https://api.github.com/repos/Phrogz/ruby-mpd/contents/{+path}","compare_url":"https://api.github.com/repos/Phrogz/ruby-mpd/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Phrogz/ruby-mpd/merges","archive_url":"https://api.github.com/repos/Phrogz/ruby-mpd/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Phrogz/ruby-mpd/downloads","issues_url":"https://api.github.com/repos/Phrogz/ruby-mpd/issues{/number}","pulls_url":"https://api.github.com/repos/Phrogz/ruby-mpd/pulls{/number}","milestones_url":"https://api.github.com/repos/Phrogz/ruby-mpd/milestones{/number}","notifications_url":"https://api.github.com/repos/Phrogz/ruby-mpd/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Phrogz/ruby-mpd/labels{/name}","releases_url":"https://api.github.com/repos/Phrogz/ruby-mpd/releases{/id}","deployments_url":"https://api.github.com/repos/Phrogz/ruby-mpd/deployments","created_at":"2015-11-08T18:46:27Z","updated_at":"2016-01-29T17:04:39Z","pushed_at":"2016-03-08T04:33:26Z","git_url":"git://github.com/Phrogz/ruby-mpd.git","ssh_url":"git@github.com:Phrogz/ruby-mpd.git","clone_url":"https://github.com/Phrogz/ruby-mpd.git","svn_url":"https://github.com/Phrogz/ruby-mpd","homepage":null,"size":527,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"archSeer:master","ref":"master","sha":"12b5e3ab4b62202aed7b7ed50a95cf8442e8f1a0","user":{"login":"archSeer","id":1372918,"avatar_url":"https://avatars.githubusercontent.com/u/1372918?v=3","gravatar_id":"","url":"https://api.github.com/users/archSeer","html_url":"https://github.com/archSeer","followers_url":"https://api.github.com/users/archSeer/followers","following_url":"https://api.github.com/users/archSeer/following{/other_user}","gists_url":"https://api.github.com/users/archSeer/gists{/gist_id}","starred_url":"https://api.github.com/users/archSeer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/archSeer/subscriptions","organizations_url":"https://api.github.com/users/archSeer/orgs","repos_url":"https://api.github.com/users/archSeer/repos","events_url":"https://api.github.com/users/archSeer/events{/privacy}","received_events_url":"https://api.github.com/users/archSeer/received_events","type":"User","site_admin":false},"repo":{"id":7654785,"name":"ruby-mpd","full_name":"archSeer/ruby-mpd","owner":{"login":"archSeer","id":1372918,"avatar_url":"https://avatars.githubusercontent.com/u/1372918?v=3","gravatar_id":"","url":"https://api.github.com/users/archSeer","html_url":"https://github.com/archSeer","followers_url":"https://api.github.com/users/archSeer/followers","following_url":"https://api.github.com/users/archSeer/following{/other_user}","gists_url":"https://api.github.com/users/archSeer/gists{/gist_id}","starred_url":"https://api.github.com/users/archSeer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/archSeer/subscriptions","organizations_url":"https://api.github.com/users/archSeer/orgs","repos_url":"https://api.github.com/users/archSeer/repos","events_url":"https://api.github.com/users/archSeer/events{/privacy}","received_events_url":"https://api.github.com/users/archSeer/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/archSeer/ruby-mpd","description":"ruby-mpd is a powerful object-oriented Music Player Daemon library, forked from librmpd.","fork":false,"url":"https://api.github.com/repos/archSeer/ruby-mpd","forks_url":"https://api.github.com/repos/archSeer/ruby-mpd/forks","keys_url":"https://api.github.com/repos/archSeer/ruby-mpd/keys{/key_id}","collaborators_url":"https://api.github.com/repos/archSeer/ruby-mpd/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/archSeer/ruby-mpd/teams","hooks_url":"https://api.github.com/repos/archSeer/ruby-mpd/hooks","issue_events_url":"https://api.github.com/repos/archSeer/ruby-mpd/issues/events{/number}","events_url":"https://api.github.com/repos/archSeer/ruby-mpd/events","assignees_url":"https://api.github.com/repos/archSeer/ruby-mpd/assignees{/user}","branches_url":"https://api.github.com/repos/archSeer/ruby-mpd/branches{/branch}","tags_url":"https://api.github.com/repos/archSeer/ruby-mpd/tags","blobs_url":"https://api.github.com/repos/archSeer/ruby-mpd/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/archSeer/ruby-mpd/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/archSeer/ruby-mpd/git/refs{/sha}","trees_url":"https://api.github.com/repos/archSeer/ruby-mpd/git/trees{/sha}","statuses_url":"https://api.github.com/repos/archSeer/ruby-mpd/statuses/{sha}","languages_url":"https://api.github.com/repos/archSeer/ruby-mpd/languages","stargazers_url":"https://api.github.com/repos/archSeer/ruby-mpd/stargazers","contributors_url":"https://api.github.com/repos/archSeer/ruby-mpd/contributors","subscribers_url":"https://api.github.com/repos/archSeer/ruby-mpd/subscribers","subscription_url":"https://api.github.com/repos/archSeer/ruby-mpd/subscription","commits_url":"https://api.github.com/repos/archSeer/ruby-mpd/commits{/sha}","git_commits_url":"https://api.github.com/repos/archSeer/ruby-mpd/git/commits{/sha}","comments_url":"https://api.github.com/repos/archSeer/ruby-mpd/comments{/number}","issue_comment_url":"https://api.github.com/repos/archSeer/ruby-mpd/issues/comments{/number}","contents_url":"https://api.github.com/repos/archSeer/ruby-mpd/contents/{+path}","compare_url":"https://api.github.com/repos/archSeer/ruby-mpd/compare/{base}...{head}","merges_url":"https://api.github.com/repos/archSeer/ruby-mpd/merges","archive_url":"https://api.github.com/repos/archSeer/ruby-mpd/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/archSeer/ruby-mpd/downloads","issues_url":"https://api.github.com/repos/archSeer/ruby-mpd/issues{/number}","pulls_url":"https://api.github.com/repos/archSeer/ruby-mpd/pulls{/number}","milestones_url":"https://api.github.com/repos/archSeer/ruby-mpd/milestones{/number}","notifications_url":"https://api.github.com/repos/archSeer/ruby-mpd/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/archSeer/ruby-mpd/labels{/name}","releases_url":"https://api.github.com/repos/archSeer/ruby-mpd/releases{/id}","deployments_url":"https://api.github.com/repos/archSeer/ruby-mpd/deployments","created_at":"2013-01-16T21:58:00Z","updated_at":"2016-03-09T15:06:14Z","pushed_at":"2016-03-26T11:56:33Z","git_url":"git://github.com/archSeer/ruby-mpd.git","ssh_url":"git@github.com:archSeer/ruby-mpd.git","clone_url":"https://github.com/archSeer/ruby-mpd.git","svn_url":"https://github.com/archSeer/ruby-mpd","homepage":null,"size":533,"stargazers_count":74,"watchers_count":74,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":31,"mirror_url":null,"open_issues_count":18,"forks":31,"open_issues":18,"watchers":74,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/archSeer/ruby-mpd/pulls/76"},"html":{"href":"https://github.com/archSeer/ruby-mpd/pull/76"},"issue":{"href":"https://api.github.com/repos/archSeer/ruby-mpd/issues/76"},"comments":{"href":"https://api.github.com/repos/archSeer/ruby-mpd/issues/76/comments"},"review_comments":{"href":"https://api.github.com/repos/archSeer/ruby-mpd/pulls/76/comments"},"review_comment":{"href":"https://api.github.com/repos/archSeer/ruby-mpd/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/archSeer/ruby-mpd/pulls/76/commits"},"statuses":{"href":"https://api.github.com/repos/archSeer/ruby-mpd/statuses/9d8c1878dddee51ed202e0586a5323921411e2e8"}}}}