Skip to content

Commit

Permalink
Add type and predicate methods to Photo and Video.
Browse files Browse the repository at this point in the history
Since collections endpoints can return both, it will be helpful to have a way to distinguish.
  • Loading branch information
dvandersluis committed Apr 15, 2021
1 parent 9d255a0 commit 4e256ee
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/pexels/photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions lib/pexels/video.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions test/photo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/video_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4e256ee

Please sign in to comment.