pexels 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1fa972e637620e24f2466be1fa4dea4c68f54e0deae50a40b86adfd6148f6a23
4
- data.tar.gz: b9892dde37db133e3d1e94113a9f00b7a74c21e4a6ac760bd226fcb1160b9d69
3
+ metadata.gz: fbc208fe2d27e3dca7c1b94e0e2a3901334d06b9cf3460d952a44aea437bd82e
4
+ data.tar.gz: f4796400297cf53c41b0740eb57ae9b70efd697018d20c17c0e91f92c9030dc6
5
5
  SHA512:
6
- metadata.gz: c823ada102ccde2f46e9fc8c3d1f82cc8c8914423558ac17f3bcd4168ba1008d83c8f49a479389cd2f1eea2f19b3096eac92cd6d2262c4507439cff5d7450088
7
- data.tar.gz: 29c54e8c7413658efd570604ffe08a024c4af08865d9c3bf3002dcf74c9474d7c4e025924ab239974ce3c2e8ceb3b917ab68ce52084c95f5222b0bbadcdaf023
6
+ metadata.gz: e050b1e25f4f66b370f1acd2695af58305cd221e7c7d9a67cf4f989bdc3959af172aaebc65b545507992af0a6d24594e19601f316f5b1e2145ebbbc59eae46ce
7
+ data.tar.gz: 5a334832f860fcbb21dde178bdd871491d70efdeccfa5515309186596fcd1884b4ca3884bd6a6574343daa40623524ce20137366096460a015372a09365020aa
data/CHANGES.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Change log
2
2
 
3
+ ## 0.3.0
4
+ * Add support for photo and video search with filters.
5
+ * Added `avg_color` attribute to `Photo` object.
6
+
3
7
  ## 0.2.1
4
8
  * Added `type`, `photo?` and `video?` helper methods to `Photo` and `Video` classes.
5
9
 
data/lib/pexels.rb CHANGED
@@ -25,6 +25,7 @@ end
25
25
  require_relative 'pexels/client'
26
26
  require_relative 'pexels/client/request'
27
27
  require_relative 'pexels/client/response'
28
+ require_relative 'pexels/client/search_filters'
28
29
  require_relative 'pexels/client/collections'
29
30
  require_relative 'pexels/client/photos'
30
31
  require_relative 'pexels/client/videos'
@@ -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.body)
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,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.body)
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
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,6 +18,7 @@ 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)
@@ -1,3 +1,3 @@
1
1
  module Pexels
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -74,10 +74,9 @@ class TestCollections < Minitest::Test
74
74
  end
75
75
 
76
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'
77
+ error = assert_raises(Pexels::APIError) do
78
+ @client.collections['this-is-not-a-valid-id']
79
+ end
80
+ assert error.message, 'Not Found'
82
81
  end
83
82
  end
data/test/photo_test.rb CHANGED
@@ -60,6 +60,7 @@ class TestPhoto < Minitest::Test
60
60
  assert_equal photo.user.url, @photo.user.url
61
61
  assert_equal photo.user.id, @photo.user.id
62
62
  assert_equal photo.src, @photo.src
63
+ assert_equal photo.avg_color, @photo.avg_color
63
64
 
64
65
  assert photo.photo?
65
66
  assert_equal photo.type, 'Photo'
@@ -67,10 +68,43 @@ class TestPhoto < Minitest::Test
67
68
  end
68
69
 
69
70
  def test_invalid_get_photo
70
- photo = @client.photos['this-is-not-a-valid-id']
71
- raise 'This should not happen'
72
- rescue StandardError => exception
73
- assert exception.is_a? Pexels::APIError
74
- 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?
75
109
  end
76
110
  end
data/test/video_test.rb CHANGED
@@ -74,10 +74,29 @@ class TestVideo < Minitest::Test
74
74
  end
75
75
 
76
76
  def test_invalid_get_video
77
- video = @client.videos['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'
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?
82
101
  end
83
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.2.1
4
+ version: 0.3.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-15 00:00:00.000000000 Z
11
+ date: 2021-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: requests
@@ -44,6 +44,7 @@ files:
44
44
  - lib/pexels/client/photos.rb
45
45
  - lib/pexels/client/request.rb
46
46
  - lib/pexels/client/response.rb
47
+ - lib/pexels/client/search_filters.rb
47
48
  - lib/pexels/client/videos.rb
48
49
  - lib/pexels/collection.rb
49
50
  - lib/pexels/collection_media_set.rb