pexels 0.0.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c35b5ede38be050f349b46e1acfa441306af1ccf5acdf89ec9e442d4474a960d
4
- data.tar.gz: ea3bb7174df2b5c135fa272bba90a4a5a42133078d7f52e2944ccf663f800518
3
+ metadata.gz: 4a5483a85edfd47c6ae8585394c2f14e6de8e8eaf8b149e26594536411a821d4
4
+ data.tar.gz: '009e00ae30a63f652473bfecd7bc46c5dbfdb839b8c405d1cb5406b7898ce6ae'
5
5
  SHA512:
6
- metadata.gz: b18c77442467ef348fde20120245d4ba880f8dd4417a2d60aabce80c3b44c876a385c413c925d5348b703984a703c98ac0f5c86e97a6a47ea25fcbedd256c62f
7
- data.tar.gz: f7e2e10664aca352d80b7479309e638d6e447b9dd59347eedcbb38ae79626852e7a7463ba9ebb396911d922c9732b4d233322d871e59bfc85f8dfe00c986b93e
6
+ metadata.gz: e55a644d3a0b7ee092796922eb6007d16667b1a5c1d794c66819e896bcde29df05d3129d5f508e48ffb2a54020f36c0fef21d4eb1f18384be0da741d81800012
7
+ data.tar.gz: e6421cd0f5ac1c92fd57c2af6239a4cb0001db5dcb8fb29349fab688d1508685e9e7ff755d9ffc420bbb365ec3f8fa2ef1fd3409653817aaeafb317833f67040
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  .gems.up.to.date
2
2
  .env
3
3
  *.gem
4
+ .headers
data/CHANGES.md ADDED
@@ -0,0 +1,18 @@
1
+ # Change log
2
+
3
+ ## 0.2.0
4
+ * Fixed incorrect URL for collections endpoints.
5
+ * Added pagination methods `next_page` and `prev_page` to `PaginatedResponse`.
6
+ * Added `total_pages` to `PaginatedResponse`.
7
+ * Extracted `Request` and `Response` objects for reusability.
8
+ * Added `Pexels/Ruby` `User-Agent` header to requests.
9
+
10
+ ## 0.1.0
11
+ * Add support for returning collections belonging to the API user.
12
+ * Add support for returning media from a collection.
13
+
14
+ ## 0.0.4
15
+ * Add `find` as an alias for `photos[]` and `videos[]`.
16
+
17
+ ## 0.0.3
18
+ * Initial release.
data/README.md CHANGED
@@ -12,9 +12,109 @@ Or add it to your `Gemfile` and run `bundle install`.
12
12
 
13
13
  ## Documentation
14
14
 
