From fedf565389cb9220e1a91308abf874c7180fc525 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Tue, 5 May 2015 02:27:29 +0000 Subject: [PATCH] Accept SHA256 as identifier for API playlists/show --- app/controllers/api/sounds_controller.rb | 5 ++++- spec/integration/api/sounds_spec.rb | 9 ++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/sounds_controller.rb b/app/controllers/api/sounds_controller.rb index 980cf12..6c0913c 100644 --- a/app/controllers/api/sounds_controller.rb +++ b/app/controllers/api/sounds_controller.rb @@ -12,7 +12,10 @@ module API private def set_sound - @sound = Sound.find(params[:id]) + @sound = case params[:id] + when /\A\d+\z/ then Sound.find(params[:id]) + when /\A\h+\z/ then Sound.find_by_sha256!(params[:id]) + end end end end diff --git a/spec/integration/api/sounds_spec.rb b/spec/integration/api/sounds_spec.rb index 2c08acc..ab1d7a3 100644 --- a/spec/integration/api/sounds_spec.rb +++ b/spec/integration/api/sounds_spec.rb @@ -4,7 +4,8 @@ describe 'API sounds' do describe 'sound show' do let(:sound) { create :sound } let(:request_method) { :get } - let(:request_show) { send request_method, api_sound_path(sound) } + let(:request_path) { api_sound_path(sound) } + let(:request_show) { send request_method, request_path } subject { response } before { request_show } @@ -28,5 +29,11 @@ describe 'API sounds' do expect(response.body).to be_empty end end + + context 'when sound is requested by SHA256 digest' do + let(:request_path) { api_sound_path id: sound.sha256 } + + it { is_expected.to have_http_status 200 } + end end end