From 5f48058d5f3c90c50ea42bd1094594e6c840df42 Mon Sep 17 00:00:00 2001 From: Richard Lee <14349+dlackty@users.noreply.github.com> Date: Mon, 6 Dec 2021 18:18:52 +0800 Subject: [PATCH] Add region_restricted? for video --- CHANGELOG.md | 4 ++++ lib/yt/models/content_detail.rb | 1 + lib/yt/models/video.rb | 5 +++++ spec/models/video_spec.rb | 12 ++++++++++++ 4 files changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58e555d7..051bcdc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ For more information about changelogs, check [Keep a Changelog](http://keepachangelog.com) and [Vandamme](http://tech-angels.github.io/vandamme). +## Unreleased + +* [FEATURE] New Video#region_restricted? method + ## 0.33.4 - 2021-01-15 * [REMOVAL] remove retry for quota errors diff --git a/lib/yt/models/content_detail.rb b/lib/yt/models/content_detail.rb index 0777dc53..e4f735cf 100644 --- a/lib/yt/models/content_detail.rb +++ b/lib/yt/models/content_detail.rb @@ -24,6 +24,7 @@ def initialize(options = {}) has_attribute :licensed_content has_attribute :content_rating, default: {} has_attribute :item_count + has_attribute :region_restriction def youtube_rating content_rating['ytRating'] diff --git a/lib/yt/models/video.rb b/lib/yt/models/video.rb index 7226f360..ecb1acb9 100644 --- a/lib/yt/models/video.rb +++ b/lib/yt/models/video.rb @@ -271,6 +271,11 @@ def licensed? content_detail.licensed_content || false end + # @return [Boolean] whether the video is only viewable in limited countries. + def region_restricted? + content_detail.region_restriction + end + # @return [Boolean] whether the video is identified by YouTube as # age-restricted content. def age_restricted? diff --git a/spec/models/video_spec.rb b/spec/models/video_spec.rb index 537940c5..19da03c1 100644 --- a/spec/models/video_spec.rb +++ b/spec/models/video_spec.rb @@ -472,6 +472,18 @@ end end + describe '#region_restricted?' do + context 'given a video with region restricted content' do + let(:attrs) { {content_details: {"regionRestriction"=>{"allowed"=>["TW"]}}} } + it { expect(video).to be_region_restricted } + end + + context 'given a video without region restricted content' do + let(:attrs) { {content_details: {}} } + it { expect(video).not_to be_region_restricted } + end + end + describe '#age_restricted?' do context 'given a video with age restricted content' do let(:attrs) { {content_details: {"contentRating"=>{"ytRating"=>"ytAgeRestricted"}}} }