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

Commit

Permalink
Note where external services are being used to potentially mock out
Browse files Browse the repository at this point in the history
  • Loading branch information
edgenard committed May 12, 2018
1 parent 50936a9 commit 67b2068
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
19 changes: 12 additions & 7 deletions refactoring_external_service/youtube_video_list.rb
@@ -1,45 +1,50 @@
class VideoService class VideoService
def video_list def video_list
# NOTE: External service/Maybe

This comment has been minimized.

Copy link
@medwards1771

medwards1771 May 15, 2018

Contributor

You're marking several lines of code as eternal services here. Why? What makes that this stuff an "external service" ?

video_list_json = File.read('videos.json')
# Read a json file and store the contents in an instance variable # Read a json file and store the contents in an instance variable
@video_list = JSON.parse(File.read('videos.json')) @video_list = JSON.parse(video_list_json)
# Get all the ids of the stored in the contents and JSON file that was just # Get all the ids of the stored in the contents and JSON file that was just
ids = @video_list.map{|v| v['youtubeID']} ids = @video_list.map{|v| v['youtubeID']}
# Get authorized with Google to be allowed to make API calls # Get authorized with Google to be allowed to make API calls
# NOTE: External Service
client = GoogleAuthorizer.new( client = GoogleAuthorizer.new(
token_key: 'api-youtube', token_key: 'api-youtube',
application_name: 'Gateway Youtube Example', application_name: 'Gateway Youtube Example',
application_version: '0.1' application_version: '0.1'
).api_client ).api_client
# Not sure what this does, but I think its a way to tell youtube what I'm asking for # Not sure what this does, but I think its a way to tell youtube what I'm asking for
# NOTE: External Service
youtube = client.discovered_api('youtube', 'v3') youtube = client.discovered_api('youtube', 'v3')
# Send the request to Youtube passing in the ids of the videos from out Json file and telling which # Send the request to Youtube passing in the ids of the videos from out Json file and telling which
# data we want from youtube. We want a video snippet, contentDetails(probably a description) # data we want from youtube. We want a video snippet, contentDetails(probably a description)
# and statistics about the video # and statistics about the video
request = { request = {
api_method: youtube.videos.list, # This looks like there are many api_methods and the one api_method: youtube.videos.list,# NOTE: External Service # This looks like there are many api_methods and the one
# we are asking for is the one that returns a list of videos. # we are asking for is the one that returns a list of videos.
parameters: { parameters: {
id: ids.join(","), id: ids.join(","),
part: 'snippet, contentDetails, statistics', part: 'snippet, contentDetails, statistics',
} }
} }
# Get the body of the response and parse it with JSON.parse to make into a Ruby hash # Get the body of the response and parse it with JSON.parse to make into a Ruby hash
# NOTE: External Service
response = JSON.parse(client.execute!(request).body) response = JSON.parse(client.execute!(request).body)
# Go through the array of ids # Go through the array of ids
ids.each do |id| ids.each do |id|
# Find the video object that matches the this specific id # Find the video object that matches the this specific id
video = @video_list.find{|v| id == v['youtubeID']} video = @video_list.find{|v| id == v['youtubeID']}
# Go through the items from the response and find one that matches the id # Go through the items from the response and find one that matches the id
youtube_record = response['items'].find{|v| id == v['id']} youtube_record = response['items'].find{|v| id == v['id']}
# Update the views key in the video object(the one we got from our JSON file) # Update the views key in the video object(the one we got from our JSON file)
# with the viewCount from the response we got from youtube. # with the viewCount from the response we got from youtube.
video['views'] = youtube_record['statistics']['viewCount'].to_i video['views'] = youtube_record['statistics']['viewCount'].to_i
# Find out how many days the video has been live from the response from youtube # Find out how many days the video has been live from the response from youtube
days_available = Date.today - Date.parse(youtube_record['snippet']['publishedAt']) days_available = Date.today - Date.parse(youtube_record['snippet']['publishedAt'])
# Update the monthly views of the video # Update the monthly views of the video
video['monthlyViews'] = video['views'] * 365.0 / days_available / 12 video['monthlyViews'] = video['views'] * 365.0 / days_available / 12
end end
# return the updated video list in a JSON dump # return the updated video list in a JSON dump
return JSON.dump(@video_list) return JSON.dump(@video_list)
end end
end end
7 changes: 7 additions & 0 deletions refactoring_external_service/youtube_video_list_spec.rb
@@ -0,0 +1,7 @@
require 'test/unit'
require './youtube_video_list'

class VideoServiceTest < Test::Unit::TestCase


end

0 comments on commit 67b2068

Please sign in to comment.