-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from pexels/add-collections-endpoints
Add support for new Collections endpoints
- Loading branch information
Showing
8 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Change log | ||
|
||
## 0.1.0 | ||
* Add support for returning collections belonging to the API user. | ||
* Add support for returning media from a collection. | ||
|
||
## 0.0.4 | ||
* Add `find` as an alias for `photos[]` and `videos[]`. | ||
|
||
## 0.0.3 | ||
* Initial release. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Pexels::Client::Collections | ||
def initialize(client) | ||
@client = client | ||
end | ||
|
||
def all(per_page: 15, page: 1) | ||
response = @client.request( | ||
'/collections', | ||
params: { | ||
per_page: per_page, | ||
page: page | ||
}) | ||
|
||
Pexels::CollectionSet.new(response) | ||
end | ||
|
||
def [](id, type: nil, per_page: 15, page: 1) | ||
response = @client.request( | ||
"/collections/#{id}", | ||
params: { | ||
per_page: per_page, | ||
page: page, | ||
type: type | ||
}) | ||
|
||
Pexels::CollectionMediaSet.new(response) | ||
end | ||
alias_method :find, :[] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Pexels | ||
class Collection | ||
attr_reader :id, | ||
:title, | ||
:description, | ||
:private, | ||
:media_count, | ||
:photos_count, | ||
:videos_count | ||
|
||
|
||
def initialize(attrs) | ||
@id = attrs.fetch('id') | ||
@title = attrs.fetch('title') | ||
@description = attrs.fetch('description') | ||
@private = attrs.fetch('private') | ||
@media_count = attrs.fetch('media_count') | ||
@photos_count = attrs.fetch('photos_count') | ||
@videos_count = attrs.fetch('videos_count') | ||
|
||
rescue KeyError => exception | ||
raise Pexels::MalformedAPIResponseError.new(exception) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Pexels | ||
class CollectionMediaSet < PaginatedResponse | ||
alias_method :media, :data | ||
public :media | ||
|
||
attr_reader :id | ||
|
||
def initialize(attrs) | ||
super | ||
@id = attrs.fetch('id') | ||
@data = attrs.fetch('media', []).map do |attrs| | ||
if attrs['type'] == 'Photo' | ||
Pexels::Photo.new(attrs) | ||
elsif attrs['type'] == 'Video' | ||
Pexels::Video.new(attrs) | ||
end | ||
end | ||
|
||
rescue KeyError => exception | ||
raise Pexels::MalformedAPIResponseError.new(exception) | ||
end | ||
|
||
def photos | ||
@photos ||= media.select { |m| m.is_a?(Pexels::Photo) } | ||
end | ||
|
||
def videos | ||
@videos ||= media.select { |m| m.is_a?(Pexels::Video) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Pexels | ||
class CollectionSet < PaginatedResponse | ||
alias_method :collections, :data | ||
public :collections | ||
|
||
def initialize(attrs) | ||
super | ||
@data = attrs.fetch('collections', []).map { |attrs| Pexels::Collection.new(attrs) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Pexels | ||
VERSION = '0.0.4' | ||
VERSION = '0.1.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
require 'minitest/autorun' | ||
require 'pexels' | ||
|
||
class TestCollections < Minitest::Test | ||
|
||
def setup | ||
@client = Pexels::Client.new(ENV.fetch('PEXELS_API_KEY')) | ||
@collection = @client.collections.all(per_page: 1).first | ||
end | ||
|
||
def test_all | ||
search_result = @client.collections.all | ||
|
||
assert search_result.is_a? Pexels::CollectionSet | ||
assert_equal search_result.per_page, 15 | ||
assert_equal search_result.page, 1 | ||
|
||
assert search_result.collections.is_a? Array | ||
assert search_result.collections.any? | ||
assert search_result.first.is_a? Pexels::Collection | ||
|
||
search_result_with_params = @client.collections.all(per_page: 1, page: 2) | ||
assert_equal search_result_with_params.per_page, 1 | ||
assert_equal search_result_with_params.page, 2 | ||
assert_equal search_result_with_params.collections.length, 1 | ||
end | ||
|
||
def test_get_collection_media | ||
collection = @client.collections[@collection.id] | ||
assert collection.is_a? Pexels::CollectionMediaSet | ||
assert_equal collection.id, @collection.id | ||
|
||
assert collection.media.is_a? Array | ||
assert collection.media.any? | ||
|
||
assert_includes([Pexels::Photo, Pexels::Video], collection.media.first.class) | ||
|
||
refute_includes([Pexels::Video], collection.photos.map(&:class)) | ||
refute_includes([Pexels::Photo], collection.videos.map(&:class)) | ||
end | ||
|
||
def test_get_collection_photos | ||
collection = @client.collections[@collection.id, type: 'photos'] | ||
assert collection.is_a? Pexels::CollectionMediaSet | ||
assert collection.media.is_a? Array | ||
assert collection.media.all? { |m| m.is_a?(Pexels::Photo) } | ||
end | ||
|
||
def test_get_collection_videos | ||
collection = @client.collections[@collection.id, type: 'videos'] | ||
assert collection.is_a? Pexels::CollectionMediaSet | ||
assert collection.media.is_a? Array | ||
assert collection.media.all? { |m| m.is_a?(Pexels::Video) } | ||
end | ||
|
||
def test_get_collection_invalid_type | ||
collection = @client.collections[@collection.id, type: 'foo'] | ||
assert collection.is_a? Pexels::CollectionMediaSet | ||
assert collection.media.is_a? Array | ||
assert collection.any? | ||
end | ||
|
||
def test_get_collection_pagination | ||
collection = @client.collections[@collection.id, per_page: 1, page: 1] | ||
assert collection.is_a? Pexels::CollectionMediaSet | ||
assert collection.media.is_a? Array | ||
assert collection.media.any? | ||
|
||
assert_equal collection.per_page, 1 | ||
assert_equal collection.page, 1 | ||
assert_equal collection.media.length, 1 | ||
end | ||
|
||
def test_invalid_get_collection | ||
@client.collections['this-is-not-a-valid-id'] | ||
raise 'This should not happen' | ||
rescue StandardError => exception | ||
assert exception.is_a? Pexels::APIError | ||
assert_equal exception.message, 'Not Found' | ||
end | ||
end |