pexels 0.2.1 → 0.3.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 +4 -4
- data/CHANGES.md +4 -0
- data/lib/pexels.rb +1 -0
- data/lib/pexels/client/photos.rb +50 -32
- data/lib/pexels/client/search_filters.rb +31 -0
- data/lib/pexels/client/videos.rb +47 -31
- data/lib/pexels/photo.rb +3 -2
- data/lib/pexels/version.rb +1 -1
- data/test/collection_test.rb +4 -5
- data/test/photo_test.rb +39 -5
- data/test/video_test.rb +24 -5
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fbc208fe2d27e3dca7c1b94e0e2a3901334d06b9cf3460d952a44aea437bd82e
|
4
|
+
data.tar.gz: f4796400297cf53c41b0740eb57ae9b70efd697018d20c17c0e91f92c9030dc6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e050b1e25f4f66b370f1acd2695af58305cd221e7c7d9a67cf4f989bdc3959af172aaebc65b545507992af0a6d24594e19601f316f5b1e2145ebbbc59eae46ce
|
7
|
+
data.tar.gz: 5a334832f860fcbb21dde178bdd871491d70efdeccfa5515309186596fcd1884b4ca3884bd6a6574343daa40623524ce20137366096460a015372a09365020aa
|
data/CHANGES.md
CHANGED
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'
|
data/lib/pexels/client/photos.rb
CHANGED
@@ -1,38 +1,56 @@
|
|
1
|
-
|
1
|
+
module Pexels
|
2
|
+
class Client
|
3
|
+
class Photos
|
4
|
+
include SearchFilters
|
2
5
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
+
def initialize(client)
|
7
|
+
@client = client
|
8
|
+
end
|
6
9
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
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
|
-
|
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
|
data/lib/pexels/client/videos.rb
CHANGED
@@ -1,37 +1,53 @@
|
|
1
|
-
|
1
|
+
module Pexels
|
2
|
+
class Client
|
3
|
+
class Videos
|
4
|
+
include SearchFilters
|
2
5
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
+
def initialize(client)
|
7
|
+
@client = client
|
8
|
+
end
|
6
9
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
27
|
-
response = @client.request(
|
28
|
-
'/videos/popular',
|
29
|
-
params: {
|
30
|
-
per_page: per_page,
|
31
|
-
page: page,
|
32
|
-
}
|
33
|
-
)
|
45
|
+
private
|
34
46
|
|
35
|
-
|
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)
|
data/lib/pexels/version.rb
CHANGED
data/test/collection_test.rb
CHANGED
@@ -74,10 +74,9 @@ class TestCollections < Minitest::Test
|
|
74
74
|
end
|
75
75
|
|
76
76
|
def test_invalid_get_collection
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
assert
|
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
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
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.
|
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-
|
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
|