pexels 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c35b5ede38be050f349b46e1acfa441306af1ccf5acdf89ec9e442d4474a960d
4
+ data.tar.gz: ea3bb7174df2b5c135fa272bba90a4a5a42133078d7f52e2944ccf663f800518
5
+ SHA512:
6
+ metadata.gz: b18c77442467ef348fde20120245d4ba880f8dd4417a2d60aabce80c3b44c876a385c413c925d5348b703984a703c98ac0f5c86e97a6a47ea25fcbedd256c62f
7
+ data.tar.gz: f7e2e10664aca352d80b7479309e638d6e447b9dd59347eedcbb38ae79626852e7a7463ba9ebb396911d922c9732b4d233322d871e59bfc85f8dfe00c986b93e
data/.env.sample ADDED
@@ -0,0 +1,8 @@
1
+ # vim: ft=sh
2
+ export GEM_HOME="$PWD/.dependencies"
3
+ export GEM_PATH="$PWD/.dependencies"
4
+ export RUBYLIB=lib:$RUBYLIB
5
+ export GEM_BIN="$GEM_HOME/bin"
6
+ export PATH="$GEM_BIN":"$PATH"
7
+
8
+ export PEXELS_API_KEY="foo"
data/.gems ADDED
@@ -0,0 +1,2 @@
1
+ requests -v 1.0.2
2
+ minitest -v 5.11.3
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .gems.up.to.date
2
+ .env
3
+ *.gem
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2020 Pexels GmbH <partnerships@pexels.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/Makefile ADDED
@@ -0,0 +1,15 @@
1
+ test: .gems.up.to.date
2
+ ruby -Ilib -e 'ARGV.each { |f| require f }' ./test/*.rb
3
+
4
+ .gems.up.to.date: .gems
5
+ (which dep || gem install dep) && dep install && touch $@
6
+
7
+ console: .gems.up.to.date
8
+ irb -r pexels
9
+
10
+ build: .gems.up.to.date
11
+ gem build pexels.gemspec
12
+
13
+ all: test
14
+
15
+ .PHONY: test console build
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # Pexels Ruby Library
2
+
3
+ The Pexels Ruby library is a convenient wrapper around the Pexels API that you can use to browse the incredible content uploaded by our talented contributors.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ gem install pexels
9
+ ```
10
+
11
+ Or add it to your `Gemfile` and run `bundle install`.
12
+
13
+ ## Documentation
14
+
15
+ See the API docs [here](https://www.pexels.com/api/documentation/?language=js)
16
+
17
+
18
+ ## Running the test suite
19
+
20
+ 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/)
21
+
22
+ For ease of use, you'll probably want to copy the provided `.env.sample` into your own `.env` file and substitute the `PEXELS_API_KEY` variable with your own.
23
+
24
+ ```
25
+ cp .env.sample .env
26
+ vim .env #=> Change PEXELS_API_KEY
27
+ source .env #=> Load the environment
28
+
29
+ make test
30
+ ```
@@ -0,0 +1,37 @@
1
+ class Pexels::Client::Photos
2
+
3
+ def initialize(client)
4
+ @client = client
5
+ end
6
+
7
+ def get(id)
8
+ results = @client.request("/v1/photos/#{id}")
9
+ Pexels::Photo.new(results)
10
+ end
11
+
12
+ def search(query, per_page: 15, page: 1, locale: 'en-US')
13
+ results = @client.request(
14
+ '/v1/search',
15
+ params: {
16
+ query: query,
17
+ per_page: per_page,
18
+ page: page,
19
+ locale: locale
20
+ }
21
+ )
22
+
23
+ Pexels::SearchResult.new(results)
24
+ end
25
+
26
+ def curated(per_page: 15, page: 1)
27
+ results = @client.request(
28
+ '/v1/curated',
29
+ params: {
30
+ per_page: per_page,
31
+ page: page,
32
+ }
33
+ )
34
+
35
+ Pexels::SearchResult.new(results)
36
+ end
37
+ end
@@ -0,0 +1,36 @@
1
+ class Pexels::Client::Videos
2
+
3
+ def initialize(client)
4
+ @client = client
5
+ end
6
+
7
+ def get(id)
8
+ results = @client.request("/videos/videos/#{id}")
9
+ Pexels::Video.new(results)
10
+ end
11
+
12
+ def search(query, per_page: 15, page: 1)
13
+ results = @client.request(
14
+ '/videos/search',
15
+ params: {
16
+ query: query,
17
+ per_page: per_page,
18
+ page: page,
19
+ }
20
+ )
21
+
22
+ Pexels::SearchResult.new(results)
23
+ end
24
+
25
+ def popular(per_page: 15, page: 1)
26
+ results = @client.request(
27
+ '/videos/popular',
28
+ params: {
29
+ per_page: per_page,
30
+ page: page,
31
+ }
32
+ )
33
+
34
+ Pexels::SearchResult.new(results)
35
+ end
36
+ end
@@ -0,0 +1,39 @@
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)
35
+ end
36
+ end
37
+
38
+ require 'pexels/client/photos'
39
+ require 'pexels/client/videos'
@@ -0,0 +1,15 @@
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
@@ -0,0 +1,4 @@
1
+ module Pexels
2
+ class APIError < StandardError; end
3
+ class MalformedAPIResponseError < APIError; end
4
+ end
@@ -0,0 +1,25 @@
1
+ class Pexels::Photo
2
+ attr_reader :id,
3
+ :width,
4
+ :height,
5
+ :url,
6
+ :user,
7
+ :src
8
+
9
+
10
+ def initialize(attrs)
11
+ @id = attrs.fetch('id')
12
+ @height = attrs.fetch('height')
13
+ @width = attrs.fetch('width')
14
+ @url = attrs.fetch('url')
15
+ @user = Pexels::User.new(
16
+ id: attrs.fetch('photographer_id'),
17
+ name: attrs.fetch('photographer'),
18
+ url: attrs.fetch('photographer_url')
19
+ )
20
+ @src = attrs.fetch('src')
21
+
22
+ rescue KeyError => exception
23
+ raise Pexels::MalformedAPIResponseError.new(exception)
24
+ end
25
+ end
@@ -0,0 +1,20 @@
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
@@ -0,0 +1,9 @@
1
+ class Pexels::User
2
+ attr_reader :id, :name, :url
3
+
4
+ def initialize(id:, name:, url:)
5
+ @id = id
6
+ @name = name
7
+ @url = url
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Pexels
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,29 @@
1
+ class Pexels::Video
2
+ attr_reader :id,
3
+ :width,
4
+ :height,
5
+ :url,
6
+ :image,
7
+ :duration,
8
+ :user,
9
+ :video_files,
10
+ :video_pictures
11
+
12
+
13
+ def initialize(attrs)
14
+ @id = attrs.fetch('id')
15
+ @height = attrs.fetch('height')
16
+ @width = attrs.fetch('width')
17
+ @url = attrs.fetch('url')
18
+ @image = attrs.fetch('image')
19
+ @duration = attrs.fetch('duration')
20
+ @user = Pexels::User.new(
21
+ id: attrs.fetch('user').fetch('id'),
22
+ name: attrs.fetch('user').fetch('name'),
23
+ url: attrs.fetch('user').fetch('url')
24
+ )
25
+
26
+ rescue KeyError => exception
27
+ raise Pexels::MalformedAPIResponseError.new(exception)
28
+ end
29
+ end
data/lib/pexels.rb ADDED
@@ -0,0 +1,18 @@
1
+ module Pexels
2
+ @api_base_url = 'https://api.pexels.com'
3
+
4
+ class << self
5
+ attr_reader :api_base_url
6
+ end
7
+ end
8
+
9
+ require_relative 'pexels/client'
10
+ require_relative 'pexels/client/photos'
11
+ require_relative 'pexels/client/videos'
12
+ require_relative 'pexels/version'
13
+ require_relative 'pexels/errors'
14
+ require_relative 'pexels/photo'
15
+ require_relative 'pexels/video'
16
+ require_relative 'pexels/user'
17
+ require_relative 'pexels/search_result'
18
+ require_relative 'pexels/curated_result'
data/pexels.gemspec ADDED
@@ -0,0 +1,15 @@
1
+ require_relative "lib/pexels/version"
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'pexels'
5
+ s.version = Pexels::VERSION
6
+ s.summary = 'A simple Ruby wrapper for the Pexels API'
7
+ s.description = 'See more details at https://www.pexels.com/api/documentation/'
8
+ s.authors = ['Pexels dev team']
9
+ s.email = ['api@pexels.com']
10
+ s.homepage = 'https://github.com/pexels/pexels-ruby'
11
+ s.license = 'MIT'
12
+ s.files = `git ls-files`.split("\n")
13
+
14
+ s.add_dependency('requests', '~> 1.0.2')
15
+ end
@@ -0,0 +1,34 @@
1
+ require 'minitest/autorun'
2
+ require 'pexels'
3
+
4
+ class TestClient < Minitest::Test
5
+
6
+ def setup
7
+ @client = Pexels::Client.new(ENV.fetch('PEXELS_API_KEY'))
8
+ end
9
+
10
+ def test_ratelimit_remaining
11
+ @client.photos.search('test')
12
+
13
+ remaining = @client.ratelimit_remaining
14
+ assert remaining.is_a? Integer
15
+ assert remaining >= 0
16
+
17
+ @client.photos.search('test')
18
+ assert_equal remaining, @client.ratelimit_remaining + 1
19
+ end
20
+
21
+ def test_exceptions
22
+ fake_request = Requests::Response.new(200, {}, ';')
23
+
24
+ ::Requests.stub :request, fake_request do
25
+ begin
26
+ @client.photos.search('test')
27
+ raise 'this shouldnt happen'
28
+ rescue StandardError => exception
29
+ assert exception.is_a? Pexels::APIError
30
+ assert exception.message != 'this shouldnt happen'
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,70 @@
1
+ require 'minitest/autorun'
2
+ require 'pexels'
3
+
4
+ class TestPhoto < Minitest::Test
5
+
6
+ def setup
7
+ @client = Pexels::Client.new(ENV.fetch('PEXELS_API_KEY'))
8
+ @photo = @client.photos.search('test', per_page: 1).photos.first
9
+ end
10
+
11
+ def test_successful_searches
12
+ search_result = @client.photos.search('test')
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
17
+ assert_equal search_result.per_page, 15
18
+ assert_equal search_result.page, 1
19
+
20
+ assert search_result.photos.is_a? Array
21
+ assert search_result.photos.any?
22
+ assert search_result.photos.first.is_a? Pexels::Photo
23
+
24
+ search_result_with_params = @client.photos.search('test', per_page: 1, page: 2)
25
+ assert_equal search_result_with_params.per_page, 1
26
+ assert_equal search_result_with_params.page, 2
27
+ assert_equal search_result_with_params.photos.length, 1
28
+ end
29
+
30
+ def test_curated_photos
31
+ search_result = @client.photos.curated
32
+
33
+ assert search_result.is_a? Pexels::SearchResult
34
+ assert search_result.next_page.is_a? String
35
+ assert_equal search_result.per_page, 15
36
+ assert_equal search_result.page, 1
37
+
38
+ assert search_result.photos.is_a? Array
39
+ assert search_result.photos.any?
40
+ assert search_result.photos.first.is_a? Pexels::Photo
41
+
42
+ search_result_with_params = @client.photos.curated(per_page: 1, page: 2)
43
+ assert_equal search_result_with_params.per_page, 1
44
+ assert_equal search_result_with_params.page, 2
45
+ assert_equal search_result_with_params.photos.length, 1
46
+ end
47
+
48
+ def test_get_photo
49
+ photo = @client.photos.get(@photo.id)
50
+
51
+ assert photo.is_a? Pexels::Photo
52
+
53
+ assert_equal photo.id, @photo.id
54
+ assert_equal photo.width, @photo.width
55
+ assert_equal photo.height, @photo.height
56
+ assert_equal photo.url, @photo.url
57
+ assert_equal photo.user.name, @photo.user.name
58
+ assert_equal photo.user.url, @photo.user.url
59
+ assert_equal photo.user.id, @photo.user.id
60
+ assert_equal photo.src, @photo.src
61
+ end
62
+
63
+ def test_invalid_get_photo
64
+ photo = @client.photos.get('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'
69
+ end
70
+ end
@@ -0,0 +1,73 @@
1
+ require 'minitest/autorun'
2
+ require 'pexels'
3
+
4
+ class TestVideo < Minitest::Test
5
+
6
+ def setup
7
+ @client = Pexels::Client.new(ENV.fetch('PEXELS_API_KEY'))
8
+ @video = @client.videos.search('test', per_page: 1).videos.first
9
+ end
10
+
11
+ def test_successful_searches
12
+ search_result = @client.videos.search('test')
13
+
14
+ assert search_result.is_a? Pexels::SearchResult
15
+ assert search_result.total_results.is_a? Integer
16
+ assert_equal search_result.per_page, 15
17
+ assert_equal search_result.page, 1
18
+
19
+ assert search_result.videos.is_a? Array
20
+ assert search_result.videos.any?
21
+ assert search_result.videos.first.is_a? Pexels::Video
22
+
23
+ search_result_with_params = @client.videos.search('test', per_page: 1, page: 2)
24
+ assert_equal search_result_with_params.per_page, 1
25
+ assert_equal search_result_with_params.page, 2
26
+ assert_equal search_result_with_params.videos.length, 1
27
+ end
28
+
29
+ def test_popular_videos
30
+ search_result = @client.videos.popular
31
+
32
+ assert search_result.is_a? Pexels::SearchResult
33
+ assert_equal search_result.per_page, 15
34
+ assert_equal search_result.page, 1
35
+
36
+ assert search_result.videos.is_a? Array
37
+ assert search_result.videos.any?
38
+ assert search_result.videos.first.is_a? Pexels::Video
39
+
40
+ search_result_with_params = @client.videos.popular(per_page: 1, page: 2)
41
+ assert_equal search_result_with_params.per_page, 1
42
+ assert_equal search_result_with_params.page, 2
43
+ assert_equal search_result_with_params.videos.length, 1
44
+ end
45
+
46
+ def test_get_video
47
+ video = @client.videos.get(@video.id)
48
+
49
+ assert video.is_a? Pexels::Video
50
+
51
+ assert_equal video.id, @video.id
52
+ assert_equal video.width, @video.width
53
+ assert_equal video.height, @video.height
54
+ assert_equal video.url, @video.url
55
+
56
+ assert video.user.is_a?(Pexels::User)
57
+ assert_equal video.user.name, @video.user.name
58
+ assert_equal video.user.url, @video.user.url
59
+ assert_equal video.user.id, @video.user.id
60
+ end
61
+
62
+ def test_invalid_get_video
63
+ video = @client.videos.get('this-is-not-a-valid-id')
64
+ raise 'This should not happen'
65
+ rescue StandardError => exception
66
+ 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'
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pexels
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pexels dev team
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: requests
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.2
27
+ description: See more details at https://www.pexels.com/api/documentation/
28
+ email:
29
+ - api@pexels.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".env.sample"
35
+ - ".gems"
36
+ - ".gitignore"
37
+ - LICENSE
38
+ - Makefile
39
+ - README.md
40
+ - lib/pexels.rb
41
+ - lib/pexels/client.rb
42
+ - lib/pexels/client/photos.rb
43
+ - lib/pexels/client/videos.rb
44
+ - lib/pexels/curated_result.rb
45
+ - lib/pexels/errors.rb
46
+ - lib/pexels/photo.rb
47
+ - lib/pexels/search_result.rb
48
+ - lib/pexels/user.rb
49
+ - lib/pexels/version.rb
50
+ - lib/pexels/video.rb
51
+ - pexels.gemspec
52
+ - test/client_test.rb
53
+ - test/photo_test.rb
54
+ - test/video_test.rb
55
+ homepage: https://github.com/pexels/pexels-ruby
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.0.3
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: A simple Ruby wrapper for the Pexels API
78
+ test_files: []