diff --git a/refactoring_external_service/youtube_video_list.rb b/refactoring_external_service/youtube_video_list.rb index f56ee58..0c091c3 100644 --- a/refactoring_external_service/youtube_video_list.rb +++ b/refactoring_external_service/youtube_video_list.rb @@ -21,21 +21,22 @@ def video_stats(youtube_ids) part: 'snippet, contentDetails, statistics', } } - + response = JSON.parse(client.execute!(request).body) end end class VideoService - attr_reader :video_repo - def initialize(video_repo:) + attr_reader :video_repo, :video_client + def initialize(video_repo:, video_client:) @video_repo = video_repo + @video_client = video_client end def video_list @video_list = video_repo.videos ids = @video_list.map{|v| v['youtubeID']} - response = get_youtube_stats_on_videos(ids) + response = video_client.video_stats(ids) ids.each do |id| video = @video_list.find{|v| id == v['youtubeID']} youtube_record = response['items'].find{|v| id == v['id']} @@ -45,10 +46,4 @@ def video_list end return JSON.dump(@video_list) end - - private - - def get_youtube_stats_on_videos(youtube_ids) - YoutubeVideoClient.new.video_stats(youtube_ids) - end end diff --git a/refactoring_external_service/youtube_video_list_spec.rb b/refactoring_external_service/youtube_video_list_spec.rb index 973b101..350084d 100644 --- a/refactoring_external_service/youtube_video_list_spec.rb +++ b/refactoring_external_service/youtube_video_list_spec.rb @@ -5,14 +5,14 @@ class VideoServiceTest < MiniTest::Test - class VideoRepoMock + class VideoRepoStub def videos [{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}] end end - class YoutubeVideoClientMock - def get_video_stats(ids=:NotGiven) + class YoutubeVideoClientStub + def video_stats(ids=:NotGiven) {'items' => [ { @@ -26,16 +26,15 @@ def get_video_stats(ids=:NotGiven) end def test_video_list_returns_video_list - video_service = VideoService.new(video_repo: VideoRepoMock.new) + video_service = VideoService.new(video_repo: VideoRepoStub.new, video_client: YoutubeVideoClientStub.new) video_array = [{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}] - youtube_response = YoutubeVideoClientMock.new.get_video_stats() video_json = JSON.generate(video_array) - video_service.stub(:get_youtube_stats_on_videos, youtube_response) do - result = JSON.parse(video_service.video_list) - actual = JSON.parse(video_json) - assert_equal(result[0]['youtubeID'], result[0]['youtubeID']) - assert_equal(result[0]['views'], result[0]['views']) - assert_in_delta(result[0]['monthlyViews'], result[0]['monthlyViews'], 0.1) - end + + result = JSON.parse(video_service.video_list) + actual = JSON.parse(video_json) + assert_equal(result[0]['youtubeID'], result[0]['youtubeID']) + assert_equal(result[0]['views'], result[0]['views']) + assert_in_delta(result[0]['monthlyViews'], result[0]['monthlyViews'], 0.1) + end end \ No newline at end of file