remove_bg 1.5.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +329 -59
  3. data/.depfu.yml +21 -0
  4. data/.editorconfig +19 -0
  5. data/.gitignore +8 -8
  6. data/.grenrc.json +20 -0
  7. data/.rspec +5 -0
  8. data/.rubocop.yml +43 -0
  9. data/.tool-versions +1 -0
  10. data/Appraisals +49 -10
  11. data/CHANGELOG.md +19 -2
  12. data/Gemfile +2 -5
  13. data/Gemfile.lock +132 -78
  14. data/LICENSE.md +25 -0
  15. data/README.md +31 -12
  16. data/Rakefile +4 -0
  17. data/SECURITY.md +36 -0
  18. data/gemfiles/faraday_2_0.gemfile +7 -0
  19. data/gemfiles/faraday_2_1.gemfile +7 -0
  20. data/gemfiles/faraday_2_2.gemfile +7 -0
  21. data/gemfiles/faraday_2_3.gemfile +7 -0
  22. data/gemfiles/faraday_2_4.gemfile +7 -0
  23. data/gemfiles/faraday_2_5.gemfile +7 -0
  24. data/gemfiles/faraday_2_6.gemfile +7 -0
  25. data/gemfiles/faraday_2_7.gemfile +7 -0
  26. data/gemfiles/faraday_2_8.gemfile +7 -0
  27. data/gemfiles/faraday_2_9.gemfile +7 -0
  28. data/gemfiles/faraday_2_x.gemfile +7 -0
  29. data/lib/remove_bg/account_info.rb +2 -0
  30. data/lib/remove_bg/api.rb +2 -0
  31. data/lib/remove_bg/api_client.rb +12 -12
  32. data/lib/remove_bg/base_request_options.rb +2 -0
  33. data/lib/remove_bg/composite_result.rb +19 -10
  34. data/lib/remove_bg/configuration.rb +2 -0
  35. data/lib/remove_bg/error.rb +3 -1
  36. data/lib/remove_bg/http_connection.rb +7 -4
  37. data/lib/remove_bg/image_composer.rb +13 -12
  38. data/lib/remove_bg/rate_limit_info.rb +9 -6
  39. data/lib/remove_bg/request_options.rb +3 -3
  40. data/lib/remove_bg/result.rb +13 -3
  41. data/lib/remove_bg/result_metadata.rb +2 -0
  42. data/lib/remove_bg/upload.rb +5 -8
  43. data/lib/remove_bg/url_validator.rb +4 -4
  44. data/lib/remove_bg/version.rb +3 -1
  45. data/lib/remove_bg.rb +2 -0
  46. data/remove_bg.gemspec +43 -20
  47. data/resources/removebgByCanva.svg +47 -0
  48. data/sonar-project.properties +22 -0
  49. data/tasks/version.rake +34 -0
  50. metadata +207 -82
  51. data/.env.test +0 -2
  52. data/LICENSE +0 -21
  53. data/gemfiles/faraday_0_15.gemfile +0 -11
  54. data/gemfiles/faraday_0_16.gemfile +0 -11
  55. data/gemfiles/faraday_0_17.gemfile +0 -11
  56. data/gemfiles/faraday_1_x.gemfile +0 -11
data/Gemfile.lock CHANGED
@@ -1,107 +1,161 @@
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 (1.5.0)
15
- faraday (>= 0.15, < 2)
16
- image_processing (>= 1.9, < 2)
4
+ remove_bg (2.0.1)
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.0)
23
- public_suffix (>= 2.0.2, < 5.0)
24
- codecov (0.1.16)
25
- json
26
- simplecov
27
- url
28
- coderay (1.1.2)
29
- crack (0.4.3)
30
- safe_yaml (~> 1.0.0)
31
- diff-lcs (1.3)
32
- docile (1.3.2)
33
- dotenv (2.7.5)
34
- faraday (1.0.1)
35
- multipart-post (>= 1.2, < 3)
36
- ffi (1.13.1)
37
- hashdiff (1.0.1)
38
- image_processing (1.11.0)
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.10.0)
29
+ faraday-net_http (>= 2.0, < 3.2)
30
+ logger
31
+ faraday-multipart (1.0.4)
32
+ multipart-post (~> 2)
33
+ faraday-net_http (3.1.0)
34
+ net-http
35
+ faraday-retry (2.2.1)
36
+ faraday (~> 2.0)
37
+ ffi (1.17.0)
38
+ ffi (1.17.0-arm64-darwin)
39
+ ffi (1.17.0-x86_64-linux-gnu)
40
+ hashdiff (1.1.0)
41
+ htmlentities (4.3.4)
42
+ image_processing (1.12.2)
39
43
  mini_magick (>= 4.9.5, < 5)
40
44
  ruby-vips (>= 2.0.17, < 3)
