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.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +298 -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/Appraisals +49 -10
  10. data/CHANGELOG.md +6 -1
  11. data/Gemfile +2 -5
  12. data/Gemfile.lock +130 -78
  13. data/LICENSE.md +25 -0
  14. data/README.md +31 -12
  15. data/Rakefile +4 -0
  16. data/SECURITY.md +36 -0
  17. data/gemfiles/faraday_2_0.gemfile +7 -0
  18. data/gemfiles/faraday_2_1.gemfile +7 -0
  19. data/gemfiles/faraday_2_2.gemfile +7 -0
  20. data/gemfiles/faraday_2_3.gemfile +7 -0
  21. data/gemfiles/faraday_2_4.gemfile +7 -0
  22. data/gemfiles/faraday_2_5.gemfile +7 -0
  23. data/gemfiles/faraday_2_6.gemfile +7 -0
  24. data/gemfiles/faraday_2_7.gemfile +7 -0
  25. data/gemfiles/faraday_2_8.gemfile +7 -0
  26. data/gemfiles/faraday_2_9.gemfile +7 -0
  27. data/gemfiles/faraday_2_x.gemfile +7 -0
  28. data/lib/remove_bg/account_info.rb +2 -0
  29. data/lib/remove_bg/api.rb +2 -0
  30. data/lib/remove_bg/api_client.rb +12 -12
  31. data/lib/remove_bg/base_request_options.rb +2 -0
  32. data/lib/remove_bg/composite_result.rb +19 -10
  33. data/lib/remove_bg/configuration.rb +2 -0
  34. data/lib/remove_bg/error.rb +3 -1
  35. data/lib/remove_bg/http_connection.rb +7 -4
  36. data/lib/remove_bg/image_composer.rb +13 -12
  37. data/lib/remove_bg/rate_limit_info.rb +9 -6
  38. data/lib/remove_bg/request_options.rb +3 -3
  39. data/lib/remove_bg/result.rb +13 -3
  40. data/lib/remove_bg/result_metadata.rb +2 -0
  41. data/lib/remove_bg/upload.rb +7 -5
  42. data/lib/remove_bg/url_validator.rb +4 -4
  43. data/lib/remove_bg/version.rb +3 -1
  44. data/lib/remove_bg.rb +2 -0
  45. data/remove_bg.gemspec +43 -20
  46. data/resources/removebgByCanva.svg +47 -0
  47. data/sonar-project.properties +22 -0
  48. data/tasks/version.rake +34 -0
  49. metadata +206 -82
  50. data/.env.test +0 -2
  51. data/LICENSE +0 -21
  52. data/gemfiles/faraday_0_15.gemfile +0 -11
  53. data/gemfiles/faraday_0_16.gemfile +0 -11
  54. data/gemfiles/faraday_0_17.gemfile +0 -11
  55. 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 (1.5.0)
15
- faraday (>= 0.15, < 2)
16
- image_processing (>= 1.9, < 2)
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.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.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.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)
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 (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)
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.9.0)
59
- rspec-mocks (3.9.1)
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.9.0)
62
- rspec-support (3.9.3)
63
- rspec-with_params (0.2.0)
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.4.1)
83
+ rspec_junit_formatter (0.6.0)
66
84
  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)
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
- 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)
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.25)
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
- codecov
93
- dotenv
94
- pry
95
- rake
139
+ pry (~> 0.14)
140
+ rake (~> 13.2)
96
141
  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
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.1.4
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/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
+ ![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 | 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