@@ -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
3741end
0 commit comments