41
- json (2.3.0)
42
- method_source (1.0.0)
43
- mini_magick (4.10.1)
44
- multipart-post (2.1.1)
45
- pry (0.13.1)
45
+ json (2.7.2)
46
+ language_server-protocol (3.17.0.3)
47
+ logger (1.6.0)
48
+ method_source (1.1.0)
49
+ mini_magick (4.13.2)
50
+ multipart-post (2.4.1)
51
+ net-http (0.4.1)
52
+ uri
53
+ parallel (1.25.1)
54
+ parser (3.3.4.0)
55
+ ast (~> 2.4.1)
56
+ racc
57
+ pry (0.14.2)
46
58
  coderay (~> 1.1)
47
59
  method_source (~> 1.0)
48
- public_suffix (4.0.4)
49
- rake (13.0.1)
50
- rspec (3.9.0)
51
- rspec-core (~> 3.9.0)
52
- rspec-expectations (~> 3.9.0)
53
- rspec-mocks (~> 3.9.0)
54
- rspec-core (3.9.2)
55
- rspec-support (~> 3.9.3)
56
- rspec-expectations (3.9.2)
60
+ public_suffix (6.0.0)
61
+ racc (1.8.0)
62
+ rainbow (3.1.1)
63
+ rake (13.2.1)
64
+ regexp_parser (2.9.2)
65
+ rexml (3.3.2)
66
+ strscan
67
+ rspec (3.13.0)
68
+ rspec-core (~> 3.13.0)
69
+ rspec-expectations (~> 3.13.0)
70
+ rspec-mocks (~> 3.13.0)
71
+ rspec-core (3.13.0)
72
+ rspec-support (~> 3.13.0)
73
+ rspec-expectations (3.13.1)
57
74
  diff-lcs (>= 1.2.0, < 2.0)
58
- rspec-support (~> 3.9.0)
59
- rspec-mocks (3.9.1)
75
+ rspec-support (~> 3.13.0)
76
+ rspec-mocks (3.13.1)
60
77
  diff-lcs (>= 1.2.0, < 2.0)
61
- rspec-support (~> 3.9.0)
62
- rspec-support (3.9.3)
63
- rspec-with_params (0.2.0)
78
+ rspec-support (~> 3.13.0)
79
+ rspec-sonarqube-formatter (1.5.0)
80
+ htmlentities (~> 4.3.3)
81
+ rspec (~> 3.0)
82
+ rspec-support (3.13.1)
83
+ rspec-with_params (0.3.0)
64
84
  rspec (~> 3.0)
65
- rspec_junit_formatter (0.4.1)
85
+ rspec_junit_formatter (0.6.0)
66
86
  rspec-core (>= 2, < 4, != 2.12.0)
67
- ruby-vips (2.0.17)
68
- ffi (~> 1.9)
69
- rubyzip (2.3.0)
70
- safe_yaml (1.0.5)
71
- simplecov (0.18.5)
87
+ rubocop (1.65.0)
88
+ json (~> 2.3)
89
+ language_server-protocol (>= 3.17.0)
90
+ parallel (~> 1.10)
91
+ parser (>= 3.3.0.2)
92
+ rainbow (>= 2.2.2, < 4.0)
93
+ regexp_parser (>= 2.4, < 3.0)
94
+ rexml (>= 3.2.5, < 4.0)
95
+ rubocop-ast (>= 1.31.1, < 2.0)
96
+ ruby-progressbar (~> 1.7)
97
+ unicode-display_width (>= 2.4.0, < 3.0)
98
+ rubocop-ast (1.31.3)
99
+ parser (>= 3.3.1.0)
100
+ rubocop-performance (1.21.1)
101
+ rubocop (>= 1.48.1, < 2.0)
102
+ rubocop-ast (>= 1.31.1, < 2.0)
103
+ rubocop-rake (0.6.0)
104
+ rubocop (~> 1.0)
105
+ rubocop-rspec (3.0.3)
106
+ rubocop (~> 1.61)
107
+ ruby-progressbar (1.13.0)
108
+ ruby-vips (2.2.1)
109
+ ffi (~> 1.12)
110
+ rubyzip (2.3.2)
111
+ simplecov (0.22.0)
72
112
  docile (~> 1.1)
73
113
  simplecov-html (~> 0.11)
74
- simplecov-html (0.12.2)
75
- thor (1.0.1)
76
- url (0.3.2)
77
- vcr (5.1.0)
78
- vcr_better_binary (0.1.0)
79
- vcr (~> 5.0)
80
- webmock (3.8.3)
81
- addressable (>= 2.3.6)
114
+ simplecov_json_formatter (~> 0.1)
115
+ simplecov-cobertura (2.1.0)
116
+ rexml
117
+ simplecov (~> 0.19)
118
+ simplecov-html (0.12.3)
119
+ simplecov_json_formatter (0.1.4)
120
+ strscan (3.1.0)
121
+ thor (1.3.1)
122
+ unicode-display_width (2.5.0)
123
+ uri (0.13.0)
124
+ vcr (6.2.0)
125
+ vcr_better_binary (0.2.0)
126
+ vcr (>= 5.0)
127
+ webmock (3.23.1)
128
+ addressable (>= 2.8.0)
82
129
  crack (>= 0.3.2)
