From 4e256eed982a077e62dbea17b9de265e53a010e4 Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Thu, 15 Apr 2021 15:40:20 -0400 Subject: [PATCH] Add `type` and predicate methods to `Photo` and `Video`. Since collections endpoints can return both, it will be helpful to have a way to distinguish. --- lib/pexels/photo.rb | 12 ++++++++++++ lib/pexels/video.rb | 12 ++++++++++++ test/photo_test.rb | 4 ++++ test/video_test.rb | 4 ++++ 4 files changed, 32 insertions(+) diff --git a/lib/pexels/photo.rb b/lib/pexels/photo.rb index a999e00..fd58c0f 100644 --- a/lib/pexels/photo.rb +++ b/lib/pexels/photo.rb @@ -22,4 +22,16 @@ def initialize(attrs) rescue KeyError => exception raise Pexels::MalformedAPIResponseError.new(exception) end + + def type + 'Photo' + end + + def photo? + true + end + + def video? + false + end end diff --git a/lib/pexels/video.rb b/lib/pexels/video.rb index 1e32003..b413042 100644 --- a/lib/pexels/video.rb +++ b/lib/pexels/video.rb @@ -31,4 +31,16 @@ def initialize(attrs) rescue KeyError => exception raise Pexels::MalformedAPIResponseError.new(exception) end + + def type + 'Video' + end + + def photo? + false + end + + def video? + true + end end diff --git a/test/photo_test.rb b/test/photo_test.rb index c1caf48..4b63404 100644 --- a/test/photo_test.rb +++ b/test/photo_test.rb @@ -60,6 +60,10 @@ def test_get_photo assert_equal photo.user.url, @photo.user.url assert_equal photo.user.id, @photo.user.id assert_equal photo.src, @photo.src + + assert photo.photo? + assert_equal photo.type, 'Photo' + refute photo.video? end def test_invalid_get_photo diff --git a/test/video_test.rb b/test/video_test.rb index 1d0d7db..2630737 100644 --- a/test/video_test.rb +++ b/test/video_test.rb @@ -57,6 +57,10 @@ def test_get_video assert_equal video.height, @video.height assert_equal video.url, @video.url + assert video.video? + assert_equal video.type, 'Video' + refute video.photo? + assert video.user.is_a?(Pexels::User) assert_equal video.user.name, @video.user.name assert_equal video.user.url, @video.user.url