pexels 0.1.0 → 0.4.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: 7f9609676bbaebe34889f5c6483f15f41831439eced062f91a7a1eb8fb0c1d8e
4
- data.tar.gz: a7be0c896cacd8e3bf02aaf5a87eaee7c0a7c3a17d1118b87f114da208972e0f
3
+ metadata.gz: e749787cf73bb5029e528f9d0232d2ce6c638dc81d13c12c7e91346b8962b2be
4
+ data.tar.gz: a531ae402dc483373295381e4fe613e0a169b813081a3964692b81d5af9bbaa4
5
5
  SHA512:
6
- metadata.gz: 506d3d399c0cc6655daead44255597131d162808b6b206e32aa7fd60022883703330f2527fb6d092b944ba31a9067b813f6e27f355f7dbc87dcbb3ab95984f2c
7
- data.tar.gz: c72ebaa5715f520f74a05941f384bcd1e6db62d45662f07f9a8afbc5236b85c6aa4527a1ccf6f2a74b9d95f36e42fe56c12333ee12411076a8cb6b0170c75105
6
+ metadata.gz: ad853babd229b319f25253cf036d8fd97b7c9262de26566d36d7fec7ddd4d7a2240038b200f4fca0551e30608038c4de6cc6844594cc3c8720368d26dd49a7e6
7
+ data.tar.gz: aeda8080d6ad67c5aa1175081481337a2be0e7f1ed46619f2e5ccbee2944297b83aa7d3e4bae0df8949a04d80356a2e01f325dc539b874d6fa29e9b64f9e8906
data/CHANGES.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Change log
2
2
 
3
+ ## 0.4.0
4
+ * Add support for returning featured collections.
5
+
6
+ ## 0.3.0
7
+ * Add support for photo and video search with filters.
8
+ * Added `avg_color` attribute to `Photo` object.
9
+
10
+ ## 0.2.1
11
+ * Added `type`, `photo?` and `video?` helper methods to `Photo` and `Video` classes.
12
+
13
+ ## 0.2.0
14
+ * Fixed incorrect URL for collections endpoints.
15
+ * Added pagination methods `next_page` and `prev_page` to `PaginatedResponse`.
16
+ * Added `total_pages` to `PaginatedResponse`.
17
+ * Extracted `Request` and `Response` objects for reusability.
18
+ * Added `Pexels/Ruby` `User-Agent` header to requests.
19
+
3
20
  ## 0.1.0
4
21
  * Add support for returning collections belonging to the API user.
5
22
  * Add support for returning media from a collection.
data/README.md CHANGED
@@ -30,10 +30,18 @@ client = Pexels::Client.new('your-access-key')
30
30
  client.photos.search('Cloud')
31
31
  ```
32
32
 
33
+ ### Search for photos with filters
34
+
35
+ ```ruby
36
+ client.photos.search('Dog', color: :yellow, size: :large, orientation: :square)
37
+ ```
38
+
33
39
  ### Find a specific photo
34
40
 
35
41
  ```ruby
36
- client.photos[2014422] || client.photos.find(2014422)
42
+ client.photos[2014422]
43
+ # or
44
+ client.photos.find(2014422)
37
45
  ```
38
46
 
39
47
  ### Browse curated photos
@@ -48,10 +56,18 @@ client.photos.curated
48
56
  client.videos.search('waves')
49
57
  ```
50
58
 
59
+ ### Search for videos with filters
60
+
61
+ ```ruby
62
+ client.videos.search('Beach', size: :medium, orientation: :landscape)
63
+ ```
64
+
51
65
  ### Find a specific photo
52
66
 
53
67
  ```ruby
54
- client.videos[2014422] || client.videos.find(2014422)
68
+ client.videos[2014422]
69
+ # or
70
+ client.videos.find(2014422)
55
71
  ```
56
72
 
57
73
  ### Browse popular videos
@@ -60,6 +76,62 @@ client.videos[2014422] || client.videos.find(2014422)
60
76
  client.videos.popular
