Skip to content

Commit

Permalink
Merge pull request #3 from pexels/refactor-collections
Browse files Browse the repository at this point in the history
Refactor multi-record responses
  • Loading branch information
dvandersluis authored Apr 9, 2021
2 parents 26c1346 + 905f79b commit 8ee1400
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 35 deletions.
4 changes: 3 additions & 1 deletion lib/pexels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ def local_headers
require_relative 'pexels/video/file'
require_relative 'pexels/video/picture'
require_relative 'pexels/user'
require_relative 'pexels/response'
require_relative 'pexels/paginated_response'
require_relative 'pexels/photo_set'
require_relative 'pexels/video_set'
4 changes: 4 additions & 0 deletions lib/pexels/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def videos
@videos ||= Pexels::Client::Videos.new(self)
end

def collections
@collections ||= Pexels::Client::Collections.new(self)
end

def request(path, method: 'GET', params: {})
url = File.join(Pexels.api_base_url, path)
headers = {
Expand Down
4 changes: 2 additions & 2 deletions lib/pexels/client/photos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def search(query, per_page: 15, page: 1, locale: 'en-US')
}
)

Pexels::Response.new(response)
Pexels::PhotoSet.new(response)
end

def curated(per_page: 15, page: 1)
Expand All @@ -33,6 +33,6 @@ def curated(per_page: 15, page: 1)
}
)

Pexels::Response.new(response)
Pexels::PhotoSet.new(response)
end
end
4 changes: 2 additions & 2 deletions lib/pexels/client/videos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def search(query, per_page: 15, page: 1)
}
)

Pexels::Response.new(response)
Pexels::VideoSet.new(response)
end

def popular(per_page: 15, page: 1)
Expand All @@ -32,6 +32,6 @@ def popular(per_page: 15, page: 1)
}
)

Pexels::Response.new(response)
Pexels::VideoSet.new(response)
end
end
28 changes: 28 additions & 0 deletions lib/pexels/paginated_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Pexels
class PaginatedResponse
include Enumerable

attr_reader :total_results,
:page,
:per_page,
:next_page,
:data

private :data

def initialize(attrs)
@total_results = attrs.fetch('total_results', nil)
@page = attrs.fetch('page')
@per_page = attrs.fetch('per_page')
@next_page = attrs.fetch('next_page', nil)
end

def each(&block)
if block_given?
data.each(&block)
else
to_enum(:each)
end
end
end
end
14 changes: 14 additions & 0 deletions lib/pexels/photo_set.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Pexels
class PhotoSet < PaginatedResponse
alias_method :photos, :data
public :photos

def initialize(attrs)
super
@data = attrs.fetch('photos', []).map { |attrs| Pexels::Photo.new(attrs) }

rescue KeyError => exception
raise Pexels::MalformedAPIResponseError.new(exception)
end
end
end
20 changes: 0 additions & 20 deletions lib/pexels/response.rb

This file was deleted.

14 changes: 14 additions & 0 deletions lib/pexels/video_set.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Pexels
class VideoSet < PaginatedResponse
alias_method :videos, :data
public :videos

def initialize(attrs)
super
@data = attrs.fetch('videos', []).map { |attrs| Pexels::Video.new(attrs) }

rescue KeyError => exception
raise Pexels::MalformedAPIResponseError.new(exception)
end
end
end
10 changes: 5 additions & 5 deletions test/photo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ class TestPhoto < Minitest::Test

def setup
@client = Pexels::Client.new(ENV.fetch('PEXELS_API_KEY'))
@photo = @client.photos.search('test', per_page: 1).photos.first
@photo = @client.photos.search('test', per_page: 1).first
end

def test_successful_searches
search_result = @client.photos.search('test')

assert search_result.is_a? Pexels::Response
assert search_result.is_a? Pexels::PhotoSet
assert search_result.next_page.is_a? String
assert search_result.total_results.is_a? Integer
assert_equal search_result.per_page, 15
assert_equal search_result.page, 1

assert search_result.photos.is_a? Array
assert search_result.photos.any?
assert search_result.photos.first.is_a? Pexels::Photo
assert search_result.first.is_a? Pexels::Photo

search_result_with_params = @client.photos.search('test', per_page: 1, page: 2)
assert_equal search_result_with_params.per_page, 1
Expand All @@ -30,14 +30,14 @@ def test_successful_searches
def test_curated_photos
search_result = @client.photos.curated

assert search_result.is_a? Pexels::Response
assert search_result.is_a? Pexels::PhotoSet
assert search_result.next_page.is_a? String
assert_equal search_result.per_page, 15
assert_equal search_result.page, 1

assert search_result.photos.is_a? Array
assert search_result.photos.any?
assert search_result.photos.first.is_a? Pexels::Photo
assert search_result.first.is_a? Pexels::Photo

search_result_with_params = @client.photos.curated(per_page: 1, page: 2)
assert_equal search_result_with_params.per_page, 1
Expand Down
10 changes: 5 additions & 5 deletions test/video_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ class TestVideo < Minitest::Test

def setup
@client = Pexels::Client.new(ENV.fetch('PEXELS_API_KEY'))
@video = @client.videos.search('test', per_page: 1).videos.first
@video = @client.videos.search('test', per_page: 1).first
end

def test_successful_searches
search_result = @client.videos.search('test')

assert search_result.is_a? Pexels::Response
assert search_result.is_a? Pexels::VideoSet
assert search_result.total_results.is_a? Integer
assert_equal search_result.per_page, 15
assert_equal search_result.page, 1

assert search_result.videos.is_a? Array
assert search_result.videos.any?
assert search_result.videos.first.is_a? Pexels::Video
assert search_result.first.is_a? Pexels::Video

search_result_with_params = @client.videos.search('test', per_page: 1, page: 2)
assert_equal search_result_with_params.per_page, 1
Expand All @@ -29,13 +29,13 @@ def test_successful_searches
def test_popular_videos
search_result = @client.videos.popular

assert search_result.is_a? Pexels::Response
assert search_result.is_a? Pexels::VideoSet
assert_equal search_result.per_page, 15
assert_equal search_result.page, 1

assert search_result.videos.is_a? Array
assert search_result.videos.any?
assert search_result.videos.first.is_a? Pexels::Video
assert search_result.first.is_a? Pexels::Video

search_result_with_params = @client.videos.popular(per_page: 1, page: 2)
assert_equal search_result_with_params.per_page, 1
Expand Down

0 comments on commit 8ee1400

Please sign in to comment.