Skip to content
This repository has been archived by the owner on Apr 24, 2019. It is now read-only.

Commit

Permalink
Extract getting the current list into its own object that is injected
Browse files Browse the repository at this point in the history
  • Loading branch information
edgenard committed May 13, 2018
1 parent c2fe51c commit 625667c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
15 changes: 13 additions & 2 deletions refactoring_external_service/youtube_video_list.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
class VideoRepo

This comment has been minimized.

Copy link
@medwards1771

medwards1771 May 15, 2018

Contributor

Nice introduction of new objects! How did you decide to name this VideoRepo and make it responsible for parsing the json?

def videos
video_list_json = File.read('videos.json')
JSON.parse(video_list_json)
end
end

class VideoService
attr_reader :video_repo
def initialize(video_repo:)
@video_repo = video_repo
end

def video_list
@video_list = get_current_video_list
ids = @video_list.map{|v| v['youtubeID']}
Expand All @@ -16,8 +28,7 @@ def video_list
private

def get_current_video_list
video_list_json = File.read('videos.json')
JSON.parse(video_list_json)
video_repo.videos
end

def get_youtube_stats_on_videos(youtube_ids)
Expand Down
23 changes: 13 additions & 10 deletions refactoring_external_service/youtube_video_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
require 'date'

class VideoServiceTest < MiniTest::Test

class VideoRepoMock
def videos
[{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}]
end
end

def test_video_list_returns_video_list
video_service = VideoService.new
video_service = VideoService.new(video_repo: VideoRepoMock.new)
video_array = [{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}]
youtube_response = {'items' =>
[
Expand All @@ -18,15 +24,12 @@ def test_video_list_returns_video_list
]
}
video_json = JSON.generate(video_array)
video_service.stub(:get_current_video_list, video_array) do
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
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
end

end

0 comments on commit 625667c

Please sign in to comment.