unsplash_image 0.1.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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +97 -0
- data/Rakefile +3 -0
- data/bin/unsplash_image +4 -0
- data/lib/tasks/unsplash_image_tasks.rake +4 -0
- data/lib/unsplash_image/cli.rb +89 -0
- data/lib/unsplash_image/download.rb +26 -0
- data/lib/unsplash_image/helper.rb +16 -0
- data/lib/unsplash_image/railtie.rb +9 -0
- data/lib/unsplash_image/version.rb +3 -0
- data/lib/unsplash_image.rb +7 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 27977f3506d7d69b51a21fe2d1cc0a521fc5fa5d6b90ffa37bbdda9d001e8774
|
4
|
+
data.tar.gz: dae359ff65d6f3edec2534581a1db3f97a2f951f9101359fec2a4066d19f1c95
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: adddf72e64434d5825eeec312dcbcf6e21131420b348ae36f026d3b2ba054d160d834640e6c67bceb05f993074183fef2b524fb77a796ef23fedc03854104292
|
7
|
+
data.tar.gz: 60a6289114199d9f7eb89d25866500f328307fb590162f82afba1f9aa528708f95563a3d1d0631cc657c6b1a7b999fa8149b747e6ba61bd8ad9839970629d1cb
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2022
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
# Unsplash Image Downloader & Helpers
|
2
|
+
|
3
|
+
[](https://www.railsjazz.com)
|
4
|
+
|
5
|
+
A CLI and a set of Rails helpers to get free images from [Unsplash](https://unsplash.com/).
|
6
|
+
|
7
|
+
This is the easiest way to fill your Rails app with real photos.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
1. as Rails helper to generate dummy images:
|
12
|
+
```ruby
|
13
|
+
<%= image_tag unsplash_image_url(size: '300x200', tags: 'cat, dog') %>
|
14
|
+
|
15
|
+
<%= image_tag unsplash_image_url(size: '800x600', tags: 'building') %>
|
16
|
+
|
17
|
+
<%= image_tag unsplash_image_url(tags: 'nature') %>
|
18
|
+
```
|
19
|
+
|
20
|
+
2. as a tool to download images from Unsplash.com and use them for your seeds or specs.
|
21
|
+
|
22
|
+
```bash
|
23
|
+
unsplash_image download --path images/cats --tags cat -n 20
|
24
|
+
```
|
25
|
+
|
26
|
+
3. If you need to have a `File` object with a random image.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
file = UnsplashImage.tempfile(size: '500x500', tags: 'cat')
|
30
|
+
```
|
31
|
+
|
32
|
+
### CLI
|
33
|
+
|
34
|
+
By default `unsplash_image download --path ./files` will download 10 random images into a `./files` folder.
|
35
|
+
|
36
|
+
You can see list of all available options by running:
|
37
|
+
|
38
|
+
|
39
|
+
```bash
|
40
|
+
unsplash_image --help download
|
41
|
+
```
|
42
|
+
|
43
|
+
With additional options you can specify a destination folder, tags, resolution of the images.
|
44
|
+
|
45
|
+
|
46
|
+
### In your Rails app
|
47
|
+
|
48
|
+
You can get random image url inside your views using `unsplash_image_url`.
|
49
|
+
|
50
|
+
Example:
|
51
|
+
```erb
|
52
|
+
<%= image_tag unsplash_image_url(size: '300x200', tags: 'cat, dog') %>
|
53
|
+
```
|
54
|
+
|
55
|
+
Also you can get it as a file with `UnsplashImage.tempfile`.
|
56
|
+
|
57
|
+
Example:
|
58
|
+
```ruby
|
59
|
+
file = UnsplashImage.tempfile(size: '500x500', tags: 'cat')
|
60
|
+
```
|
61
|
+
|
62
|
+
## Installation
|
63
|
+
|
64
|
+
Add this line to your application's Gemfile:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
gem "unsplash_image"
|
68
|
+
```
|
69
|
+
|
70
|
+
And then execute:
|
71
|
+
```bash
|
72
|
+
$ bundle
|
73
|
+
```
|
74
|
+
|
75
|
+
To use CLI anywhere in the system you can install gem globally:
|
76
|
+
|
77
|
+
```bash
|
78
|
+
gem install unsplash_image
|
79
|
+
```
|
80
|
+
|
81
|
+
## Contributing
|
82
|
+
|
83
|
+
You are welcome to contribute. See list of `TODO's` below.
|
84
|
+
|
85
|
+
## TODO
|
86
|
+
|
87
|
+
- allow caching downloaded files for performance improvement (eg using in factories)
|
88
|
+
- check with older Rails versions
|
89
|
+
- tests or specs
|
90
|
+
- make stand-alone executable installable via `brew`, `apt get`, etc?
|
91
|
+
|
92
|
+
## License
|
93
|
+
|
94
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
95
|
+
|
96
|
+
[<img src="https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/more_gems.png?raw=true"
|
97
|
+
/>](https://www.railsjazz.com/?utm_source=github&utm_medium=bottom&utm_campaign=unsplash_image)
|
data/Rakefile
ADDED
data/bin/unsplash_image
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require "thor"
|
2
|
+
|
3
|
+
require "unsplash_image"
|
4
|
+
require "pathname"
|
5
|
+
|
6
|
+
module UnsplashImage
|
7
|
+
class CLI < Thor
|
8
|
+
package_name "UnsplashImage"
|
9
|
+
|
10
|
+
map "-d" => :download
|
11
|
+
map "--download" => :download
|
12
|
+
|
13
|
+
default_task :info
|
14
|
+
|
15
|
+
desc :info, "Print info about cli"
|
16
|
+
|
17
|
+
method_option :version, type: :boolean, default: false, aliases: [:v], banner: "Print UnsplashImage version"
|
18
|
+
def info
|
19
|
+
if options[:version]
|
20
|
+
puts "UnsplashImage #{UnsplashImage::VERSION}"
|
21
|
+
else
|
22
|
+
info = [
|
23
|
+
"UnsplashImage #{UnsplashImage::VERSION}",
|
24
|
+
"",
|
25
|
+
"Usage example: ",
|
26
|
+
" unsplash_image download --path spec/files -n 10",
|
27
|
+
" unsplash_image download --path images/cats -s 400x400 --tags cat -n 20",
|
28
|
+
" unsplash_image download --path files/ -s 300x300 --tags cat dogs birds -n 20",
|
29
|
+
"",
|
30
|
+
"run 'unsplash_image download' to see awailable options"
|
31
|
+
]
|
32
|
+
puts info.join("\n")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "download [OPTIONS]", "Download Unsplash images"
|
37
|
+
|
38
|
+
long_desc <<-LONGDESC
|
39
|
+
`unsplash_image download` will download random unsplash images on your PC
|
40
|
+
|
41
|
+
Example:
|
42
|
+
|
43
|
+
> $ unsplash_image download --path images/cats -s 400x400 --tags cat -n 20
|
44
|
+
LONGDESC
|
45
|
+
|
46
|
+
DEFAULT_COUNT = 10
|
47
|
+
DEFAULT_PATH = "."
|
48
|
+
|
49
|
+
method_option :size, type: :string, aliases: [:s], banner: "Specify image size. Example: -s 640x480"
|
50
|
+
method_option :count, type: :numeric, aliases: [:n], banner: "Specify images count. Example: -n 10"
|
51
|
+
method_option :path, type: :string, banner: "Specify folder. Example: --path images/cats"
|
52
|
+
method_option :tags, type: :array, aliases: [:t], banner: "Specify tags. Example: -t cats"
|
53
|
+
method_option :prefix, type: :string, banner: "Specify file name prefix. Example: --prefix cat_image"
|
54
|
+
def download
|
55
|
+
if options.keys.empty?
|
56
|
+
invoke :help, ["download"]
|
57
|
+
else
|
58
|
+
prefix = if !!options[:prefix]
|
59
|
+
options[:prefix]
|
60
|
+
else
|
61
|
+
!(options[:tags].nil? || options[:tags].empty?) ? options[:tags].join("_") : "image"
|
62
|
+
end
|
63
|
+
tags = options[:tags]&.join(" ").to_s
|
64
|
+
|
65
|
+
base_path = Pathname.new(options[:path] || DEFAULT_PATH)
|
66
|
+
FileUtils.mkdir_p(base_path)
|
67
|
+
|
68
|
+
puts "Downloading images to #{base_path.absolute? ? base_path : base_path.realpath.relative_path_from(Dir.pwd)}"
|
69
|
+
count = (options[:count] || DEFAULT_COUNT).to_i
|
70
|
+
count.times do |i|
|
71
|
+
filename = count == 1 ? "#{prefix}.jpeg" : "#{prefix}_#{i + 1}.jpeg"
|
72
|
+
filename = filename.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "�").strip.tr("\u{202E}%$|:;/\t\r\n\\", "-")
|
73
|
+
path = File.expand_path(filename, base_path)
|
74
|
+
puts "Downloading #{filename}"
|
75
|
+
UnsplashImage.tempfile(size: options[:size], filename: filename, tags: tags + (" " * i)) do |tempfile|
|
76
|
+
File.write(path, tempfile.read)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
puts "Done!"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class << self
|
84
|
+
def exit_on_failure?
|
85
|
+
true
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "open-uri"
|
2
|
+
require "tempfile"
|
3
|
+
|
4
|
+
module UnsplashImage
|
5
|
+
module Download
|
6
|
+
def tempfile(size: nil, filename: "image.jpeg", tags: nil)
|
7
|
+
file = Tempfile.new(filename)
|
8
|
+
begin
|
9
|
+
file.write(URI.parse(UnsplashImage::Helper.unsplash_image_url(size: size, tags: tags)).read)
|
10
|
+
file.rewind
|
11
|
+
if block_given?
|
12
|
+
yield file
|
13
|
+
else
|
14
|
+
file
|
15
|
+
end
|
16
|
+
ensure
|
17
|
+
if block_given?
|
18
|
+
file.close
|
19
|
+
file.unlink
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
extend Download
|
26
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module UnsplashImage
|
2
|
+
module Helper
|
3
|
+
extend self
|
4
|
+
|
5
|
+
BASE = "https://source.unsplash.com/random".freeze
|
6
|
+
|
7
|
+
def unsplash_image_url(size:, tags: nil)
|
8
|
+
options = [BASE]
|
9
|
+
|
10
|
+
options << size
|
11
|
+
options << "?#{tags}" if tags
|
12
|
+
|
13
|
+
options.join("/")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unsplash_image
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Igor Kasyanchuk
|
8
|
+
- Liubomyr Manastyretskyi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2022-06-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: thor
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0.20'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0.20'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rails
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: faker
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: pry
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: puma
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
description: Show the random images in your Rails app or download them to use for
|
85
|
+
seeding a DB
|
86
|
+
email:
|
87
|
+
- igorkasyanchuk@gmail.com
|
88
|
+
- manastyretskyi@gmail.com
|
89
|
+
executables:
|
90
|
+
- unsplash_image
|
91
|
+
extensions: []
|
92
|
+
extra_rdoc_files: []
|
93
|
+
files:
|
94
|
+
- MIT-LICENSE
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/unsplash_image
|
98
|
+
- lib/tasks/unsplash_image_tasks.rake
|
99
|
+
- lib/unsplash_image.rb
|
100
|
+
- lib/unsplash_image/cli.rb
|
101
|
+
- lib/unsplash_image/download.rb
|
102
|
+
- lib/unsplash_image/helper.rb
|
103
|
+
- lib/unsplash_image/railtie.rb
|
104
|
+
- lib/unsplash_image/version.rb
|
105
|
+
homepage: https://github.com/railsjazz/unsplash_image
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubygems_version: 3.2.3
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Show the random images in your Rails app or download them to use for seeding
|
128
|
+
a DB
|
129
|
+
test_files: []
|