Permalink
Browse files

Extract external service dependencies into method calls, get first re…

…al test
  • Loading branch information...
edgenard committed May 13, 2018
1 parent 5a1e537 commit c2fe51c5dffa0efa5d2cb84a66ec555473146692
@@ -2,7 +2,25 @@ class VideoService
def video_list
@video_list = get_current_video_list
ids = @video_list.map{|v| v['youtubeID']}
# NOTE: External Service
response = get_youtube_stats_on_videos(ids)
ids.each do |id|
video = @video_list.find{|v| id == v['youtubeID']}
youtube_record = response['items'].find{|v| id == v['id']}
video['views'] = youtube_record['statistics']['viewCount'].to_i
days_available = Date.today - Date.parse(youtube_record['snippet']['publishedAt'])
video['monthlyViews'] = video['views'] * 365.0 / days_available / 12
end
return JSON.dump(@video_list)
end

private

def get_current_video_list

This comment has been minimized.

@medwards1771

medwards1771 May 15, 2018

Collaborator

I see you're adding a new method here, #get_current_video_list -- why doesn't this method get its own test?

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

def get_youtube_stats_on_videos(youtube_ids)
client = GoogleAuthorizer.new(
token_key: 'api-youtube',
application_name: 'Gateway Youtube Example',
@@ -13,25 +31,11 @@ def video_list
request = {
api_method: youtube.videos.list,# NOTE: External Service
parameters: {
id: ids.join(","),
id: youtube_ids.join(","),
part: 'snippet, contentDetails, statistics',
}
}
# NOTE: External Service
response = JSON.parse(client.execute!(request).body)
ids.each do |id|
video = @video_list.find{|v| id == v['youtubeID']}
youtube_record = response['items'].find{|v| id == v['id']}
video['views'] = youtube_record['statistics']['viewCount'].to_i
days_available = Date.today - Date.parse(youtube_record['snippet']['publishedAt'])
video['monthlyViews'] = video['views'] * 365.0 / days_available / 12
end
return JSON.dump(@video_list)
end

private
def get_current_video_list
video_list_json = File.read('videos.json')
JSON.parse(video_list_json)
end
end
@@ -1,15 +1,31 @@
require 'minitest/autorun'
require './youtube_video_list'
require 'json'
require 'date'

class VideoServiceTest < MiniTest::Test

def test_video_list_returns_video_list
video_service = VideoService.new
video_array = [{'youtubeId': 'blahblahblah', 'views': 3, 'monthlyViews': 1}]
video_array = [{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}]
youtube_response = {'items' =>
[
{
'id' =>'blahblahblah',
'statistics' => {'viewCount' => '3'},
'snippet' => {'publishedAt' => (Date.today - 30).to_s }
}
]
}
video_json = JSON.generate(video_array)
video_service.stub(:get_current_video_list, video_array) do
assert_equal(video_service.video_list, video_json)
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 c2fe51c

Please sign in to comment.