83
130
  hashdiff (>= 0.4.0, < 2.0.0)
84
- yard (0.9.25)
131
+ yard (0.9.36)
85
132
 
86
133
  PLATFORMS
134
+ arm64-darwin-22
87
135
  ruby
136
+ x86_64-linux
88
137
 
89
138
  DEPENDENCIES
90
- appraisal!
139
+ appraisal (~> 2.5)
91
140
  bundler (~> 2.0)
92
- codecov
93
- dotenv
94
- pry
95
- rake
141
+ pry (~> 0.14)
142
+ rake (~> 13.2)
96
143
  remove_bg!
97
- rspec (~> 3.8)
98
- rspec-with_params
99
- rspec_junit_formatter
100
- simplecov
101
- vcr
102
- vcr_better_binary
103
- webmock
104
- yard
144
+ rspec (~> 3.13)
145
+ rspec-sonarqube-formatter (~> 1.5)
146
+ rspec-with_params (~> 0.3)
147
+ rspec_junit_formatter (~> 0.6)
148
+ rubocop (~> 1.64)
149
+ rubocop-performance (~> 1.21)
150
+ rubocop-rake (~> 0.6)
151
+ rubocop-rspec (~> 3.0)
152
+ simplecov (~> 0.22)
153
+ simplecov-cobertura (~> 2.1)
154
+ simplecov_json_formatter (~> 0.1)
155
+ vcr (~> 6.2)
156
+ vcr_better_binary (~> 0.2)
157
+ webmock (~> 3.23)
158
+ yard (~> 0.9)
105
159
 
106
160
  BUNDLED WITH
107
- 2.1.4
161
+ 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/logo_black.png)](https://www.remove.bg/)
1
+ [![RemoveBG](resources/removebgByCanva.svg)](https://www.remove.bg/)
2
2
 
3
- # Ruby library
3
+ # remove.bg Ruby Gem
4
4
 
5
- [![CircleCI](https://circleci.com/gh/remove-bg/ruby/tree/master.svg?style=shield)](https://circleci.com/gh/remove-bg/ruby/tree/master) [![Gem Version](https://badge.fury.io/rb/remove_bg.svg)](https://rubygems.org/gems/remove_bg) [![codecov](https://codecov.io/gh/remove-bg/ruby/branch/master/graph/badge.svg)](https://codecov.io/gh/remove-bg/ruby)
5
+ <p align="center">
6
6
 
7
- ## Quickstart installation
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
+ [![Dependencies](https://img.shields.io/depfu/dependencies/github/remove-bg%2Fruby?style=for-the-badge&logo=dependabot&logoColor=aaa)](https://github.com/remove-bg/ruby/network/dependencies)
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 | 25 megapixels |
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 may
34
- prefer to explicitly configure which library to use:
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 | 25 megapixels |
49
- | JPG | 25 megapixels |
50
- | ZIP | 25 megapixels |
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
- result.save("image.png", overwrite: true) # Careful!
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
@@ -1,6 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
2
4
  require "rspec/core/rake_task"
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
8
+ Dir.glob("tasks/*.rake").each { |r| import r }
9
+
6
10
  task default: :spec
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.
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "faraday", "~> 2.0.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "faraday", "~> 2.1.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "faraday", "~> 2.2.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "faraday", "~> 2.3.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "faraday", "~> 2.4.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "faraday", "~> 2.5.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "faraday", "~> 2.6.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "faraday", "~> 2.7.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "faraday", "~> 2.8.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "faraday", "~> 2.9.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "faraday", "~> 2", "< 3"
6
+
7
+ gemspec path: "../"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RemoveBg
2
4
  class AccountInfo
3
5
  # @return [RemoveBg::AccountInfo::ApiInfo]
data/lib/remove_bg/api.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RemoveBg
2
4
  module Api
3
5
  URL = "https://api.remove.bg"
@@ -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: 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 = Proc.new do |chunk, _|
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.15 / v0.17
80
- if !streaming
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: 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: response, body: response_body)
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: response, body: response.body)
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: 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
- .fetch(:data)
143
- .fetch(:attributes)
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 "error"
2
4
 
3
5
  module RemoveBg
@@ -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
- raise FileOverwriteError.new(file_path)
18
- end
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 ||= begin
31
- binary_tempfile(["composed", ".png"])
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: color_file,
49
- alpha_file: 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 { |file| file.binmode }
71
+ Tempfile.new(basename).tap(&:binmode)
63
72
  end
64
73
  end
65
74
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "image_composer"
2
4
 
3
5
  module RemoveBg