remove_bg 1.5.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +298 -59
- data/.depfu.yml +21 -0
- data/.editorconfig +19 -0
- data/.gitignore +8 -8
- data/.grenrc.json +20 -0
- data/.rspec +5 -0
- data/.rubocop.yml +43 -0
- data/Appraisals +49 -10
- data/CHANGELOG.md +6 -1
- data/Gemfile +2 -5
- data/Gemfile.lock +130 -78
- data/LICENSE.md +25 -0
- data/README.md +31 -12
- data/Rakefile +4 -0
- data/SECURITY.md +36 -0
- data/gemfiles/faraday_2_0.gemfile +7 -0
- data/gemfiles/faraday_2_1.gemfile +7 -0
- data/gemfiles/faraday_2_2.gemfile +7 -0
- data/gemfiles/faraday_2_3.gemfile +7 -0
- data/gemfiles/faraday_2_4.gemfile +7 -0
- data/gemfiles/faraday_2_5.gemfile +7 -0
- data/gemfiles/faraday_2_6.gemfile +7 -0
- data/gemfiles/faraday_2_7.gemfile +7 -0
- data/gemfiles/faraday_2_8.gemfile +7 -0
- data/gemfiles/faraday_2_9.gemfile +7 -0
- data/gemfiles/faraday_2_x.gemfile +7 -0
- data/lib/remove_bg/account_info.rb +2 -0
- data/lib/remove_bg/api.rb +2 -0
- data/lib/remove_bg/api_client.rb +12 -12
- data/lib/remove_bg/base_request_options.rb +2 -0
- data/lib/remove_bg/composite_result.rb +19 -10
- data/lib/remove_bg/configuration.rb +2 -0
- data/lib/remove_bg/error.rb +3 -1
- data/lib/remove_bg/http_connection.rb +7 -4
- data/lib/remove_bg/image_composer.rb +13 -12
- data/lib/remove_bg/rate_limit_info.rb +9 -6
- data/lib/remove_bg/request_options.rb +3 -3
- data/lib/remove_bg/result.rb +13 -3
- data/lib/remove_bg/result_metadata.rb +2 -0
- data/lib/remove_bg/upload.rb +7 -5
- data/lib/remove_bg/url_validator.rb +4 -4
- data/lib/remove_bg/version.rb +3 -1
- data/lib/remove_bg.rb +2 -0
- data/remove_bg.gemspec +43 -20
- data/resources/removebgByCanva.svg +47 -0
- data/sonar-project.properties +22 -0
- data/tasks/version.rake +34 -0
- metadata +206 -82
- data/.env.test +0 -2
- data/LICENSE +0 -21
- data/gemfiles/faraday_0_15.gemfile +0 -11
- data/gemfiles/faraday_0_16.gemfile +0 -11
- data/gemfiles/faraday_0_17.gemfile +0 -11
- data/gemfiles/faraday_1_x.gemfile +0 -11
data/Gemfile.lock
CHANGED
@@ -1,107 +1,159 @@
|
|
1
|
-
GIT
|
2
|
-
remote: https://github.com/thoughtbot/appraisal.git
|
3
|
-
revision: 586864393e405a67b1457b563a4d5adc99e50e2d
|
4
|
-
ref: 5868643
|
5
|
-
specs:
|
6
|
-
appraisal (2.2.0)
|
7
|
-
bundler
|
8
|
-
rake
|
9
|
-
thor (>= 0.14.0)
|
10
|
-
|
11
1
|
PATH
|
12
2
|
remote: .
|
13
3
|
specs:
|
14
|
-
remove_bg (
|
15
|
-
faraday (>=
|
16
|
-
|
4
|
+
remove_bg (2.0.0)
|
5
|
+
faraday (>= 2, <= 3)
|
6
|
+
faraday-multipart (~> 1.0)
|
7
|
+
faraday-retry (~> 2.2)
|
8
|
+
image_processing (~> 1.12)
|
17
9
|
rubyzip (>= 2.0, < 3)
|
18
10
|
|
19
11
|
GEM
|
20
12
|
remote: https://rubygems.org/
|
21
13
|
specs:
|
22
|
-
addressable (2.7
|
23
|
-
public_suffix (>= 2.0.2, <
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
14
|
+
addressable (2.8.7)
|
15
|
+
public_suffix (>= 2.0.2, < 7.0)
|
16
|
+
appraisal (2.5.0)
|
17
|
+
bundler
|
18
|
+
rake
|
19
|
+
thor (>= 0.14.0)
|
20
|
+
ast (2.4.2)
|
21
|
+
bigdecimal (3.1.8)
|
22
|
+
coderay (1.1.3)
|
23
|
+
crack (1.0.0)
|
24
|
+
bigdecimal
|
25
|
+
rexml
|
26
|
+
diff-lcs (1.5.1)
|
27
|
+
docile (1.4.0)
|
28
|
+
faraday (2.9.2)
|
29
|
+
faraday-net_http (>= 2.0, < 3.2)
|
30
|
+
faraday-multipart (1.0.4)
|
31
|
+
multipart-post (~> 2)
|
32
|
+
faraday-net_http (3.1.0)
|
33
|
+
net-http
|
34
|
+
faraday-retry (2.2.1)
|
35
|
+
faraday (~> 2.0)
|
36
|
+
ffi (1.17.0)
|
37
|
+
ffi (1.17.0-arm64-darwin)
|
38
|
+
ffi (1.17.0-x86_64-linux-gnu)
|
39
|
+
hashdiff (1.1.0)
|
40
|
+
htmlentities (4.3.4)
|
41
|
+
image_processing (1.12.2)
|
39
42
|
mini_magick (>= 4.9.5, < 5)
|
40
43
|
ruby-vips (>= 2.0.17, < 3)
|
41
|
-
json (2.
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
44
|
+
json (2.7.2)
|
45
|
+
language_server-protocol (3.17.0.3)
|
46
|
+
method_source (1.1.0)
|
47
|
+
mini_magick (4.13.1)
|
48
|
+
multipart-post (2.4.1)
|
49
|
+
net-http (0.4.1)
|
50
|
+
uri
|
51
|
+
parallel (1.25.1)
|
52
|
+
parser (3.3.3.0)
|
53
|
+
ast (~> 2.4.1)
|
54
|
+
racc
|
55
|
+
pry (0.14.2)
|
46
56
|
coderay (~> 1.1)
|
47
57
|
method_source (~> 1.0)
|
48
|
-
public_suffix (
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
58
|
+
public_suffix (6.0.0)
|
59
|
+
racc (1.8.0)
|
60
|
+
rainbow (3.1.1)
|
61
|
+
rake (13.2.1)
|
62
|
+
regexp_parser (2.9.2)
|
63
|
+
rexml (3.3.1)
|
64
|
+
strscan
|
65
|
+
rspec (3.13.0)
|
66
|
+
rspec-core (~> 3.13.0)
|
67
|
+
rspec-expectations (~> 3.13.0)
|
68
|
+
rspec-mocks (~> 3.13.0)
|
69
|
+
rspec-core (3.13.0)
|
70
|
+
rspec-support (~> 3.13.0)
|
71
|
+
rspec-expectations (3.13.1)
|
57
72
|
diff-lcs (>= 1.2.0, < 2.0)
|
58
|
-
rspec-support (~> 3.
|
59
|
-
rspec-mocks (3.
|
73
|
+
rspec-support (~> 3.13.0)
|
74
|
+
rspec-mocks (3.13.1)
|
60
75
|
diff-lcs (>= 1.2.0, < 2.0)
|
61
|
-
rspec-support (~> 3.
|
62
|
-
rspec-
|
63
|
-
|
76
|
+
rspec-support (~> 3.13.0)
|
77
|
+
rspec-sonarqube-formatter (1.5.0)
|
78
|
+
htmlentities (~> 4.3.3)
|
79
|
+
rspec (~> 3.0)
|
80
|
+
rspec-support (3.13.1)
|
81
|
+
rspec-with_params (0.3.0)
|
64
82
|
rspec (~> 3.0)
|
65
|
-
rspec_junit_formatter (0.
|
83
|
+
rspec_junit_formatter (0.6.0)
|
66
84
|
rspec-core (>= 2, < 4, != 2.12.0)
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
85
|
+
rubocop (1.64.1)
|
86
|
+
json (~> 2.3)
|
87
|
+
language_server-protocol (>= 3.17.0)
|
88
|
+
parallel (~> 1.10)
|
89
|
+
parser (>= 3.3.0.2)
|
90
|
+
rainbow (>= 2.2.2, < 4.0)
|
91
|
+
regexp_parser (>= 1.8, < 3.0)
|
92
|
+
rexml (>= 3.2.5, < 4.0)
|
93
|
+
rubocop-ast (>= 1.31.1, < 2.0)
|
94
|
+
ruby-progressbar (~> 1.7)
|
95
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
96
|
+
rubocop-ast (1.31.3)
|
97
|
+
parser (>= 3.3.1.0)
|
98
|
+
rubocop-performance (1.21.1)
|
99
|
+
rubocop (>= 1.48.1, < 2.0)
|
100
|
+
rubocop-ast (>= 1.31.1, < 2.0)
|
101
|
+
rubocop-rake (0.6.0)
|
102
|
+
rubocop (~> 1.0)
|
103
|
+
rubocop-rspec (3.0.2)
|
104
|
+
rubocop (~> 1.61)
|
105
|
+
ruby-progressbar (1.13.0)
|
106
|
+
ruby-vips (2.2.1)
|
107
|
+
ffi (~> 1.12)
|
108
|
+
rubyzip (2.3.2)
|
109
|
+
simplecov (0.22.0)
|
72
110
|
docile (~> 1.1)
|
73
111
|
simplecov-html (~> 0.11)
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
112
|
+
simplecov_json_formatter (~> 0.1)
|
113
|
+
simplecov-cobertura (2.1.0)
|
114
|
+
rexml
|
115
|
+
simplecov (~> 0.19)
|
116
|
+
simplecov-html (0.12.3)
|
117
|
+
simplecov_json_formatter (0.1.4)
|
118
|
+
strscan (3.1.0)
|
119
|
+
thor (1.3.1)
|
120
|
+
unicode-display_width (2.5.0)
|
121
|
+
uri (0.13.0)
|
122
|
+
vcr (6.2.0)
|
123
|
+
vcr_better_binary (0.2.0)
|
124
|
+
vcr (>= 5.0)
|
125
|
+
webmock (3.23.1)
|
126
|
+
addressable (>= 2.8.0)
|
82
127
|
crack (>= 0.3.2)
|
83
128
|
hashdiff (>= 0.4.0, < 2.0.0)
|
84
|
-
yard (0.9.
|
129
|
+
yard (0.9.36)
|
85
130
|
|
86
131
|
PLATFORMS
|
132
|
+
arm64-darwin-22
|
87
133
|
ruby
|
134
|
+
x86_64-linux
|
88
135
|
|
89
136
|
DEPENDENCIES
|
90
|
-
appraisal
|
137
|
+
appraisal (~> 2.5)
|
91
138
|
bundler (~> 2.0)
|
92
|
-
|
93
|
-
|
94
|
-
pry
|
95
|
-
rake
|
139
|
+
pry (~> 0.14)
|
140
|
+
rake (~> 13.2)
|
96
141
|
remove_bg!
|
97
|
-
rspec (~> 3.
|
98
|
-
rspec-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
142
|
+
rspec (~> 3.13)
|
143
|
+
rspec-sonarqube-formatter (~> 1.5)
|
144
|
+
rspec-with_params (~> 0.3)
|
145
|
+
rspec_junit_formatter (~> 0.6)
|
146
|
+
rubocop (~> 1.64)
|
147
|
+
rubocop-performance (~> 1.21)
|
148
|
+
rubocop-rake (~> 0.6)
|
149
|
+
rubocop-rspec (~> 3.0)
|
150
|
+
simplecov (~> 0.22)
|
151
|
+
simplecov-cobertura (~> 2.1)
|
152
|
+
simplecov_json_formatter (~> 0.1)
|
153
|
+
vcr (~> 6.2)
|
154
|
+
vcr_better_binary (~> 0.2)
|
155
|
+
webmock (~> 3.23)
|
156
|
+
yard (~> 0.9)
|
105
157
|
|
106
158
|
BUNDLED WITH
|
107
|
-
2.
|
159
|
+
2.4.19
|
data/LICENSE.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
=====================
|
3
|
+
|
4
|
+
Copyright © 2019–2024 Canva Austria GmbH
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person
|
7
|
+
obtaining a copy of this software and associated documentation
|
8
|
+
files (the “Software”), to deal in the Software without
|
9
|
+
restriction, including without limitation the rights to use,
|
10
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
copies of the Software, and to permit persons to whom the
|
12
|
+
Software is furnished to do so, subject to the following
|
13
|
+
conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be
|
16
|
+
included in all copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
19
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,14 +1,32 @@
|
|
1
|
-
[![RemoveBG](resources/
|
1
|
+
[![RemoveBG](resources/removebgByCanva.svg)](https://www.remove.bg/)
|
2
2
|
|
3
|
-
# Ruby
|
3
|
+
# remove.bg Ruby Gem
|
4
4
|
|
5
|
-
|
5
|
+
<p align="center">
|
6
6
|
|
7
|
-
|
7
|
+
[![Gem Version](https://img.shields.io/gem/v/remove_bg?style=for-the-badge&logo=rubygems&logoColor=aaa&color=367cd3)](https://rubygems.org/gems/remove_bg)
|
8
|
+
[![Gem Total Downloads](https://img.shields.io/gem/dt/remove_bg?style=for-the-badge&logo=ruby&logoColor=aaa&color=367cd3)](https://rubygems.org/gems/remove_bg)
|
9
|
+
[![CircleCI](https://img.shields.io/circleci/build/github/remove-bg/ruby?style=for-the-badge&logo=circleci&logoColor=aaa)](https://circleci.com/gh/remove-bg/ruby/tree/main)
|
10
|
+
[![GitHub License](https://img.shields.io/github/license/remove-bg/ruby?style=for-the-badge&logo=readthedocs&logoColor=aaa)](https://github.com/remove-bg/ruby/blob/main/LICENSE.md)
|
11
|
+
[![Codecov](https://img.shields.io/codecov/c/github/remove-bg/ruby?style=for-the-badge&logo=codecov&logoColor=aaa)](https://app.codecov.io/gh/remove-bg/ruby)
|
12
|
+
[![Sonar Quality Gate](https://img.shields.io/sonar/quality_gate/remove-bg_ruby?server=https%3A%2F%2Fsonarcloud.io&style=for-the-badge&logo=sonarcloud&logoColor=aaa)](https://sonarcloud.io/summary/overall?id=remove-bg_ruby)
|
13
|
+
[![Violations](https://img.shields.io/sonar/violations/remove-bg_ruby?server=https%3A%2F%2Fsonarcloud.io&format=short&style=for-the-badge&logo=sonarcloud&logoColor=aaa)](https://sonarcloud.io/summary/overall?id=remove-bg_ruby)
|
14
|
+
![Depfu](https://img.shields.io/depfu/dependencies/github/remove-bg%2Fruby?style=for-the-badge&logo=dependabot&logoColor=aaa)
|
15
|
+
|
16
|
+
</p>
|
17
|
+
|
18
|
+
## Requirements
|
19
|
+
|
20
|
+
This gem is compatible with Ruby 3.1+ and can be used with
|
21
|
+
[Faraday](https://rubygems.org/gems/faraday/) version 2 and above
|
22
|
+
|
23
|
+
An API key (free) from [remove.bg](https://www.remove.bg/api) is required.
|
24
|
+
|
25
|
+
## Quickstart Installation
|
8
26
|
|
9
27
|
Add the gem to your `Gemfile` and run `bundle install`:
|
10
28
|
|
11
|
-
```
|
29
|
+
```ruby
|
12
30
|
gem "remove_bg"
|
13
31
|
```
|
14
32
|
|
@@ -19,7 +37,7 @@ Please note the base configuration has the following resolution limits:
|
|
19
37
|
| Output format | Resolution limit |
|
20
38
|
|---------------|------------------|
|
21
39
|
| PNG | 10 megapixels |
|
22
|
-
| JPG |
|
40
|
+
| JPG | 50 megapixels |
|
23
41
|
|
24
42
|
## Full installation
|
25
43
|
|
@@ -30,8 +48,8 @@ Please install one of the following libraries:
|
|
30
48
|
- [GraphicsMagick](http://www.graphicsmagick.org/)
|
31
49
|
- [libvips](http://libvips.github.io/libvips/)
|
32
50
|
|
33
|
-
The gem will auto-detect any image processing libraries present. However you
|
34
|
-
|
51
|
+
The gem will auto-detect any image processing libraries present. However, you can
|
52
|
+
also explicitly configure which library to use:
|
35
53
|
|
36
54
|
```ruby
|
37
55
|
RemoveBg.configure do |config|
|
@@ -45,9 +63,9 @@ The full installation has the following resolution limits:
|
|
45
63
|
|
46
64
|
| Output format | Resolution limit |
|
47
65
|
|---------------|------------------|
|
48
|
-
| PNG |
|
49
|
-
| JPG |
|
50
|
-
| ZIP |
|
66
|
+
| PNG | 10 megapixels |
|
67
|
+
| JPG | 50 megapixels |
|
68
|
+
| ZIP | 50 megapixels |
|
51
69
|
|
52
70
|
# Usage
|
53
71
|
|
@@ -108,7 +126,8 @@ There's also a `#save` convenience method:
|
|
108
126
|
|
109
127
|
```ruby
|
110
128
|
result.save("processed/image.png")
|
111
|
-
|
129
|
+
# Overwrite any existing file
|
130
|
+
result.save!("processed/image.png")
|
112
131
|
```
|
113
132
|
|
114
133
|
## Producing transparent images over 10 megapixels
|
data/Rakefile
CHANGED
data/SECURITY.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Security Policy
|
2
|
+
|
3
|
+
We take the security of our software products and services seriously, including all of
|
4
|
+
the open source code repositories managed through our organization.
|
5
|
+
|
6
|
+
## Supported Versions
|
7
|
+
|
8
|
+
We currently support these versions of this library with security updates.
|
9
|
+
|
10
|
+
| Version | Supported |
|
11
|
+
| -------- | ------------------ |
|
12
|
+
| `2.x.x` | :white_check_mark: |
|
13
|
+
| `<= 1.5` | :x: |
|
14
|
+
|
15
|
+
## Reporting a Vulnerability
|
16
|
+
|
17
|
+
If you believe you have found a security vulnerability in this repository, please report
|
18
|
+
it to us through coordinated disclosure.
|
19
|
+
|
20
|
+
Please do not report security vulnerabilities through public GitHub issues, discussions,
|
21
|
+
or pull requests.
|
22
|
+
|
23
|
+
Instead, please contact us at https://www.remove.bg/support/contact
|
24
|
+
|
25
|
+
Please include as much of the information listed below as you can to help us better
|
26
|
+
understand and resolve the issue:
|
27
|
+
|
28
|
+
- The type of issue (e.g., buffer overflow, issue in a dependency,...)
|
29
|
+
- Full paths of source file(s) related to the manifestation of the issue
|
30
|
+
- The location of the affected source code (tag/branch/commit or direct URL)
|
31
|
+
- Any special configuration required to reproduce the issue
|
32
|
+
- Step-by-step instructions to reproduce the issue
|
33
|
+
- Proof-of-concept or exploit code (if possible)
|
34
|
+
- Impact of the issue, including how an attacker might exploit the issue
|
35
|
+
|
36
|
+
This information will help us triage your report more quickly.
|
data/lib/remove_bg/api.rb
CHANGED
data/lib/remove_bg/api_client.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "json"
|
2
4
|
require "tempfile"
|
3
5
|
|
@@ -41,7 +43,7 @@ module RemoveBg
|
|
41
43
|
#
|
42
44
|
def remove_from_url(image_url, options)
|
43
45
|
RemoveBg::UrlValidator.validate(image_url)
|
44
|
-
data = options.data.merge(image_url:
|
46
|
+
data = options.data.merge(image_url:)
|
45
47
|
request_remove_bg(data, options.api_key)
|
46
48
|
end
|
47
49
|
|
@@ -70,26 +72,24 @@ module RemoveBg
|
|
70
72
|
# Faraday v0.16 & v1.0+ support streaming, v0.17 did not (rollback release)
|
71
73
|
if req.options.respond_to?(:on_data)
|
72
74
|
streaming = true
|
73
|
-
req.options.on_data =
|
75
|
+
req.options.on_data = proc do |chunk, _|
|
74
76
|
download.write(chunk)
|
75
77
|
end
|
76
78
|
end
|
77
79
|
end
|
78
80
|
|
79
|
-
# Faraday v0.
|
80
|
-
|
81
|
-
download.write(response.body)
|
82
|
-
end
|
81
|
+
# Faraday v0.17
|
82
|
+
download.write(response.body) unless streaming
|
83
83
|
|
84
84
|
download.rewind
|
85
85
|
|
86
86
|
if response.status == 200
|
87
|
-
parse_image_result(headers: response.headers, download:
|
87
|
+
parse_image_result(headers: response.headers, download:)
|
88
88
|
else
|
89
89
|
response_body = download.read
|
90
90
|
download.close
|
91
91
|
download.unlink
|
92
|
-
handle_http_error(response
|
92
|
+
handle_http_error(response:, body: response_body)
|
93
93
|
end
|
94
94
|
end
|
95
95
|
|
@@ -101,7 +101,7 @@ module RemoveBg
|
|
101
101
|
if response.status == 200
|
102
102
|
parse_account_result(response)
|
103
103
|
else
|
104
|
-
handle_http_error(response
|
104
|
+
handle_http_error(response:, body: response.body)
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
@@ -123,7 +123,7 @@ module RemoveBg
|
|
123
123
|
|
124
124
|
def parse_image_result(headers:, download:)
|
125
125
|
result_for_content_type(headers["Content-Type"]).new(
|
126
|
-
download
|
126
|
+
download:,
|
127
127
|
metadata: ResultMetadata.new(headers),
|
128
128
|
rate_limit: RateLimitInfo.new(headers)
|
129
129
|
)
|
@@ -139,8 +139,8 @@ module RemoveBg
|
|
139
139
|
|
140
140
|
def parse_account_result(response)
|
141
141
|
attributes = JSON.parse(response.body, symbolize_names: true)
|
142
|
-
|
143
|
-
|
142
|
+
.fetch(:data)
|
143
|
+
.fetch(:attributes)
|
144
144
|
|
145
145
|
RemoveBg::AccountInfo.new(attributes)
|
146
146
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative "result"
|
2
4
|
|
3
5
|
module RemoveBg
|
@@ -13,13 +15,22 @@ module RemoveBg
|
|
13
15
|
# @return [nil]
|
14
16
|
#
|
15
17
|
def save_zip(file_path, overwrite: false)
|
16
|
-
if File.exist?(file_path) && !overwrite
|
17
|
-
|
18
|
-
|
18
|
+
raise FileOverwriteError.new(file_path) if File.exist?(file_path) && !overwrite
|
19
|
+
|
20
|
+
warn("DEPRECATION WARNING: overwrite: true is deprecated and will be removed from remove_bg 2.0 (use save_zip! instead)") if overwrite
|
19
21
|
|
20
22
|
FileUtils.cp(download, file_path)
|
21
23
|
end
|
22
24
|
|
25
|
+
# Saves the ZIP archive containing the alpha.png and color.jpg files, overwriting any exisiting files
|
26
|
+
# (useful if you want to handle composition yourself)
|
27
|
+
# @param file_path [string]
|
28
|
+
# @return [nil]
|
29
|
+
#
|
30
|
+
def save_zip!(file_path)
|
31
|
+
FileUtils.cp(download, file_path)
|
32
|
+
end
|
33
|
+
|
23
34
|
private
|
24
35
|
|
25
36
|
def image_file
|
@@ -27,10 +38,8 @@ module RemoveBg
|
|
27
38
|
end
|
28
39
|
|
29
40
|
def composite_file
|
30
|
-
@composite_file ||=
|
31
|
-
|
32
|
-
.tap { |file| compose_to_file(file) }
|
33
|
-
end
|
41
|
+
@composite_file ||= binary_tempfile(["composed", ".png"])
|
42
|
+
.tap { |file| compose_to_file(file) }
|
34
43
|
end
|
35
44
|
|
36
45
|
def color_file
|
@@ -45,8 +54,8 @@ module RemoveBg
|
|
45
54
|
extract_parts
|
46
55
|
|
47
56
|
ImageComposer.new.compose(
|
48
|
-
color_file
|
49
|
-
alpha_file
|
57
|
+
color_file:,
|
58
|
+
alpha_file:,
|
50
59
|
destination_path: destination.path
|
51
60
|
)
|
52
61
|
end
|
@@ -59,7 +68,7 @@ module RemoveBg
|
|
59
68
|
end
|
60
69
|
|
61
70
|
def binary_tempfile(basename)
|
62
|
-
Tempfile.new(basename).tap
|
71
|
+
Tempfile.new(basename).tap(&:binmode)
|
63
72
|
end
|
64
73
|
end
|
65
74
|
end
|