Permalink
Browse files

Extract getting the current list into its own object that is injected

  • Loading branch information...
edgenard committed May 13, 2018
1 parent c2fe51c commit 625667c365b050293fbdf8b99154f2617bbf1f45
@@ -1,4 +1,16 @@
class VideoRepo

This comment has been minimized.

@medwards1771

medwards1771 May 15, 2018

Collaborator

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']}
@@ -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)
@@ -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' =>
[
@@ -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.