61
77
  ```
62
78
 
79
+ ### List all collections
80
+
81
+ Note: this is limited to collections belonging to the API user.
82
+
83
+ ```ruby
84
+ client.collections.all
85
+ ```
86
+ ### List all featured collections.
87
+
88
+ ```ruby
89
+ client.collections.featured.all
90
+ ```
91
+
92
+ ### Get all media for a collection
93
+
94
+ ```ruby
95
+ client.collections['collection-id'].media
96
+ # or
97
+ client.collections.find('collection-id').media
98
+ ```
99
+
100
+ You can also filter for only `photos` or `videos`.
101
+
102
+ ```ruby
103
+ client.collections['collection-id', type: 'photos'].media
104
+ client.collections['collection-id', type: 'videos'].media
105
+ ```
106
+
107
+ ## Rate Limiting
108
+
109
+ After performing a request, you can access your remaining rate limit via the client.
110
+
111
+ ```ruby
112
+ client.ratelimit_remaining
113
+ ```
114
+
115
+ ## Pagination
116
+
117
+ 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.
118
+
119
+ Note: The Pexels API returns a maximum of 80 records for one request.
120
+
121
+ ```ruby
122
+ response = client.photos.search('dog', page: 2, per_page: 50)
123
+ response.total_results #=> 1000
124
+ response.total_pages #= 20
125
+ ```
126
+
127
+ If there are further pages, you can also paginate through the API client:
128
+
129
+ ```ruby
130
+ response = client.photos.search('dog', page: 2, per_page: 50)
131
+ response.prev_page # queries page 1
132
+ response.next_page # queries page 3
133
+ ```
134
+
63
135
  ## Running the test suite
64
136
 
65
137
  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
@@ -23,6 +23,9 @@ module Pexels
23
23
  end
24
24
 
25
25
  require_relative 'pexels/client'
26
+ require_relative 'pexels/client/request'
27
+ require_relative 'pexels/client/response'
28
+ require_relative 'pexels/client/search_filters'
26
29
  require_relative 'pexels/client/collections'
27
30
  require_relative 'pexels/client/photos'
28
31
  require_relative 'pexels/client/videos'
data/lib/pexels/client.rb CHANGED
@@ -1,47 +1,29 @@
1
- require 'requests'
2
-
3
- class Pexels::Client
4
- attr_reader :api_key,
5
- :ratelimit_remaining
6
-
7
- def initialize(api_key = ENV['PEXELS_API_KEY'])
8
- @api_key = api_key
9
- end
10
-
11
- def photos
12
- @photos ||= Pexels::Client::Photos.new(self)
13
- end
14
-
15
- def videos
16
- @videos ||= Pexels::Client::Videos.new(self)
17
- end
18
-
19
- def collections
20
- @collections ||= Pexels::Client::Collections.new(self)
21
- end
22
-
23
- def request(path, method: 'GET', params: {})
24
- url = File.join(Pexels.api_base_url, path)
25
- headers = {
26
- 'Authorization' => api_key
27
- }.merge(Pexels.local_headers)
28
-
29
- puts "Requesting #{url} with #{headers}" if ENV['DEBUG']
30
-
31
- results = Requests.request(
32
- method,
33
- url,
34
- params: params,
35
- headers: headers
36
- )
37
-
38
- @ratelimit_remaining = results.headers['x-ratelimit-remaining']&.first&.to_i
39
-
40
- return JSON.parse(results.body)
41
- rescue StandardError => exception
42
- 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
43
28
  end
44
29
  end
45
-
46
- require 'pexels/client/photos'
47
- require 'pexels/client/videos'
@@ -5,7 +5,18 @@ class Pexels::Client::Collections
5
5
 
6
6
  def all(per_page: 15, page: 1)
7
7
  response = @client.request(
8
- '/collections',
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 featured(per_page: 15, page: 1)
18
+ response = @client.request(
19
+ "#{Pexels.api_version}/collections/featured",
9
20
  params: {
10
21
  per_page: per_page,
11
22
  page: page
@@ -16,7 +27,7 @@ class Pexels::Client::Collections
16
27
 
17
28
  def [](id, type: nil, per_page: 15, page: 1)
18
29
  response = @client.request(
19
- "/collections/#{id}",
30
+ "#{Pexels.api_version}/collections/#{id}",
20
31
  params: {
21
32
  per_page: per_page,
22
33
  page: page,
@@ -1,38 +1,56 @@
1
- class Pexels::Client::Photos
1
+ module Pexels
2
+ class Client
3
+ class Photos
4
+ include SearchFilters
2
5
 
3
- def initialize(client)
4
- @client = client
5
- end
6
+ def initialize(client)
7
+ @client = client
8
+ end
6
9
 
7
- def [](id)
8
- response = @client.request("#{Pexels.api_version}/photos/#{id}")
9
- Pexels::Photo.new(response)
10
- end
11
- alias_method :find, :[]
12
-
13
- def search(query, per_page: 15, page: 1, locale: 'en-US')
14
- response = @client.request(
15
- "#{Pexels.api_version}/search",
16
- params: {
17
- query: query,
18
- per_page: per_page,
19
- page: page,
20
- locale: locale
21
- }
22
- )
23
-
24
- Pexels::PhotoSet.new(response)
25
- end
10
+ def [](id)
11
+ response = @client.request("#{Pexels.api_version}/photos/#{id}")
12
+ Pexels::Photo.new(response.body)
13
+ end
14
+ alias_method :find, :[]
15
+
16
+ def search(query, per_page: 15, page: 1, locale: 'en-US', orientation: nil, size: nil, color: nil)
17
+ validate_search_params(orientation, size, color)
18
+
19
+ response = @client.request(
20
+ "#{Pexels.api_version}/search",
21
+ params: {
22
+ query: query,
23
+ per_page: per_page,
24
+ page: page,
25
+ locale: locale,
26
+ orientation: orientation,
27
+ size: size,
28
+ color: color
29
+ }.compact
30
+ )
31
+
32
+ Pexels::PhotoSet.new(response)
33
+ end
34
+
35
+ def curated(per_page: 15, page: 1)
36
+ response = @client.request(
37
+ "#{Pexels.api_version}/curated",
38
+ params: {
39
+ per_page: per_page,
40
+ page: page
41
+ }
42
+ )
43
+
44
+ Pexels::PhotoSet.new(response)
45
+ end
26
46
 
27
- def curated(per_page: 15, page: 1)
28
- response = @client.request(
29
- "#{Pexels.api_version}/curated",
30
- params: {
31
- per_page: per_page,
32
- page: page,
33
- }
34
- )
47
+ private
35
48
 
36
- Pexels::PhotoSet.new(response)
49
+ def validate_search_params(orientation, size, color)
50
+ validate_orientation(orientation) &&
51
+ validate_size(size) &&
52
+ validate_color(color)
53
+ end
54
+ end
37
55
  end
38
56
  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
@@ -0,0 +1,31 @@
1
+ module Pexels
2
+ class Client
3
+ module SearchFilters
4
+ ORIENTATIONS = %w(portrait landscape square).freeze
5
+ SIZES = %w(small medium large).freeze
6
+ COLORS = %w(red orange yellow green turquoise blue violet pink brown black gray white).freeze
7
+
8
+ def validate_orientation(orientation)
9
+ return true unless orientation
10
+ return true if ORIENTATIONS.include?(orientation.to_s)
11
+
12
+ raise ArgumentError, "`orientation` must be one of #{ORIENTATIONS.join(', ')}."
13
+ end
14
+
15
+ def validate_size(size)
16
+ return true unless size
17
+ return true if SIZES.include?(size.to_s)
18
+
19
+ raise ArgumentError, "`size` must be one of #{SIZES.join(', ')}."
20
+ end
21
+
22
+ def validate_color(color)
23
+ return true unless color
24
+ return true if COLORS.include?(color.to_s)
25
+ return true if color.to_s =~ /\A#?(?:[0-9a-f]{3}){1,2}\z/i
26
+
27
+ raise ArgumentError, "`color` must be one of #{COLORS.join(', ')} or a valid hex code."
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,37 +1,53 @@
1
- class Pexels::Client::Videos
1
+ module Pexels
2
+ class Client
3
+ class Videos
4
+ include SearchFilters
2
5
 
3
- def initialize(client)
4
- @client = client
5
- end
6
+ def initialize(client)
7
+ @client = client
8
+ end
6
9
 
7
- def [](id)
8
- response = @client.request("/videos/videos/#{id}")
9
- Pexels::Video.new(response)
10
- end
11
- alias_method :find, :[]
12
-
13
- def search(query, per_page: 15, page: 1)
14
- response = @client.request(
15
- '/videos/search',
16
- params: {
17
- query: query,
18
- per_page: per_page,
19
- page: page,
20
- }
21
- )
22
-
23
- Pexels::VideoSet.new(response)
24
- end
10
+ def [](id)
11
+ response = @client.request("/videos/videos/#{id}")
12
+ Pexels::Video.new(response.body)
13
+ end
14
+ alias_method :find, :[]
15
+
16
+ def search(query, per_page: 15, page: 1, orientation: nil, size: nil)
17
+ validate_search_params(orientation, size)
18
+
19
+ response = @client.request(
20
+ '/videos/search',
21
+ params: {
22
+ query: query,
23
+ per_page: per_page,
24
+ page: page,
25
+ orientation: orientation,
26
+ size: size
27
+ }.compact
28
+ )
29
+
30
+ Pexels::VideoSet.new(response)
31
+ end
32
+
33
+ def popular(per_page: 15, page: 1)
34
+ response = @client.request(
35
+ '/videos/popular',
36
+ params: {
37
+ per_page: per_page,
38
+ page: page,
39
+ }
40
+ )
41
+
42
+ Pexels::VideoSet.new(response)
43
+ end
25
44
 
26
- def popular(per_page: 15, page: 1)
27
- response = @client.request(
28
- '/videos/popular',
29
- params: {
30
- per_page: per_page,
31
- page: page,
32
- }
33
- )
45
+ private
34
46
 
35
- Pexels::VideoSet.new(response)
47
+ def validate_search_params(orientation, size)
48
+ validate_orientation(orientation) &&
49
+ validate_size(size)
50
+ end
51
+ end
36
52
  end
37
53
  end
@@ -5,7 +5,7 @@ module Pexels
5
5
 
6
6
  attr_reader :id
7
7
 
8
- def initialize(attrs)
8
+ def initialize(response)
9
9
  super
10
10
  @id = attrs.fetch('id')
11
11
  @data = attrs.fetch('media', []).map do |attrs|
@@ -3,7 +3,7 @@ module Pexels
3
3
  alias_method :collections, :data
4
4
  public :collections
5
5
 
6
- def initialize(attrs)
6
+ def initialize(response)
7
7
  super
8
8
  @data = attrs.fetch('collections', []).map { |attrs| Pexels::Collection.new(attrs) }
9
9
  end
@@ -1,3 +1,5 @@
1
+ require 'cgi'
2
+
1
3
  module Pexels
2
4
  class PaginatedResponse
3
5
  include Enumerable
@@ -5,18 +7,25 @@ module Pexels
5
7
  attr_reader :total_results,
6
8
  :page,
7
9
  :per_page,
8
- :next_page,
9
10
  :data
10
11
 
11
12
  private :data
12
13
 
13
- def initialize(attrs)
14
+ def initialize(response)
15
+ @response = response
16
+ @attrs = @response.body
17
+
14
18
  @total_results = attrs.fetch('total_results', nil)
15
19
  @page = attrs.fetch('page')
16
20
  @per_page = attrs.fetch('per_page')
21
+ @prev_page = attrs.fetch('prev_page', nil)
17
22
  @next_page = attrs.fetch('next_page', nil)
18
23
  end
19
24
 
25
+ def total_pages
26
+ total_results.fdiv(per_page).ceil
27
+ end
28
+
20
29
  def each(&block)
21
30
  if block_given?
22
31
  data.each(&block)
@@ -24,5 +33,31 @@ module Pexels
24
33
  to_enum(:each)
25
34
  end
26
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
27
62
  end
28
63
  end
data/lib/pexels/photo.rb CHANGED
@@ -4,8 +4,8 @@ class Pexels::Photo
4
4
  :height,
5
5
  :url,
6
6
  :user,
7
- :src
8
-
7
+ :src,
8
+ :avg_color
9
9
 
10
10
  def initialize(attrs)
11
11
  @id = attrs.fetch('id')
@@ -18,8 +18,21 @@ class Pexels::Photo
18
18
  url: attrs.fetch('photographer_url')
19
19
  )
20
20
  @src = attrs.fetch('src')
21
+ @avg_color = attrs.fetch('avg_color')
21
22
 
22
23
  rescue KeyError => exception
23
24
  raise Pexels::MalformedAPIResponseError.new(exception)
24
25
  end
26
+
27
+ def type
28
+ 'Photo'
29
+ end
30
+
31
+ def photo?
32
+ true
33
+ end
34
+
35
+ def video?
36
+ false
37
+ end
25
38
  end
@@ -3,7 +3,7 @@ module Pexels
3
3
  alias_method :photos, :data
4
4
  public :photos
5
5
 
6
- def initialize(attrs)
6
+ def initialize(response)
7
7
  super
8
8
  @data = attrs.fetch('photos', []).map { |attrs| Pexels::Photo.new(attrs) }
9
9
 
@@ -1,3 +1,3 @@
1
1
  module Pexels
2
- VERSION = '0.1.0'
2
+ VERSION = '0.4.0'
3
3
  end
data/lib/pexels/video.rb CHANGED
@@ -31,4 +31,16 @@ class Pexels::Video
31
31
  rescue KeyError => exception
32
32
  raise Pexels::MalformedAPIResponseError.new(exception)
33
33
  end
34
+
35
+ def type
36
+ 'Video'
37
+ end
38
+
39
+ def photo?
40
+ false
41
+ end
42
+
43
+ def video?
44
+ true
45
+ end
34
46
  end
@@ -3,7 +3,7 @@ module Pexels
3
3
  alias_method :videos, :data
4
4
  public :videos
5
5
 
6
- def initialize(attrs)
6
+ def initialize(response)
7
7
  super
8
8
  @data = attrs.fetch('videos', []).map { |attrs| Pexels::Video.new(attrs) }
9
9
 
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
@@ -9,28 +9,49 @@ class TestCollections < Minitest::Test
9
9
  end
10
10
 
11
11
  def test_all
12
- search_result = @client.collections.all
12
+ collection = @client.collections.all
13
13
 
14
- assert search_result.is_a? Pexels::CollectionSet
15
- assert_equal search_result.per_page, 15
16
- assert_equal search_result.page, 1
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_featured
31
+ collection = @client.collections.featured
32
+
33
+ assert_kind_of Pexels::CollectionSet, collection
34
+ assert_equal collection.per_page, 15
35
+ assert_equal collection.page, 1
17
36
 
18
- assert search_result.collections.is_a? Array
19
- assert search_result.collections.any?
20
- assert search_result.first.is_a? Pexels::Collection
37
+ assert collection.collections.is_a? Array
38
+ assert collection.collections.any?
39
+ assert collection.first.is_a? Pexels::Collection
21
40
 
22
- search_result_with_params = @client.collections.all(per_page: 1, page: 2)
23
- assert_equal search_result_with_params.per_page, 1
24
- assert_equal search_result_with_params.page, 2
25
- assert_equal search_result_with_params.collections.length, 1
41
+ collection_with_params = @client.collections.featured(per_page: 1, page: 2)
42
+ assert_equal collection_with_params.per_page, 1
43
+ assert_equal collection_with_params.page, 2
44
+ assert_equal collection_with_params.collections.length, 1
45
+ assert_kind_of Pexels::CollectionSet, collection_with_params.next_page
46
+ assert_kind_of Pexels::CollectionSet, collection_with_params.prev_page
26
47
  end
27
48
 
28
49
  def test_get_collection_media
29
50
  collection = @client.collections[@collection.id]
30
- assert collection.is_a? Pexels::CollectionMediaSet
51
+ assert_kind_of Pexels::CollectionMediaSet, collection
31
52
  assert_equal collection.id, @collection.id
32
53
 
33
- assert collection.media.is_a? Array
54
+ assert_kind_of Array, collection.media
34
55
  assert collection.media.any?
35
56
 
36
57
  assert_includes([Pexels::Photo, Pexels::Video], collection.media.first.class)
@@ -41,29 +62,29 @@ class TestCollections < Minitest::Test
41
62
 
42
63
  def test_get_collection_photos
43
64
  collection = @client.collections[@collection.id, type: 'photos']
44
- assert collection.is_a? Pexels::CollectionMediaSet
45
- assert collection.media.is_a? Array
65
+ assert_kind_of Pexels::CollectionMediaSet, collection
66
+ assert_kind_of Array, collection.media
46
67
  assert collection.media.all? { |m| m.is_a?(Pexels::Photo) }
47
68
  end
48
69
 
49
70
  def test_get_collection_videos
50
71
  collection = @client.collections[@collection.id, type: 'videos']
51
- assert collection.is_a? Pexels::CollectionMediaSet
52
- assert collection.media.is_a? Array
72
+ assert_kind_of Pexels::CollectionMediaSet, collection
73
+ assert_kind_of Array, collection.media
53
74
  assert collection.media.all? { |m| m.is_a?(Pexels::Video) }
54
75
  end
55
76
 
56
77
  def test_get_collection_invalid_type
57
78
  collection = @client.collections[@collection.id, type: 'foo']
58
- assert collection.is_a? Pexels::CollectionMediaSet
59
- assert collection.media.is_a? Array
79
+ assert_kind_of Pexels::CollectionMediaSet, collection
80
+ assert_kind_of Array, collection.media
60
81
  assert collection.any?
61
82
  end
62
83
 
63
84
  def test_get_collection_pagination
64
85
  collection = @client.collections[@collection.id, per_page: 1, page: 1]
65
- assert collection.is_a? Pexels::CollectionMediaSet
66
- assert collection.media.is_a? Array
86
+ assert_kind_of Pexels::CollectionMediaSet, collection
87
+ assert_kind_of Array, collection.media
67
88
  assert collection.media.any?
68
89
 
69
90
  assert_equal collection.per_page, 1
@@ -72,10 +93,9 @@ class TestCollections < Minitest::Test
72
93
  end
73
94
 
74
95
  def test_invalid_get_collection
75
- @client.collections['this-is-not-a-valid-id']
76
- raise 'This should not happen'
77
- rescue StandardError => exception
78
- assert exception.is_a? Pexels::APIError
79
- assert_equal exception.message, 'Not Found'
96
+ error = assert_raises(Pexels::APIError) do
97
+ @client.collections['this-is-not-a-valid-id']
98
+ end
99
+ assert error.message, 'Not Found'
80
100
  end
81
101
  end
data/test/photo_test.rb CHANGED
@@ -11,9 +11,9 @@ class TestPhoto < Minitest::Test
11
11
  def test_successful_searches
12
12
  search_result = @client.photos.search('test')
13
13
 
14
- assert search_result.is_a? Pexels::PhotoSet
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
 
@@ -25,13 +25,14 @@ class TestPhoto < Minitest::Test
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::PhotoSet
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
 
@@ -43,6 +44,7 @@ class TestPhoto < Minitest::Test
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
@@ -58,13 +60,51 @@ class TestPhoto < Minitest::Test
58
60
  assert_equal photo.user.url, @photo.user.url
59
61
  assert_equal photo.user.id, @photo.user.id
60
62
  assert_equal photo.src, @photo.src
63
+ assert_equal photo.avg_color, @photo.avg_color
64
+
65
+ assert photo.photo?
66
+ assert_equal photo.type, 'Photo'
67
+ refute photo.video?
61
68
  end
62
69
 
63
70
  def test_invalid_get_photo
64
- photo = @client.photos['this-is-not-a-valid-id']
65
- raise 'This should not happen'
66
- rescue StandardError => exception
67
- assert exception.is_a? Pexels::APIError
68
- assert_equal exception.message, 'Not Found'
71
+ error = assert_raises(Pexels::APIError) do
72
+ @client.photos['this-is-not-a-valid-id']
73
+ end
74
+ assert_equal 'Not Found', error.message
75
+ end
76
+
77
+ def test_invalid_orientation
78
+ error = assert_raises(ArgumentError) do
79
+ @client.photos.search('dog', orientation: 'foo')
80
+ end
81
+ assert_match '`orientation` must be one of', error.message
82
+ end
83
+
84
+ def test_invalid_size
85
+ error = assert_raises(ArgumentError) do
86
+ @client.photos.search('dog', size: 'foo')
87
+ end
88
+ assert_match '`size` must be one of', error.message
89
+ end
90
+
91
+ def test_invalid_color
92
+ error = assert_raises(ArgumentError) do
93
+ @client.photos.search('dog', color: 'foo')
94
+ end
95
+ assert_match '`color` must be one of', error.message
96
+ end
97
+
98
+ def test_invalid_color_hex
99
+ error = assert_raises(ArgumentError) do
100
+ @client.photos.search('dog', color: '#gggggg')
101
+ end
102
+ assert_match '`color` must be one of', error.message
103
+ end
104
+
105
+ def test_search_filters
106
+ search_result = @client.photos.search('dog', color: '#FF0000', size: :large, orientation: :square)
107
+ assert_kind_of Pexels::PhotoSet, search_result
108
+ assert search_result.photos.any?
69
109
  end
70
110
  end
data/test/video_test.rb CHANGED
@@ -11,8 +11,9 @@ class TestVideo < Minitest::Test
11
11
  def test_successful_searches
12
12
  search_result = @client.videos.search('test')
13
13
 
14
- assert search_result.is_a? Pexels::VideoSet
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
 
@@ -24,12 +25,14 @@ class TestVideo < Minitest::Test
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::VideoSet
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
 
@@ -41,6 +44,7 @@ class TestVideo < Minitest::Test
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
@@ -53,6 +57,10 @@ class TestVideo < Minitest::Test
53
57
  assert_equal video.height, @video.height
54
58
  assert_equal video.url, @video.url
55
59
 
60
+ assert video.video?
61
+ assert_equal video.type, 'Video'
62
+ refute video.photo?
63
+
56
64
  assert video.user.is_a?(Pexels::User)
57
65
  assert_equal video.user.name, @video.user.name
58
66
  assert_equal video.user.url, @video.user.url
@@ -66,10 +74,29 @@ class TestVideo < Minitest::Test
66
74
  end
67
75
 
68
76
  def test_invalid_get_video
69
- video = @client.videos['this-is-not-a-valid-id']
70
- raise 'This should not happen'
71
- rescue StandardError => exception
72
- assert exception.is_a? Pexels::APIError
73
- assert_equal exception.message, 'Not Found'
77
+ error = assert_raises(Pexels::APIError) do
78
+ @client.videos['this-is-not-a-valid-id']
79
+ end
80
+ assert_equal 'Not Found', error.message
81
+ end
82
+
83
+ def test_invalid_orientation
84
+ error = assert_raises(ArgumentError) do
85
+ @client.photos.search('dog', orientation: 'foo')
86
+ end
87
+ assert_match '`orientation` must be one of', error.message
88
+ end
89
+
90
+ def test_invalid_size
91
+ error = assert_raises(ArgumentError) do
92
+ @client.photos.search('dog', size: 'foo')
93
+ end
94
+ assert_match '`size` must be one of', error.message
95
+ end
96
+
97
+ def test_search_filters
98
+ search_result = @client.videos.search('cat', size: :medium, orientation: :square)
99
+ assert_kind_of Pexels::VideoSet, search_result
100
+ assert search_result.videos.any?
74
101
  end
75
102
  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.1.0
4
+ version: 0.4.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: 2021-04-09 00:00:00.000000000 Z
11
+ date: 2021-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: requests
@@ -42,6 +42,9 @@ files:
42
42
  - lib/pexels/client.rb
43
43
  - lib/pexels/client/collections.rb
44
44
  - lib/pexels/client/photos.rb
45
+ - lib/pexels/client/request.rb
46
+ - lib/pexels/client/response.rb
47
+ - lib/pexels/client/search_filters.rb
45
48
  - lib/pexels/client/videos.rb
46
49
  - lib/pexels/collection.rb
47
50
  - lib/pexels/collection_media_set.rb
@@ -73,7 +76,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
73
76
  requirements:
74
77
  - - ">="
75
78
  - !ruby/object:Gem::Version
76
- version: '0'
79
+ version: 2.4.0
77
80
  required_rubygems_version: !ruby/object:Gem::Requirement
78
81
  requirements:
79
82
  - - ">="