15
- See the API docs [here](https://www.pexels.com/api/documentation/?language=js)
15
+ See the API docs [here](https://www.pexels.com/api/documentation/?language=rb)
16
16
 
17
17
 
18
+ ## Basic usage
19
+
20
+ ### Create a client
21
+
22
+ ```ruby
23
+ # If you don't specify one, the environment variable PEXELS_API_KEY is used by default
24
+ client = Pexels::Client.new('your-access-key')
25
+ ```
26
+
27
+ ### Search for photos
28
+
29
+ ```ruby
30
+ client.photos.search('Cloud')
31
+ ```
32
+
33
+ ### Find a specific photo
34
+
35
+ ```ruby
36
+ client.photos[2014422]
37
+ # or
38
+ client.photos.find(2014422)
39
+ ```
40
+
41
+ ### Browse curated photos
42
+
43
+ ```ruby
44
+ client.photos.curated
45
+ ```
46
+
47
+ ### Search for videos
48
+
49
+ ```ruby
50
+ client.videos.search('waves')
51
+ ```
52
+
53
+ ### Find a specific photo
54
+
55
+ ```ruby
56
+ client.videos[2014422]
57
+ # or
58
+ client.videos.find(2014422)
59
+ ```
60
+
61
+ ### Browse popular videos
62
+
63
+ ```ruby
64
+ client.videos.popular
65
+ ```
66
+
67
+ ### List all collections
68
+
69
+ Note: this is limited to collections belonging to the API user.
70
+
71
+ ```ruby
72
+ client.collections.all
73
+ ```
74
+
75
+ ### Get all media for a collection
76
+
77
+ ```ruby
78
+ client.collections['collection-id'].media
79
+ # or
80
+ client.collections.find('collection-id').media
81
+ ```
82
+
83
+ You can also filter for only `photos` or `videos`.
84
+
85
+ ```ruby
86
+ client.collections['collection-id', type: 'photos'].media
87
+ client.collections['collection-id', type: 'videos'].media
88
+ ```
89
+
90
+ ## Rate Limiting
91
+
92
+ After performing a request, you can access your remaining rate limit via the client.
93
+
94
+ ```ruby
95
+ client.ratelimit_remaining
96
+ ```
97
+
98
+ ## Pagination
99
+
100
+ Requests that return multiple objects are paginated. You can pass in `page` and `per_page` options to these requests to get a specific page. You can also access the total number of results by accessing `total_results` on the response.
101
+
102
+ Note: The Pexels API returns a maximum of 80 records for one request.
103
+
104
+ ```ruby
105
+ response = client.photos.search('dog', page: 2, per_page: 50)
106
+ response.total_results #=> 1000
107
+ response.total_pages #= 20
108
+ ```
109
+
110
+ If there are further pages, you can also paginate through the API client:
111
+
112
+ ```ruby
113
+ response = client.photos.search('dog', page: 2, per_page: 50)
114
+ response.prev_page # queries page 1
115
+ response.next_page # queries page 3
116
+ ```
117
+
18
118
  ## Running the test suite
19
119
 
20
120
  You'll need your own API key to run the test suite, you can get one on the [Pexels API Key management page](https://www.pexels.com/api/new/)
data/lib/pexels.rb CHANGED
@@ -1,18 +1,43 @@
1
1
  module Pexels
2
- @api_base_url = 'https://api.pexels.com'
2
+ @api_base_url = ENV['PEXELS_API_BASE_URL'] || 'https://api.pexels.com'
3
+ @api_version = ENV['PEXELS_API_VERSION'] || 'v1'
3
4
 
4
5
  class << self
5
- attr_reader :api_base_url
6
+ attr_reader :api_base_url, :api_version
7
+
8
+ # Local headers can be defined inside a `.headers` file at the project root,
9
+ # with the following format:
10
+ #
11
+ # header1=value
12
+ # header2=value
13
+ # etc.
14
+ #
15
+ def local_headers
16
+ @local_headers ||= if File.exist?('.headers')
17
+ File.read('.headers').split.to_h { |header| header.split('=') }
18
+ else
19
+ {}
20
+ end
21
+ end
6
22
  end
7
23
  end
8
24
 
9
25
  require_relative 'pexels/client'
26
+ require_relative 'pexels/client/request'
27
+ require_relative 'pexels/client/response'
28
+ require_relative 'pexels/client/collections'
10
29
  require_relative 'pexels/client/photos'
11
30
  require_relative 'pexels/client/videos'
12
31
  require_relative 'pexels/version'
13
32
  require_relative 'pexels/errors'
33
+ require_relative 'pexels/collection'
14
34
  require_relative 'pexels/photo'
15
35
  require_relative 'pexels/video'
36
+ require_relative 'pexels/video/file'
37
+ require_relative 'pexels/video/picture'
16
38
  require_relative 'pexels/user'
17
- require_relative 'pexels/search_result'
18
- require_relative 'pexels/curated_result'
39
+ require_relative 'pexels/paginated_response'
40
+ require_relative 'pexels/collection_set'
41
+ require_relative 'pexels/collection_media_set'
42
+ require_relative 'pexels/photo_set'
43
+ require_relative 'pexels/video_set'
data/lib/pexels/client.rb CHANGED
@@ -1,39 +1,29 @@
1
- require 'requests'
2
-
3
- class Pexels::Client
4
- attr_reader :api_key,
5
- :ratelimit_remaining
6
-
7
-
8
- def initialize(api_key = ENV['PEXELS_API_KEY'])
9
- @api_key = api_key
10
- end
11
-
12
- def photos
13
- @photos ||= Pexels::Client::Photos.new(self)
14
- end
15
-
16
- def videos
17
- @videos ||= Pexels::Client::Videos.new(self)
18
- end
19
-
20
- def request(path, method: 'GET', params: {})
21
- results = Requests.request(
22
- method,
23
- "#{Pexels.api_base_url}#{path}",
24
- params: params,
25
- headers: {
26
- 'Authorization' => api_key
27
- }
28
- )
29
-
30
- @ratelimit_remaining = results.headers['x-ratelimit-remaining'].first.to_i
31
-
32
- return JSON.parse(results.body)
33
- rescue StandardError => exception
34
- raise Pexels::APIError.new(exception)
1
+ module Pexels
2
+ class Client
3
+ attr_reader :api_key,
4
+ :ratelimit_remaining
5
+
6
+ def initialize(api_key = ENV['PEXELS_API_KEY'])
7
+ @api_key = api_key
8
+ end
9
+
10
+ def photos
11
+ @photos ||= Pexels::Client::Photos.new(self)
12
+ end
13
+
14
+ def videos
15
+ @videos ||= Pexels::Client::Videos.new(self)
16
+ end
17
+
18
+ def collections
19
+ @collections ||= Pexels::Client::Collections.new(self)
20
+ end
21
+
22
+ def request(path, method: 'GET', params: {})
23
+ request = Request.new(api_key, path, method, params)
24
+ request.call.tap do |response|
25
+ @ratelimit_remaining = response.ratelimit_remaining
26
+ end
27
+ end
35
28
  end
36
29
  end
37
-
38
- require 'pexels/client/photos'
39
- require 'pexels/client/videos'
@@ -0,0 +1,29 @@
1
+ class Pexels::Client::Collections
2
+ def initialize(client)
3
+ @client = client
4
+ end
5
+
6
+ def all(per_page: 15, page: 1)
7
+ response = @client.request(
8
+ "#{Pexels.api_version}/collections",
9
+ params: {
10
+ per_page: per_page,
11
+ page: page
12
+ })
13
+
14
+ Pexels::CollectionSet.new(response)
15
+ end
16
+
17
+ def [](id, type: nil, per_page: 15, page: 1)
18
+ response = @client.request(
19
+ "#{Pexels.api_version}/collections/#{id}",
20
+ params: {
21
+ per_page: per_page,
22
+ page: page,
23
+ type: type
24
+ })
25
+
26
+ Pexels::CollectionMediaSet.new(response)
27
+ end
28
+ alias_method :find, :[]
29
+ end
@@ -4,14 +4,15 @@ class Pexels::Client::Photos
4
4
  @client = client
5
5
  end
6
6
 
7
- def get(id)
8
- results = @client.request("/v1/photos/#{id}")
9
- Pexels::Photo.new(results)
7
+ def [](id)
8
+ response = @client.request("#{Pexels.api_version}/photos/#{id}")
9
+ Pexels::Photo.new(response.body)
10
10
  end
11
+ alias_method :find, :[]
11
12
 
12
13
  def search(query, per_page: 15, page: 1, locale: 'en-US')
13
- results = @client.request(
14
- '/v1/search',
14
+ response = @client.request(
15
+ "#{Pexels.api_version}/search",
15
16
  params: {
16
17
  query: query,
17
18
  per_page: per_page,
@@ -20,18 +21,18 @@ class Pexels::Client::Photos
20
21
  }
21
22
  )
22
23
 
23
- Pexels::SearchResult.new(results)
24
+ Pexels::PhotoSet.new(response)
24
25
  end
25
26
 
26
27
  def curated(per_page: 15, page: 1)
27
- results = @client.request(
28
- '/v1/curated',
28
+ response = @client.request(
29
+ "#{Pexels.api_version}/curated",
29
30
  params: {
30
31
  per_page: per_page,
31
32
  page: page,
32
33
  }
33
34
  )
34
35
 
35
- Pexels::SearchResult.new(results)
36
+ Pexels::PhotoSet.new(response)
36
37
  end
37
38
  end
@@ -0,0 +1,53 @@
1
+ require 'requests'
2
+
3
+ module Pexels
4
+ class Client
5
+ class Request
6
+ attr_reader :api_key, :path, :method, :params
7
+
8
+ def initialize(api_key, path, method, params)
9
+ @api_key = api_key
10
+ @path = path
11
+ @method = method
12
+ @params = params
13
+ end
14
+
15
+ def call
16
+ log_request if ENV['DEBUG']
17
+
18
+ Response.new(self, execute)
19
+
20
+ rescue StandardError => exception
21
+ raise Pexels::APIError.new(exception)
22
+ end
23
+
24
+ private
25
+
26
+ def execute
27
+ Requests.request(
28
+ method,
29
+ url,
30
+ params: params,
31
+ headers: headers
32
+ )
33
+ end
34
+
35
+ def url
36
+ @url ||= File.join(Pexels.api_base_url, path)
37
+ end
38
+
39
+ def headers
40
+ @headers = {
41
+ 'Authorization' => api_key,
42
+ 'User-Agent' => "Pexels/Ruby (#{Pexels::VERSION})"
43
+ }.merge(Pexels.local_headers)
44
+ end
45
+
46
+ def log_request
47
+ puts "Requesting #{url}"
48
+ puts " → params: #{params}"
49
+ puts " → headers: #{headers}"
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,29 @@
1
+ require 'requests'
2
+
3
+ module Pexels
4
+ class Client
5
+ class Response
6
+ attr_reader :request, :response
7
+
8
+ def initialize(request, response)
9
+ @request = request
10
+ @response = response
11
+ end
12
+
13
+ def body
14
+ JSON.parse(response.body)
15
+
16
+ rescue JSON::JSONError => exception
17
+ raise Pexels::APIError.new(exception)
18
+ end
19
+
20
+ def headers
21
+ response.headers
22
+ end
23
+
24
+ def ratelimit_remaining
25
+ headers['x-ratelimit-remaining']&.first&.to_i
26
+ end
27
+ end
28
+ end
29
+ end
@@ -4,13 +4,14 @@ class Pexels::Client::Videos
4
4
  @client = client
5
5
  end
6
6
 
7
- def get(id)
8
- results = @client.request("/videos/videos/#{id}")
9
- Pexels::Video.new(results)
7
+ def [](id)
8
+ response = @client.request("/videos/videos/#{id}")
9
+ Pexels::Video.new(response.body)
10
10
  end
11
+ alias_method :find, :[]
11
12
 
12
13
  def search(query, per_page: 15, page: 1)
13
- results = @client.request(
14
+ response = @client.request(
14
15
  '/videos/search',
15
16
  params: {
16
17
  query: query,
@@ -19,11 +20,11 @@ class Pexels::Client::Videos
19
20
  }
20
21
  )
21
22
 
22
- Pexels::SearchResult.new(results)
23
+ Pexels::VideoSet.new(response)
23
24
  end
24
25
 
25
26
  def popular(per_page: 15, page: 1)
26
- results = @client.request(
27
+ response = @client.request(
27
28
  '/videos/popular',
28
29
  params: {
29
30
  per_page: per_page,
@@ -31,6 +32,6 @@ class Pexels::Client::Videos
31
32
  }
32
33
  )
33
34
 
34
- Pexels::SearchResult.new(results)
35
+ Pexels::VideoSet.new(response)
35
36
  end
36
37
  end
@@ -0,0 +1,25 @@
1
+ module Pexels
2
+ class Collection
3
+ attr_reader :id,
4
+ :title,
5
+ :description,
6
+ :private,
7
+ :media_count,
8
+ :photos_count,
9
+ :videos_count
10
+
11
+
12
+ def initialize(attrs)
13
+ @id = attrs.fetch('id')
14
+ @title = attrs.fetch('title')
15
+ @description = attrs.fetch('description')
16
+ @private = attrs.fetch('private')
17
+ @media_count = attrs.fetch('media_count')
18
+ @photos_count = attrs.fetch('photos_count')
19
+ @videos_count = attrs.fetch('videos_count')
20
+
21
+ rescue KeyError => exception
22
+ raise Pexels::MalformedAPIResponseError.new(exception)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ module Pexels
2
+ class CollectionMediaSet < PaginatedResponse
3
+ alias_method :media, :data
4
+ public :media
5
+
6
+ attr_reader :id
7
+
8
+ def initialize(response)
9
+ super
10
+ @id = attrs.fetch('id')
11
+ @data = attrs.fetch('media', []).map do |attrs|
12
+ if attrs['type'] == 'Photo'
13
+ Pexels::Photo.new(attrs)
14
+ elsif attrs['type'] == 'Video'
15
+ Pexels::Video.new(attrs)
16
+ end
17
+ end
18
+
19
+ rescue KeyError => exception
20
+ raise Pexels::MalformedAPIResponseError.new(exception)
21
+ end
22
+
23
+ def photos
24
+ @photos ||= media.select { |m| m.is_a?(Pexels::Photo) }
25
+ end
26
+
27
+ def videos
28
+ @videos ||= media.select { |m| m.is_a?(Pexels::Video) }
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,11 @@
1
+ module Pexels
2
+ class CollectionSet < PaginatedResponse
3
+ alias_method :collections, :data
4
+ public :collections
5
+
6
+ def initialize(response)
7
+ super
8
+ @data = attrs.fetch('collections', []).map { |attrs| Pexels::Collection.new(attrs) }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,63 @@
1
+ require 'cgi'
2
+
3
+ module Pexels
4
+ class PaginatedResponse
5
+ include Enumerable
6
+
7
+ attr_reader :total_results,
8
+ :page,
9
+ :per_page,
10
+ :data
11
+
12
+ private :data
13
+
14
+ def initialize(response)
15
+ @response = response
16
+ @attrs = @response.body
17
+
18
+ @total_results = attrs.fetch('total_results', nil)
19
+ @page = attrs.fetch('page')
20
+ @per_page = attrs.fetch('per_page')
21
+ @prev_page = attrs.fetch('prev_page', nil)
22
+ @next_page = attrs.fetch('next_page', nil)
23
+ end
24
+
25
+ def total_pages
26
+ total_results.fdiv(per_page).ceil
27
+ end
28
+
29
+ def each(&block)
30
+ if block_given?
31
+ data.each(&block)
32
+ else
33
+ to_enum(:each)
34
+ end
35
+ end
36
+
37
+ def next_page
38
+ return unless @next_page
39
+
40
+ request.params[:page] = extract_page(@next_page)
41
+ self.class.new(request.call)
42
+ end
43
+
44
+ def prev_page
45
+ return unless @prev_page
46
+
47
+ request.params[:page] = extract_page(@next_page)
48
+ self.class.new(request.call)
49
+ end
50
+
51
+ private
52
+
53
+ attr_reader :response, :attrs
54
+
55
+ def request
56
+ response.request
57
+ end
58
+
59
+ def extract_page(url)
60
+ CGI.parse(URI.parse(url).query)['page'].first
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,14 @@
1
+ module Pexels
2
+ class PhotoSet < PaginatedResponse
3
+ alias_method :photos, :data
4
+ public :photos
5
+
6
+ def initialize(response)
7
+ super
8
+ @data = attrs.fetch('photos', []).map { |attrs| Pexels::Photo.new(attrs) }
9
+
10
+ rescue KeyError => exception
11
+ raise Pexels::MalformedAPIResponseError.new(exception)
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module Pexels
2
- VERSION = '0.0.1'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/pexels/video.rb CHANGED
@@ -6,8 +6,8 @@ class Pexels::Video
6
6
  :image,
7
7
  :duration,
8
8
  :user,
9
- :video_files,
10
- :video_pictures
9
+ :files,
10
+ :pictures
11
11
 
12
12
 
13
13
  def initialize(attrs)
@@ -23,6 +23,11 @@ class Pexels::Video
23
23
  url: attrs.fetch('user').fetch('url')
24
24
  )
25
25
 
26
+ @files = attrs.fetch('video_files', []).map { |vf| Pexels::Video::File.new(vf) }
27
+
28
+ @pictures = attrs.fetch('video_pictures', []).map { |vp| Pexels::Video::Picture.new(vp) }
29
+
30
+
26
31
  rescue KeyError => exception
27
32
  raise Pexels::MalformedAPIResponseError.new(exception)
28
33
  end
@@ -0,0 +1,17 @@
1
+ class Pexels::Video::File
2
+ attr_reader :id,
3
+ :quality,
4
+ :file_type,
5
+ :width,
6
+ :height,
7
+ :link
8
+
9
+ def initialize(attrs)
10
+ @id = attrs.fetch('id')
11
+ @quality = attrs.fetch('quality')
12
+ @file_type = attrs.fetch('file_type')
13
+ @width = attrs.fetch('width')
14
+ @height = attrs.fetch('height')
15
+ @link = attrs.fetch('link')
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ class Pexels::Video::Picture
2
+ attr_reader :id,
3
+ :picture
4
+ :nr
5
+
6
+ def initialize(attrs)
7
+ @id = attrs.fetch('id')
8
+ @picture = attrs.fetch('picture')
9
+ @nr = attrs.fetch('nr')
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ module Pexels
2
+ class VideoSet < PaginatedResponse
3
+ alias_method :videos, :data
4
+ public :videos
5
+
6
+ def initialize(response)
7
+ super
8
+ @data = attrs.fetch('videos', []).map { |attrs| Pexels::Video.new(attrs) }
9
+
10
+ rescue KeyError => exception
11
+ raise Pexels::MalformedAPIResponseError.new(exception)
12
+ end
13
+ end
14
+ end
data/pexels.gemspec CHANGED
@@ -11,5 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.license = 'MIT'
12
12
  s.files = `git ls-files`.split("\n")
13
13
 
14
+ s.required_ruby_version = '>= 2.4.0'
15
+
14
16
  s.add_dependency('requests', '~> 1.0.2')
15
17
  end
data/test/client_test.rb CHANGED
@@ -15,7 +15,7 @@ class TestClient < Minitest::Test
15
15
  assert remaining >= 0
16
16
 
17
17
  @client.photos.search('test')
18
- assert_equal remaining, @client.ratelimit_remaining + 1
18
+ assert_equal @client.ratelimit_remaining, remaining - 1
19
19
  end
20
20
 
21
21
  def test_exceptions
@@ -26,7 +26,7 @@ class TestClient < Minitest::Test
26
26
  @client.photos.search('test')
27
27
  raise 'this shouldnt happen'
28
28
  rescue StandardError => exception
29
- assert exception.is_a? Pexels::APIError
29
+ assert_kind_of Pexels::APIError, exception
30
30
  assert exception.message != 'this shouldnt happen'
31
31
  end
32
32
  end
@@ -0,0 +1,83 @@
1
+ require 'minitest/autorun'
2
+ require 'pexels'
3
+
4
+ class TestCollections < Minitest::Test
5
+
6
+ def setup
7
+ @client = Pexels::Client.new(ENV.fetch('PEXELS_API_KEY'))
8
+ @collection = @client.collections.all(per_page: 1).first
9
+ end
10
+
11
+ def test_all
12
+ collection = @client.collections.all
13
+
14
+ assert_kind_of Pexels::CollectionSet, collection
15
+ assert_equal collection.per_page, 15
16
+ assert_equal collection.page, 1
17
+
18
+ assert collection.collections.is_a? Array
19
+ assert collection.collections.any?
20
+ assert collection.first.is_a? Pexels::Collection
21
+
22
+ collection_with_params = @client.collections.all(per_page: 1, page: 2)
23
+ assert_equal collection_with_params.per_page, 1
24
+ assert_equal collection_with_params.page, 2
25
+ assert_equal collection_with_params.collections.length, 1
26
+ assert_kind_of Pexels::CollectionSet, collection_with_params.next_page
27
+ assert_kind_of Pexels::CollectionSet, collection_with_params.prev_page
28
+ end
29
+
30
+ def test_get_collection_media
31
+ collection = @client.collections[@collection.id]
32
+ assert_kind_of Pexels::CollectionMediaSet, collection
33
+ assert_equal collection.id, @collection.id
34
+
35
+ assert_kind_of Array, collection.media
36
+ assert collection.media.any?
37
+
38
+ assert_includes([Pexels::Photo, Pexels::Video], collection.media.first.class)
39
+
40
+ refute_includes([Pexels::Video], collection.photos.map(&:class))
41
+ refute_includes([Pexels::Photo], collection.videos.map(&:class))
42
+ end
43
+
44
+ def test_get_collection_photos
45
+ collection = @client.collections[@collection.id, type: 'photos']
46
+ assert_kind_of Pexels::CollectionMediaSet, collection
47
+ assert_kind_of Array, collection.media
48
+ assert collection.media.all? { |m| m.is_a?(Pexels::Photo) }
49
+ end
50
+
51
+ def test_get_collection_videos
52
+ collection = @client.collections[@collection.id, type: 'videos']
53
+ assert_kind_of Pexels::CollectionMediaSet, collection
54
+ assert_kind_of Array, collection.media
55
+ assert collection.media.all? { |m| m.is_a?(Pexels::Video) }
56
+ end
57
+
58
+ def test_get_collection_invalid_type
59
+ collection = @client.collections[@collection.id, type: 'foo']
60
+ assert_kind_of Pexels::CollectionMediaSet, collection
61
+ assert_kind_of Array, collection.media
62
+ assert collection.any?
63
+ end
64
+
65
+ def test_get_collection_pagination
66
+ collection = @client.collections[@collection.id, per_page: 1, page: 1]
67
+ assert_kind_of Pexels::CollectionMediaSet, collection
68
+ assert_kind_of Array, collection.media
69
+ assert collection.media.any?
70
+
71
+ assert_equal collection.per_page, 1
72
+ assert_equal collection.page, 1
73
+ assert_equal collection.media.length, 1
74
+ end
75
+
76
+ def test_invalid_get_collection
77
+ @client.collections['this-is-not-a-valid-id']
78
+ raise 'This should not happen'
79
+ rescue StandardError => exception
80
+ assert exception.is_a? Pexels::APIError
81
+ assert_equal exception.message, 'Not Found'
82
+ end
83
+ end
data/test/photo_test.rb CHANGED
@@ -5,48 +5,50 @@ class TestPhoto < Minitest::Test
5
5
 
6
6
  def setup
7
7
  @client = Pexels::Client.new(ENV.fetch('PEXELS_API_KEY'))
8
- @photo = @client.photos.search('test', per_page: 1).photos.first
8
+ @photo = @client.photos.search('test', per_page: 1).first
9
9
  end
10
10
 
11
11
  def test_successful_searches
12
12
  search_result = @client.photos.search('test')
13
13
 
14
- assert search_result.is_a? Pexels::SearchResult
15
- assert search_result.next_page.is_a? String
16
- assert search_result.total_results.is_a? Integer
14
+ assert_kind_of Pexels::PhotoSet, search_result
15
+ assert_kind_of Pexels::PhotoSet, search_result.next_page
16
+ assert_kind_of Integer, search_result.total_results
17
17
  assert_equal search_result.per_page, 15
18
18
  assert_equal search_result.page, 1
19
19
 
20
20
  assert search_result.photos.is_a? Array
21
21
  assert search_result.photos.any?
22
- assert search_result.photos.first.is_a? Pexels::Photo
22
+ assert search_result.first.is_a? Pexels::Photo
23
23
 
24
24
  search_result_with_params = @client.photos.search('test', per_page: 1, page: 2)
25
25
  assert_equal search_result_with_params.per_page, 1
26
26
  assert_equal search_result_with_params.page, 2
27
27
  assert_equal search_result_with_params.photos.length, 1
28
+ assert_kind_of Pexels::PhotoSet, search_result_with_params.prev_page
28
29
  end
29
30
 
30
31
  def test_curated_photos
31
32
  search_result = @client.photos.curated
32
33
 
33
- assert search_result.is_a? Pexels::SearchResult
34
- assert search_result.next_page.is_a? String
34
+ assert_kind_of Pexels::PhotoSet, search_result
35
+ assert_kind_of Pexels::PhotoSet, search_result.next_page
35
36
  assert_equal search_result.per_page, 15
36
37
  assert_equal search_result.page, 1
37
38
 
38
39
  assert search_result.photos.is_a? Array
39
40
  assert search_result.photos.any?
40
- assert search_result.photos.first.is_a? Pexels::Photo
41
+ assert search_result.first.is_a? Pexels::Photo
41
42
 
42
43
  search_result_with_params = @client.photos.curated(per_page: 1, page: 2)
43
44
  assert_equal search_result_with_params.per_page, 1
44
45
  assert_equal search_result_with_params.page, 2
45
46
  assert_equal search_result_with_params.photos.length, 1
47
+ assert_kind_of Pexels::PhotoSet, search_result_with_params.prev_page
46
48
  end
47
49
 
48
50
  def test_get_photo
49
- photo = @client.photos.get(@photo.id)
51
+ photo = @client.photos[@photo.id]
50
52
 
51
53
  assert photo.is_a? Pexels::Photo
52
54
 
@@ -61,7 +63,7 @@ class TestPhoto < Minitest::Test
61
63
  end
62
64
 
63
65
  def test_invalid_get_photo
64
- photo = @client.photos.get('this-is-not-a-valid-id')
66
+ photo = @client.photos['this-is-not-a-valid-id']
65
67
  raise 'This should not happen'
66
68
  rescue StandardError => exception
67
69
  assert exception.is_a? Pexels::APIError
data/test/video_test.rb CHANGED
@@ -5,46 +5,50 @@ class TestVideo < Minitest::Test
5
5
 
6
6
  def setup
7
7
  @client = Pexels::Client.new(ENV.fetch('PEXELS_API_KEY'))
8
- @video = @client.videos.search('test', per_page: 1).videos.first
8
+ @video = @client.videos.search('test', per_page: 1).first
9
9
  end
10
10
 
11
11
  def test_successful_searches
12
12
  search_result = @client.videos.search('test')
13
13
 
14
- assert search_result.is_a? Pexels::SearchResult
15
- assert search_result.total_results.is_a? Integer
14
+ assert_kind_of Pexels::VideoSet, search_result
15
+ assert_kind_of Pexels::VideoSet, search_result.next_page
16
+ assert_kind_of Integer, search_result.total_results
16
17
  assert_equal search_result.per_page, 15
17
18
  assert_equal search_result.page, 1
18
19
 
19
20
  assert search_result.videos.is_a? Array
20
21
  assert search_result.videos.any?
21
- assert search_result.videos.first.is_a? Pexels::Video
22
+ assert search_result.first.is_a? Pexels::Video
22
23
 
23
24
  search_result_with_params = @client.videos.search('test', per_page: 1, page: 2)
24
25
  assert_equal search_result_with_params.per_page, 1
25
26
  assert_equal search_result_with_params.page, 2
26
27
  assert_equal search_result_with_params.videos.length, 1
28
+ assert_kind_of Pexels::VideoSet, search_result_with_params.prev_page
27
29
  end
28
30
 
29
31
  def test_popular_videos
30
32
  search_result = @client.videos.popular
31
33
 
32
- assert search_result.is_a? Pexels::SearchResult
34
+ assert_kind_of Pexels::VideoSet, search_result
35
+ assert_kind_of Pexels::VideoSet, search_result.next_page
33
36
  assert_equal search_result.per_page, 15
34
37
  assert_equal search_result.page, 1
35
38
 
36
39
  assert search_result.videos.is_a? Array
37
40
  assert search_result.videos.any?
38
- assert search_result.videos.first.is_a? Pexels::Video
41
+ assert search_result.first.is_a? Pexels::Video
39
42
 
40
43
  search_result_with_params = @client.videos.popular(per_page: 1, page: 2)
41
44
  assert_equal search_result_with_params.per_page, 1
42
45
  assert_equal search_result_with_params.page, 2
43
46
  assert_equal search_result_with_params.videos.length, 1
47
+ assert_kind_of Pexels::VideoSet, search_result_with_params.prev_page
44
48
  end
45
49
 
46
50
  def test_get_video
47
- video = @client.videos.get(@video.id)
51
+ video = @client.videos[@video.id]
48
52
 
49
53
  assert video.is_a? Pexels::Video
50
54
 
@@ -57,17 +61,19 @@ class TestVideo < Minitest::Test
57
61
  assert_equal video.user.name, @video.user.name
58
62
  assert_equal video.user.url, @video.user.url
59
63
  assert_equal video.user.id, @video.user.id
64
+
65
+ assert video.files.is_a?(Array)
66
+ assert video.files.first.is_a?(Pexels::Video::File)
67
+
68
+ assert video.pictures.is_a?(Array)
69
+ assert video.pictures.first.is_a?(Pexels::Video::Picture)
60
70
  end
61
71
 
62
72
  def test_invalid_get_video
63
- video = @client.videos.get('this-is-not-a-valid-id')
73
+ video = @client.videos['this-is-not-a-valid-id']
64
74
  raise 'This should not happen'
65
75
  rescue StandardError => exception
66
76
  assert exception.is_a? Pexels::APIError
67
- #assert_equal exception.message, 'Not Found'
68
- #
69
- ## This is incorrect behavior from the API, which we should change
70
- # once its fixed.
71
- assert_equal exception.message, 'Internal Server Error'
77
+ assert_equal exception.message, 'Not Found'
72
78
  end
73
79
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pexels
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pexels dev team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-19 00:00:00.000000000 Z
11
+ date: 2021-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: requests
@@ -34,22 +34,33 @@ files:
34
34
  - ".env.sample"
35
35
  - ".gems"
36
36
  - ".gitignore"
37
+ - CHANGES.md
37
38
  - LICENSE
38
39
  - Makefile
39
40
  - README.md
40
41
  - lib/pexels.rb
41
42
  - lib/pexels/client.rb
43
+ - lib/pexels/client/collections.rb
42
44
  - lib/pexels/client/photos.rb
45
+ - lib/pexels/client/request.rb
46
+ - lib/pexels/client/response.rb
43
47
  - lib/pexels/client/videos.rb
44
- - lib/pexels/curated_result.rb
48
+ - lib/pexels/collection.rb
49
+ - lib/pexels/collection_media_set.rb
50
+ - lib/pexels/collection_set.rb
45
51
  - lib/pexels/errors.rb
52
+ - lib/pexels/paginated_response.rb
46
53
  - lib/pexels/photo.rb
47
- - lib/pexels/search_result.rb
54
+ - lib/pexels/photo_set.rb
48
55
  - lib/pexels/user.rb
49
56
  - lib/pexels/version.rb
50
57
  - lib/pexels/video.rb
58
+ - lib/pexels/video/file.rb
59
+ - lib/pexels/video/picture.rb
60
+ - lib/pexels/video_set.rb
51
61
  - pexels.gemspec
52
62
  - test/client_test.rb
63
+ - test/collection_test.rb
53
64
  - test/photo_test.rb
54
65
  - test/video_test.rb
55
66
  homepage: https://github.com/pexels/pexels-ruby
@@ -64,7 +75,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
64
75
  requirements:
65
76
  - - ">="
66
77
  - !ruby/object:Gem::Version
67
- version: '0'
78
+ version: 2.4.0
68
79
  required_rubygems_version: !ruby/object:Gem::Requirement
69
80
  requirements:
70
81
  - - ">="
@@ -1,15 +0,0 @@
1
- class Pexels::CuratedResult
2
- attr_reader :photos,
3
- :page,
4
- :per_page,
5
- :next_page
6
-
7
- def initialize(attrs)
8
- @page = attrs.fetch('page')
9
- @per_page = attrs.fetch('per_page')
10
- @next_page = attrs.fetch('next_page')
11
- @photos = attrs.fetch('photos').map { |attrs| Pexels::Photo.new(attrs) }
12
-
13
- return self
14
- end
15
- end
@@ -1,20 +0,0 @@
1
- class Pexels::SearchResult
2
- attr_reader :photos,
3
- :videos,
4
- :total_results,
5
- :page,
6
- :per_page,
7
- :next_page
8
-
9
- def initialize(attrs, type: :Photo)
10
- @total_results = attrs.fetch('total_results', nil)
11
- @page = attrs.fetch('page')
12
- @per_page = attrs.fetch('per_page')
13
- @next_page = attrs.fetch('next_page', nil)
14
-
15
- @photos = attrs.fetch('photos', []).map { |attrs| Pexels::Photo.new(attrs) }
16
- @videos = attrs.fetch('videos', []).map { |attrs| Pexels::Video.new(attrs) }
17
-
18
- return self
19
- end
20
- end