From 83dbbc35a8d8bfa1778eeabb4e7e4932f7632f3a Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Fri, 7 Oct 2016 15:52:06 +0530 Subject: [PATCH] small fix for something --- app/controllers/videos_controller.rb | 41 ++++++++++++++-------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index f211484..d7a2139 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -32,37 +32,38 @@ class VideosController < ApplicationController uri = URI('https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=' + channel_id + '&key=AIzaSyCTFdyLgb7GJfrr1JOI3gvVslZFw7td2zQ') result = Net::HTTP.get(uri) # => String result = JSON.parse(result) - vc = [] nextPageToken = result['nextPageToken'] rescue "" while !nextPageToken.nil? - url = result['items'].first["id"]["videoId"].present? ? "https://www.youtube.com/embed/#{result['items'].first["id"]["videoId"]}" : "#" - vc << { - "title" => result['items'].first['snippet']['title'], - "description" => result['items'].first['snippet']['description'] , - "publish_date" => result['items'].first['snippet']['publishedAt'], - "img1_src" => result['items'].first['snippet']['thumbnails']['default']['url'], - "img2_src" => result['items'].first['snippet']['thumbnails']['medium']['url'], - "img3_src" => result['items'].first['snippet']['thumbnails']['high']['url'] , - "video_url" => url, - "channel_title" => result['items'].first['snippet']['channelTitle'] - } + if !result.nil? && !result['items'].nil? + url = (result['items'].first["id"]["videoId"].present? ? "https://www.youtube.com/embed/#{result['items'].first["id"]["videoId"]}" : "#") rescue "#" + vc << { + "title" => (result['items'].first['snippet']['title'] rescue ""), + "description" => (result['items'].first['snippet']['description'] rescue ""), + "publish_date" => (result['items'].first['snippet']['publishedAt'] rescue ""), + "img1_src" => (result['items'].first['snippet']['thumbnails']['default']['url'] rescue ""), + "img2_src" => (result['items'].first['snippet']['thumbnails']['medium']['url'] rescue ""), + "img3_src" => (result['items'].first['snippet']['thumbnails']['high']['url'] rescue ""), + "video_url" => url, + "channel_title" => (result['items'].first['snippet']['channelTitle'] rescue "") + } + end uri = URI('https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=' + channel_id + '&key=AIzaSyCTFdyLgb7GJfrr1JOI3gvVslZFw7td2zQ&pageToken=' + nextPageToken) result = Net::HTTP.get(uri) # => String result = JSON.parse(result) nextPageToken = result['nextPageToken'] rescue "" end - url = result['items'].first["id"]["videoId"].present? ? "https://www.youtube.com/embed/#{result['items'].first["id"]["videoId"]}" : "#" + url = (result['items'].first["id"]["videoId"].present? ? "https://www.youtube.com/embed/#{result['items'].first["id"]["videoId"]}" : "#") rescue "#" vc << { - "title" => result['items'].first['snippet']['title'], - "description" => result['items'].first['snippet']['description'] , - "publish_date" => result['items'].first['snippet']['publishedAt'], - "img1_src" => result['items'].first['snippet']['thumbnails']['default']['url'], - "img2_src" => result['items'].first['snippet']['thumbnails']['medium']['url'], - "img3_src" => result['items'].first['snippet']['thumbnails']['high']['url'] , + "title" => (result['items'].first['snippet']['title'] rescue ""), + "description" => (result['items'].first['snippet']['description'] rescue ""), + "publish_date" => (result['items'].first['snippet']['publishedAt'] rescue ""), + "img1_src" => (result['items'].first['snippet']['thumbnails']['default']['url'] rescue ""), + "img2_src" => (result['items'].first['snippet']['thumbnails']['medium']['url'] rescue ""), + "img3_src" => (result['items'].first['snippet']['thumbnails']['high']['url'] rescue ""), "video_url" => url, - "channel_title" => result['items'].first['snippet']['channelTitle'] + "channel_title" => (result['items'].first['snippet']['channelTitle'] rescue "") }