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

Commit c2fe51c

Browse files
committed
Extract external service dependencies into method calls, get first real test
1 parent 5a1e537 commit c2fe51c

2 files changed

Lines changed: 38 additions & 18 deletions

File tree

refactoring_external_service/youtube_video_list.rb

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,25 @@ class VideoService
22
def video_list
33
@video_list = get_current_video_list
44
ids = @video_list.map{|v| v['youtubeID']}
5-
# NOTE: External Service
5+
response = get_youtube_stats_on_videos(ids)
6+
ids.each do |id|
7+
video = @video_list.find{|v| id == v['youtubeID']}
8+
youtube_record = response['items'].find{|v| id == v['id']}
9+
video['views'] = youtube_record['statistics']['viewCount'].to_i
10+
days_available = Date.today - Date.parse(youtube_record['snippet']['publishedAt'])
11+
video['monthlyViews'] = video['views'] * 365.0 / days_available / 12
12+
end
13+
return JSON.dump(@video_list)
14+
end
15+
16+
private
17+
18+
def get_current_video_list
19+
video_list_json = File.read('videos.json')
20+
JSON.parse(video_list_json)
21+
end
22+
23+
def get_youtube_stats_on_videos(youtube_ids)
624
client = GoogleAuthorizer.new(
725
token_key: 'api-youtube',
826
application_name: 'Gateway Youtube Example',
@@ -13,25 +31,11 @@ def video_list
1331
request = {
1432
api_method: youtube.videos.list,# NOTE: External Service
1533
parameters: {
16-
id: ids.join(","),
34+
id: youtube_ids.join(","),
1735
part: 'snippet, contentDetails, statistics',
1836
}
1937
}
2038
# NOTE: External Service
2139
response = JSON.parse(client.execute!(request).body)
22-
ids.each do |id|
23-
video = @video_list.find{|v| id == v['youtubeID']}
24-
youtube_record = response['items'].find{|v| id == v['id']}
25-
video['views'] = youtube_record['statistics']['viewCount'].to_i
26-
days_available = Date.today - Date.parse(youtube_record['snippet']['publishedAt'])
27-
video['monthlyViews'] = video['views'] * 365.0 / days_available / 12
28-
end
29-
return JSON.dump(@video_list)
30-
end
31-
32-
private
33-
def get_current_video_list
34-
video_list_json = File.read('videos.json')
35-
JSON.parse(video_list_json)
3640
end
3741
end

refactoring_external_service/youtube_video_list_spec.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
require 'minitest/autorun'
22
require './youtube_video_list'
33
require 'json'
4+
require 'date'
45

56
class VideoServiceTest < MiniTest::Test
67

78
def test_video_list_returns_video_list
89
video_service = VideoService.new
9-
video_array = [{'youtubeId': 'blahblahblah', 'views': 3, 'monthlyViews': 1}]
10+
video_array = [{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}]
11+
youtube_response = {'items' =>
12+
[
13+
{
14+
'id' =>'blahblahblah',
15+
'statistics' => {'viewCount' => '3'},
16+
'snippet' => {'publishedAt' => (Date.today - 30).to_s }
17+
}
18+
]
19+
}
1020
video_json = JSON.generate(video_array)
1121
video_service.stub(:get_current_video_list, video_array) do
12-
assert_equal(video_service.video_list, video_json)
22+
video_service.stub(:get_youtube_stats_on_videos, youtube_response) do
23+
result = JSON.parse(video_service.video_list)
24+
actual = JSON.parse(video_json)
25+
assert_equal(result[0]['youtubeID'], result[0]['youtubeID'])
26+
assert_equal(result[0]['views'], result[0]['views'])
27+
assert_in_delta(result[0]['monthlyViews'], result[0]['monthlyViews'], 0.1)
28+
end
1329
end
1430
end
1531

0 commit comments

Comments
 (0)