pixabay_api 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.DS_Store +0 -0
- data/.rspec +3 -0
- data/.rubocop.yml +39 -0
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +82 -0
- data/LICENSE.txt +21 -0
- data/README.md +215 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/simple_request.rb +10 -0
- data/lib/.DS_Store +0 -0
- data/lib/pixabay_api.rb +27 -0
- data/lib/pixabay_api/.DS_Store +0 -0
- data/lib/pixabay_api/api_base.rb +45 -0
- data/lib/pixabay_api/configuration.rb +7 -0
- data/lib/pixabay_api/error.rb +25 -0
- data/lib/pixabay_api/images_api.rb +13 -0
- data/lib/pixabay_api/request.rb +30 -0
- data/lib/pixabay_api/response.rb +27 -0
- data/lib/pixabay_api/video_api.rb +11 -0
- data/lib/version.rb +5 -0
- data/pixabay_api.gemspec +28 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6a330512cd2bc8440f1b4a571d958efa61223607
|
4
|
+
data.tar.gz: 46a0644c2a888e6a3a210b2ec950001aca024d1a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c1923a5b0a38d3ba14c26e8dcae7a307a0e4dd463fb190a2d7bfdc0882537e126f97b48431b59f62fda01dab3da0bdd491bc1933080e042a398bc216cde63cd6
|
7
|
+
data.tar.gz: b2449cd30e461848bad3b14b462d56e23852f2923aa4341d6e39fc405765047031b560f21e9b0e99948d305c86d38f1e3f4b2a9179389d1ad7abff3eeb756518
|
data/.DS_Store
ADDED
Binary file
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
Exclude:
|
4
|
+
- 'examples/**/*'
|
5
|
+
- 'Gemfile'
|
6
|
+
- 'bin/*'
|
7
|
+
- '*.gemspec'
|
8
|
+
|
9
|
+
Rails:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
Documentation:
|
13
|
+
Enabled: false
|
14
|
+
Style/RaiseArgs:
|
15
|
+
EnforcedStyle: compact
|
16
|
+
# Maximum line length
|
17
|
+
LineLength:
|
18
|
+
Max: 100
|
19
|
+
|
20
|
+
# Use "postfix if/unless" if the lenght is less than this
|
21
|
+
IfUnlessModifier:
|
22
|
+
MaxLineLength: 80
|
23
|
+
|
24
|
+
# Maximum method length
|
25
|
+
MethodLength:
|
26
|
+
Max: 30
|
27
|
+
|
28
|
+
# Tune to MethodLength
|
29
|
+
Metrics/AbcSize:
|
30
|
+
Max: 30
|
31
|
+
|
32
|
+
# Tune to MethodLength
|
33
|
+
Metrics/ClassLength:
|
34
|
+
Max: 200
|
35
|
+
|
36
|
+
Metrics/BlockLength:
|
37
|
+
ExcludedMethods: ['describe', 'context', 'before', 'shared_context', 'let']
|
38
|
+
Exclude:
|
39
|
+
- "**/*_spec.rb"
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
pixabay_api (0.0.1)
|
5
|
+
http (~> 2.2)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.5.2)
|
11
|
+
public_suffix (>= 2.0.2, < 4.0)
|
12
|
+
ast (2.3.0)
|
13
|
+
coderay (1.1.2)
|
14
|
+
crack (0.4.3)
|
15
|
+
safe_yaml (~> 1.0.0)
|
16
|
+
diff-lcs (1.3)
|
17
|
+
domain_name (0.5.20170404)
|
18
|
+
unf (>= 0.0.5, < 1.0.0)
|
19
|
+
hashdiff (0.3.6)
|
20
|
+
http (2.2.2)
|
21
|
+
addressable (~> 2.3)
|
22
|
+
http-cookie (~> 1.0)
|
23
|
+
http-form_data (~> 1.0.1)
|
24
|
+
http_parser.rb (~> 0.6.0)
|
25
|
+
http-cookie (1.0.3)
|
26
|
+
domain_name (~> 0.5)
|
27
|
+
http-form_data (1.0.3)
|
28
|
+
http_parser.rb (0.6.0)
|
29
|
+
method_source (0.9.0)
|
30
|
+
parser (2.4.0.0)
|
31
|
+
ast (~> 2.2)
|
32
|
+
powerpack (0.1.1)
|
33
|
+
pry (0.11.1)
|
34
|
+
coderay (~> 1.1.0)
|
35
|
+
method_source (~> 0.9.0)
|
36
|
+
public_suffix (3.0.0)
|
37
|
+
rainbow (2.2.2)
|
38
|
+
rake
|
39
|
+
rake (12.1.0)
|
40
|
+
rspec (3.6.0)
|
41
|
+
rspec-core (~> 3.6.0)
|
42
|
+
rspec-expectations (~> 3.6.0)
|
43
|
+
rspec-mocks (~> 3.6.0)
|
44
|
+
rspec-core (3.6.0)
|
45
|
+
rspec-support (~> 3.6.0)
|
46
|
+
rspec-expectations (3.6.0)
|
47
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
48
|
+
rspec-support (~> 3.6.0)
|
49
|
+
rspec-mocks (3.6.0)
|
50
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
51
|
+
rspec-support (~> 3.6.0)
|
52
|
+
rspec-support (3.6.0)
|
53
|
+
rubocop (0.47.1)
|
54
|
+
parser (>= 2.3.3.1, < 3.0)
|
55
|
+
powerpack (~> 0.1)
|
56
|
+
rainbow (>= 1.99.1, < 3.0)
|
57
|
+
ruby-progressbar (~> 1.7)
|
58
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
59
|
+
ruby-progressbar (1.9.0)
|
60
|
+
safe_yaml (1.0.4)
|
61
|
+
unf (0.1.4)
|
62
|
+
unf_ext
|
63
|
+
unf_ext (0.0.7.4)
|
64
|
+
unicode-display_width (1.3.0)
|
65
|
+
webmock (3.0.1)
|
66
|
+
addressable (>= 2.3.6)
|
67
|
+
crack (>= 0.3.2)
|
68
|
+
hashdiff
|
69
|
+
|
70
|
+
PLATFORMS
|
71
|
+
ruby
|
72
|
+
|
73
|
+
DEPENDENCIES
|
74
|
+
bundler (~> 1.15)
|
75
|
+
pixabay_api!
|
76
|
+
pry (~> 0.11.1)
|
77
|
+
rspec (~> 3.0)
|
78
|
+
rubocop (~> 0.47.1)
|
79
|
+
webmock (~> 3.0)
|
80
|
+
|
81
|
+
BUNDLED WITH
|
82
|
+
1.15.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 ERIC DUC LINH
|
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/README.md
ADDED
@@ -0,0 +1,215 @@
|
|
1
|
+
# Pixabay_api
|
2
|
+
[![Code Climate](https://codeclimate.com/github/haanhduclinh/s3-to-ftp.png)](https://codeclimate.com/github/haanhduclinh/s3-to-ftp) [![CI](https://travis-ci.org/haanhduclinh/pixabay_api.svg?branch=master)](https://travis-ci.org/haanhduclinh/pixabay_api)
|
3
|
+
|
4
|
+
# Background
|
5
|
+
- Use PixaBayApi to get image data from pixabay.com follow API - https://pixabay.com/api/docs/.
|
6
|
+
- Sign up to get free API - https://pixabay.com/en/accounts/register/
|
7
|
+
|
8
|
+
# Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'pixabay_api'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install pixabay_api
|
23
|
+
|
24
|
+
## Configuring PixabayApi
|
25
|
+
|
26
|
+
If you're using Rails, create an initializer for this:
|
27
|
+
|
28
|
+
```
|
29
|
+
config/initializers/pixabay_api.rb
|
30
|
+
```
|
31
|
+
|
32
|
+
You can configure as below:
|
33
|
+
|
34
|
+
```
|
35
|
+
PixabayApi.configure do |c|
|
36
|
+
c.api_key = YOUR_API_KEY
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
# Usage
|
41
|
+
|
42
|
+
## get pictures data
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
images_api = PixabayApi::ImagesApi.new
|
46
|
+
pictures = images_api.find(keyword: 'test')
|
47
|
+
|
48
|
+
p pictures.success?
|
49
|
+
p pictures.body['totalHits']
|
50
|
+
p pictures.body
|
51
|
+
```
|
52
|
+
|
53
|
+
samples return
|
54
|
+
|
55
|
+
```
|
56
|
+
# true
|
57
|
+
# 500
|
58
|
+
{
|
59
|
+
'totalHits' => 500,
|
60
|
+
'hits' =>
|
61
|
+
[
|
62
|
+
{
|
63
|
+
'previewHeight' => 84,
|
64
|
+
'likes' => 23,
|
65
|
+
'favorites' => 6,
|
66
|
+
'tags' => 'bread, ham, tight max',
|
67
|
+
'webformatHeight' => 360,
|
68
|
+
'views' => 772,
|
69
|
+
'webformatWidth' => 640,
|
70
|
+
'previewWidth' => 150,
|
71
|
+
'comments' => 24,
|
72
|
+
'downloads' => 494,
|
73
|
+
'pageURL' => 'https://pixabay.com/en/bread-ham-tight-max-egg-fried-2796393/',
|
74
|
+
'previewURL' => 'https://cdn.pixabay.com/photo/2017/09/28/18/13/bread-2796393_150.jpg',
|
75
|
+
'webformatURL' => 'https://pixabay.com/get/eb32b8092bfd023ed95c4518b7484595ea7fe3d204b0144192f0c879a2efb1_640.jpg',
|
76
|
+
'imageWidth' => 5472,
|
77
|
+
'user_id' => 19_628,
|
78
|
+
'user' => 'RitaE',
|
79
|
+
'type' => 'photo',
|
80
|
+
'id' => 2_796_393,
|
81
|
+
'userImageURL' => 'https://cdn.pixabay.com/user/2017/06/28/22-56-11-59_250x250.jpg',
|
82
|
+
'imageHeight' => 3_080
|
83
|
+
}
|
84
|
+
]
|
85
|
+
}
|
86
|
+
```
|
87
|
+
|
88
|
+
## Get picture array data
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
images_api = PixabayApi::ImagesApi.new
|
92
|
+
pictures = images_api.find_and_return_array(keyword: 'test')
|
93
|
+
|
94
|
+
p pictures.success?
|
95
|
+
p pictures.body['totalHits']
|
96
|
+
p pictures.body
|
97
|
+
```
|
98
|
+
|
99
|
+
It will be return picture array
|
100
|
+
|
101
|
+
|
102
|
+
```
|
103
|
+
[
|
104
|
+
{
|
105
|
+
'previewHeight' => 84,
|
106
|
+
'likes' => 23,
|
107
|
+
'favorites' => 6,
|
108
|
+
'tags' => 'bread, ham, tight max',
|
109
|
+
'webformatHeight' => 360,
|
110
|
+
'views' => 772,
|
111
|
+
'webformatWidth' => 640,
|
112
|
+
'previewWidth' => 150,
|
113
|
+
'comments' => 24,
|
114
|
+
'downloads' => 494,
|
115
|
+
'pageURL' => 'https://pixabay.com/en/bread-ham-tight-max-egg-fried-2796393/',
|
116
|
+
'previewURL' => 'https://cdn.pixabay.com/photo/2017/09/28/18/13/bread-2796393_150.jpg',
|
117
|
+
'webformatURL' => 'https://pixabay.com/get/eb32b8092bfd023ed95c4518b7484595ea7fe3d204b0144192f0c879a2efb1_640.jpg',
|
118
|
+
'imageWidth' => 5472,
|
119
|
+
'user_id' => 19_628,
|
120
|
+
'user' => 'RitaE',
|
121
|
+
'type' => 'photo',
|
122
|
+
'id' => 2_796_393,
|
123
|
+
'userImageURL' => 'https://cdn.pixabay.com/user/2017/06/28/22-56-11-59_250x250.jpg',
|
124
|
+
'imageHeight' => 3_080
|
125
|
+
},
|
126
|
+
...
|
127
|
+
]
|
128
|
+
|
129
|
+
```
|
130
|
+
|
131
|
+
## Get video data and return array_data
|
132
|
+
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
video_api = PixabayApi::VideoApi.new
|
136
|
+
videos = video_api.find(keyword: 'test')
|
137
|
+
|
138
|
+
p pictures.success?
|
139
|
+
p pictures.body['totalHits']
|
140
|
+
p pictures.body
|
141
|
+
```
|
142
|
+
|
143
|
+
## Options
|
144
|
+
|
145
|
+
### Search Images
|
146
|
+
|
147
|
+
|fields|type|describle
|
148
|
+
|---|---|---|
|
149
|
+
|key (required)|str|Your API key|
|
150
|
+
|q|str|A URL encoded search term. If omitted, all images are returned. This value may not exceed 100 characters. Example: "yellow+flower"|
|
151
|
+
|lang|str|Language code of the language to be searched in. Accepted values: cs, da, de, en, es, fr, id, it, hu, nl, no, pl, pt, ro, sk, fi, sv, tr, vi, th, bg, ru, el, ja, ko, zh Default: "en"|
|
152
|
+
|id|str|ID, hash ID, or a comma separated list of values for retrieving specific images. In a comma separated list, IDs and hash IDs cannot be used together.|
|
153
|
+
|response_group|str|Choose between retrieving high resolution images and image details. When selecting details, you can access images up to a dimension of 960 x 720 px. Accepted values: "image_details", "high_resolution" (requires permission) Default: "image_details"|
|
154
|
+
|image_type|str|Filter results by image type. Accepted values: "all", "photo", "illustration", "vector" Default: "all"|
|
155
|
+
|orientation|str|Whether an image is wider than it is tall, or taller than it is wide. Accepted values: "all", "horizontal", "vertical" Default: "all"|
|
156
|
+
|category|str|Filter results by category. Accepted values: fashion, nature, backgrounds, science, education, people, feelings, religion, health, places, animals, industry, food, computer, sports, transportation, travel, buildings, business, music|
|
157
|
+
|min_width|int|Minimum image width. Default: "0"|
|
158
|
+
|min_height|int|Minimum image height. Default: "0"|
|
159
|
+
|editors_choice|bool|Select images that have received an Editor's Choice award. Accepted values: "true", "false" Default: "false"|
|
160
|
+
|safesearch| bool| A flag indicating that only images suitable for all ages should be returned. Accepted values: "true", "false" Default: "false"|
|
161
|
+
|order| str| How the results should be ordered. Accepted values: "popular", "latest" Default: "popular"|
|
162
|
+
|page| int| Returned search results are paginated. Use this parameter to select the page number. Default: 1|
|
163
|
+
|per_page| int| Determine the number of results per page. Accepted values: 3 - 200 Default: 20|
|
164
|
+
|callback| string| JSONP callback function name|
|
165
|
+
|pretty| bool| Indent JSON output. This option should not be used in production. Accepted values: "true", "false" Default: "false"|
|
166
|
+
|
167
|
+
### Search Videos
|
168
|
+
|
169
|
+
|fields|type|describle
|
170
|
+
|---|---|---|
|
171
|
+
|key (required)|str|Your API key|
|
172
|
+
|q|str|A URL encoded search term. If omitted, all images are returned. This value may not exceed 100 characters. Example: "yellow+flower"|
|
173
|
+
|lang|str|Language code of the language to be searched in. Accepted values: cs, da, de, en, es, fr, id, it, hu, nl, no, pl, pt, ro, sk, fi, sv, tr, vi, th, bg, ru, el, ja, ko, zh Default: "en"|
|
174
|
+
|id|str|ID, hash ID, or a comma separated list of values for retrieving specific images. In a comma separated list, IDs and hash IDs cannot be used together.|
|
175
|
+
|response_group|str|Choose between retrieving high resolution images and image details. When selecting details, you can access images up to a dimension of 960 x 720 px. Accepted values: "image_details", "high_resolution" (requires permission) Default: "image_details"|
|
176
|
+
|video_type|str|Filter results by video type. Accepted values: "all", "film", "animation" Default: "all"|
|
177
|
+
|orientation|str|Whether an image is wider than it is tall, or taller than it is wide. Accepted values: "all", "horizontal", "vertical" Default: "all"|
|
178
|
+
|category|str|Filter results by category. Accepted values: fashion, nature, backgrounds, science, education, people, feelings, religion, health, places, animals, industry, food, computer, sports, transportation, travel, buildings, business, music|
|
179
|
+
|min_width|int|Minimum image width. Default: "0"|
|
180
|
+
|min_height|int|Minimum image height. Default: "0"|
|
181
|
+
|editors_choice|bool|Select images that have received an Editor's Choice award. Accepted values: "true", "false" Default: "false"|
|
182
|
+
|safesearch| bool| A flag indicating that only images suitable for all ages should be returned. Accepted values: "true", "false" Default: "false"|
|
183
|
+
|order| str| How the results should be ordered. Accepted values: "popular", "latest" Default: "popular"|
|
184
|
+
|page| int| Returned search results are paginated. Use this parameter to select the page number. Default: 1|
|
185
|
+
|per_page| int| Determine the number of results per page. Accepted values: 3 - 200 Default: 20|
|
186
|
+
|callback| string| JSONP callback function name|
|
187
|
+
|pretty| bool| Indent JSON output. This option should not be used in production. Accepted values: "true", "false" Default: "false"|
|
188
|
+
|
189
|
+
|
190
|
+
- Get more detail - https://pixabay.com/api/docs/
|
191
|
+
|
192
|
+
# Rate Limit
|
193
|
+
|
194
|
+
By default, you can make up to 5,000 requests per hour. Requests are associated with the API key, and not with your IP address. The response headers tell you everything you need to know about your current rate limit status:
|
195
|
+
|
196
|
+
|
197
|
+
|Header name|Description|
|
198
|
+
|---|---|
|
199
|
+
|X-RateLimit-Limit|The maximum number of requests that the consumer is permitted to make in 30 minutes.|
|
200
|
+
|X-RateLimit-Remaining|The number of requests remaining in the current rate limit window.|
|
201
|
+
|X-RateLimit-Reset|The remaining time in seconds after which the current rate limit window resets.|
|
202
|
+
|
203
|
+
To keep the Pixabay API fast for everyone, requests must be cached for 24 hours. Also, the API is made for real human requests; do not send lots of automated queries. Systematic mass downloads are not allowed. If needed, we can increase this limit at any time - given that you've implemented the API properly.
|
204
|
+
|
205
|
+
# How to contributor
|
206
|
+
- Folk project
|
207
|
+
- Create pull request. Please remember add `rspec` and make sure pass all of test.
|
208
|
+
|
209
|
+
# Run test
|
210
|
+
- `bundle exec rspec`
|
211
|
+
|
212
|
+
|
213
|
+
# Author
|
214
|
+
|
215
|
+
haanhduclinh@yahoo.com | www.haanhduclinh.com
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'pixabay_api'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require 'pry'
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/.DS_Store
ADDED
Binary file
|
data/lib/pixabay_api.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'resolv-replace'
|
4
|
+
require 'net/http'
|
5
|
+
require 'uri'
|
6
|
+
require 'cgi'
|
7
|
+
require 'json'
|
8
|
+
|
9
|
+
require 'pixabay_api/configuration'
|
10
|
+
require 'pixabay_api/error'
|
11
|
+
require 'pixabay_api/api_base'
|
12
|
+
require 'pixabay_api/images_api'
|
13
|
+
require 'pixabay_api/video_api'
|
14
|
+
|
15
|
+
module PixabayApi
|
16
|
+
class << self
|
17
|
+
attr_writer :configuration
|
18
|
+
|
19
|
+
def configure
|
20
|
+
yield(configuration) if block_given?
|
21
|
+
end
|
22
|
+
|
23
|
+
def configuration
|
24
|
+
@configuration ||= Configuration.new
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
Binary file
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative 'error'
|
3
|
+
require_relative 'response'
|
4
|
+
require_relative 'request'
|
5
|
+
require_relative 'configuration'
|
6
|
+
require 'pixabay_api'
|
7
|
+
|
8
|
+
module PixabayApi
|
9
|
+
class ApiBase
|
10
|
+
attr_accessor :params
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@api = PixabayApi.configuration.api_key
|
14
|
+
end
|
15
|
+
|
16
|
+
def find(keyword:, options: {})
|
17
|
+
self.params = { q: keyword }.merge(options)
|
18
|
+
response_raw = PixabayApi::Request.create(
|
19
|
+
api_key: @api,
|
20
|
+
params: params,
|
21
|
+
endpoint: endpoint
|
22
|
+
)
|
23
|
+
response = PixabayApi::Response.new(response_raw)
|
24
|
+
|
25
|
+
if response.failed?
|
26
|
+
raise PixabayApi::Error::RequestError.new(response.body)
|
27
|
+
end
|
28
|
+
|
29
|
+
response
|
30
|
+
end
|
31
|
+
|
32
|
+
def find_and_return_array(keyword:, options: {})
|
33
|
+
response = find(keyword: keyword, options: options)
|
34
|
+
|
35
|
+
return [] if response.body['totalHits'].to_i.zero?
|
36
|
+
response.body['hits']
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def endpoint
|
42
|
+
'https://pixabay.com/api/'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
module PixabayApi
|
5
|
+
module Error
|
6
|
+
class PixabayError < StandardError
|
7
|
+
end
|
8
|
+
|
9
|
+
# RequestError is raised when request to Pixabay server is invalid
|
10
|
+
class RequestError < PixabayError
|
11
|
+
end
|
12
|
+
|
13
|
+
# RequestError is raised when request to Pixabay server is timeout
|
14
|
+
class RequestTimeOut < PixabayError
|
15
|
+
end
|
16
|
+
|
17
|
+
# RequestError is raised when request is failed due to NG data
|
18
|
+
class NgError < PixabayError
|
19
|
+
end
|
20
|
+
|
21
|
+
# RequestError is raised when request is failed due to Pixabay system error
|
22
|
+
class SystemError < PixabayError
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'resolv-replace'
|
4
|
+
require 'cgi'
|
5
|
+
|
6
|
+
module PixabayApi
|
7
|
+
class Request
|
8
|
+
class << self
|
9
|
+
def create(api_key:, params: {}, endpoint: 'xxx')
|
10
|
+
query = build_query(api_key, params, endpoint)
|
11
|
+
uri = URI(query)
|
12
|
+
|
13
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
|
14
|
+
request = Net::HTTP::Get.new uri
|
15
|
+
http.request(request)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def build_query(api_key, params, endpoint)
|
22
|
+
"#{endpoint}?key=#{api_key}&#{params_to_s(params)}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def params_to_s(params)
|
26
|
+
CGI.escape(params.map { |k, v| "#{k}=#{v}" }.join('&'))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PixabayApi
|
4
|
+
class Response
|
5
|
+
attr_accessor :status_code, :status, :body, :response
|
6
|
+
|
7
|
+
def initialize(response)
|
8
|
+
self.response = response
|
9
|
+
end
|
10
|
+
|
11
|
+
def success?
|
12
|
+
response.code == 200
|
13
|
+
end
|
14
|
+
|
15
|
+
def failed?
|
16
|
+
response.code.to_i != 200
|
17
|
+
end
|
18
|
+
|
19
|
+
def status_code
|
20
|
+
self.status_code = response.code.to_i
|
21
|
+
end
|
22
|
+
|
23
|
+
def body
|
24
|
+
self.body = JSON.parse(response.body)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/version.rb
ADDED
data/pixabay_api.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# coding: utf-8
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |s|
|
9
|
+
s.name = 'pixabay_api'
|
10
|
+
s.version = PixabayApi::VERSION
|
11
|
+
s.date = '2017-09-29'
|
12
|
+
s.summary = 'awesome_gem is the best'
|
13
|
+
s.description = 'API get free picture from pixabay'
|
14
|
+
s.authors = ['Eric Duc Linh']
|
15
|
+
s.email = 'haanhduclinh@yahoo.com'
|
16
|
+
s.homepage = 'https://github.com/haanhduclinh/pixabay_api'
|
17
|
+
s.licenses = 'MIT'
|
18
|
+
s.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
s.require_paths = ['lib']
|
22
|
+
s.add_development_dependency 'bundler', '~> 1.15'
|
23
|
+
s.add_development_dependency 'webmock', '~> 3.0'
|
24
|
+
s.add_development_dependency 'rspec', '~> 3.0'
|
25
|
+
s.add_development_dependency 'rubocop', '~> 0.47.1'
|
26
|
+
s.add_development_dependency 'pry', '~> 0.11.1'
|
27
|
+
s.add_dependency 'http', '~> 2.2'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pixabay_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eric Duc Linh
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: webmock
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.47.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.47.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.11.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.11.1
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: http
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.2'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.2'
|
97
|
+
description: API get free picture from pixabay
|
98
|
+
email: haanhduclinh@yahoo.com
|
99
|
+
executables: []
|
100
|
+
extensions: []
|
101
|
+
extra_rdoc_files: []
|
102
|
+
files:
|
103
|
+
- ".DS_Store"
|
104
|
+
- ".rspec"
|
105
|
+
- ".rubocop.yml"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- Gemfile.lock
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- examples/simple_request.rb
|
114
|
+
- lib/.DS_Store
|
115
|
+
- lib/pixabay_api.rb
|
116
|
+
- lib/pixabay_api/.DS_Store
|
117
|
+
- lib/pixabay_api/api_base.rb
|
118
|
+
- lib/pixabay_api/configuration.rb
|
119
|
+
- lib/pixabay_api/error.rb
|
120
|
+
- lib/pixabay_api/images_api.rb
|
121
|
+
- lib/pixabay_api/request.rb
|
122
|
+
- lib/pixabay_api/response.rb
|
123
|
+
- lib/pixabay_api/video_api.rb
|
124
|
+
- lib/version.rb
|
125
|
+
- pixabay_api.gemspec
|
126
|
+
homepage: https://github.com/haanhduclinh/pixabay_api
|
127
|
+
licenses:
|
128
|
+
- MIT
|
129
|
+
metadata: {}
|
130
|
+
post_install_message:
|
131
|
+
rdoc_options: []
|
132
|
+
require_paths:
|
133
|
+
- lib
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
requirements: []
|
145
|
+
rubyforge_project:
|
146
|
+
rubygems_version: 2.6.13
|
147
|
+
signing_key:
|
148
|
+
specification_version: 4
|
149
|
+
summary: awesome_gem is the best
|
150
|
+
test_files: []
|