remove_bg 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +61 -0
- data/.env.test +2 -0
- data/.gitignore +20 -0
- data/.rspec +2 -0
- data/Appraisals +7 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +70 -0
- data/LICENSE +21 -0
- data/README.md +95 -0
- data/Rakefile +6 -0
- data/bin/console +10 -0
- data/bin/setup +8 -0
- data/gemfiles/faraday_0_14.gemfile +7 -0
- data/gemfiles/faraday_0_15.gemfile +7 -0
- data/lib/remove_bg.rb +20 -0
- data/lib/remove_bg/api.rb +20 -0
- data/lib/remove_bg/api_client.rb +70 -0
- data/lib/remove_bg/configuration.rb +13 -0
- data/lib/remove_bg/error.rb +45 -0
- data/lib/remove_bg/http_connection.rb +40 -0
- data/lib/remove_bg/request_options.rb +46 -0
- data/lib/remove_bg/result.rb +22 -0
- data/lib/remove_bg/upload.rb +26 -0
- data/lib/remove_bg/url_validator.rb +18 -0
- data/lib/remove_bg/version.rb +3 -0
- data/remove_bg.gemspec +37 -0
- data/resources/logo_black.png +0 -0
- metadata +213 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a7f50d14e1a986537d272bbe7f283a079456533f97af5892bd215570d19d6a7d
|
4
|
+
data.tar.gz: 5cecc0da93cf064be92d43869c5a5956f7db0014deb43a6b6c50e4f778fc2422
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dea6369641d46b28893ae73518447e146bc8705907ad399082d0824bc22bb9b7a09cb0748f76e8fed404336f6de4860ce8c7c6364247ce4bd84e9248d6ab7148
|
7
|
+
data.tar.gz: 4c2c67933d84c3850b7b8e9ca77fadf39f8e1a5476f92c2c7fe93deafc6b624b1ed0d47d3b2fcd371f7aea3d70520f1661a6f65a8e319be79a3bf42bc5f0c2b8
|
@@ -0,0 +1,61 @@
|
|
1
|
+
---
|
2
|
+
version: 2
|
3
|
+
|
4
|
+
base_job: &base_job
|
5
|
+
steps:
|
6
|
+
- checkout
|
7
|
+
|
8
|
+
- restore_cache:
|
9
|
+
keys:
|
10
|
+
- v1-dependencies-{{ checksum "Gemfile.lock" }}
|
11
|
+
|
12
|
+
- run:
|
13
|
+
name: install dependencies
|
14
|
+
command: bundle install --jobs=4 --retry=3 --path vendor/bundle
|
15
|
+
|
16
|
+
- save_cache:
|
17
|
+
paths:
|
18
|
+
- ./vendor/bundle
|
19
|
+
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
|
20
|
+
|
21
|
+
- run:
|
22
|
+
name: run tests
|
23
|
+
command: |
|
24
|
+
bundle exec rspec --format progress \
|
25
|
+
--format RspecJunitFormatter \
|
26
|
+
--out test-results/rspec.xml
|
27
|
+
|
28
|
+
- run:
|
29
|
+
name: run appraisals
|
30
|
+
command: |
|
31
|
+
bundle exec appraisal install --jobs 4 --retry 3
|
32
|
+
bundle exec appraisal rspec
|
33
|
+
|
34
|
+
- store_test_results:
|
35
|
+
path: test-results
|
36
|
+
|
37
|
+
################################################################################
|
38
|
+
|
39
|
+
jobs:
|
40
|
+
ruby-2.4:
|
41
|
+
<<: *base_job
|
42
|
+
docker:
|
43
|
+
- image: circleci/ruby:2.4
|
44
|
+
ruby-2.5:
|
45
|
+
<<: *base_job
|
46
|
+
docker:
|
47
|
+
- image: circleci/ruby:2.5
|
48
|
+
ruby-2.6:
|
49
|
+
<<: *base_job
|
50
|
+
docker:
|
51
|
+
- image: circleci/ruby:2.6
|
52
|
+
|
53
|
+
################################################################################
|
54
|
+
|
55
|
+
workflows:
|
56
|
+
version: 2
|
57
|
+
multiple-rubies:
|
58
|
+
jobs:
|
59
|
+
- ruby-2.4
|
60
|
+
- ruby-2.5
|
61
|
+
- ruby-2.6
|
data/.env.test
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/_yardoc/
|
4
|
+
/coverage/
|
5
|
+
/doc/
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/tmp/
|
9
|
+
|
10
|
+
# rspec failure tracking
|
11
|
+
spec/examples.txt
|
12
|
+
|
13
|
+
.env.*.local
|
14
|
+
|
15
|
+
# CircleCI / RSpec JUnit formatter
|
16
|
+
test-results
|
17
|
+
|
18
|
+
gemfiles/*.gemfile.lock
|
19
|
+
|
20
|
+
examples/output
|
data/.rspec
ADDED
data/Appraisals
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
remove_bg (1.0.0)
|
5
|
+
faraday (~> 0.14)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.6.0)
|
11
|
+
public_suffix (>= 2.0.2, < 4.0)
|
12
|
+
appraisal (2.2.0)
|
13
|
+
bundler
|
14
|
+
rake
|
15
|
+
thor (>= 0.14.0)
|
16
|
+
coderay (1.1.2)
|
17
|
+
crack (0.4.3)
|
18
|
+
safe_yaml (~> 1.0.0)
|
19
|
+
diff-lcs (1.3)
|
20
|
+
dotenv (2.7.1)
|
21
|
+
faraday (0.15.4)
|
22
|
+
multipart-post (>= 1.2, < 3)
|
23
|
+
hashdiff (0.3.8)
|
24
|
+
method_source (0.9.2)
|
25
|
+
multipart-post (2.0.0)
|
26
|
+
pry (0.12.2)
|
27
|
+
coderay (~> 1.1.0)
|
28
|
+
method_source (~> 0.9.0)
|
29
|
+
public_suffix (3.0.3)
|
30
|
+
rake (12.3.2)
|
31
|
+
rspec (3.8.0)
|
32
|
+
rspec-core (~> 3.8.0)
|
33
|
+
rspec-expectations (~> 3.8.0)
|
34
|
+
rspec-mocks (~> 3.8.0)
|
35
|
+
rspec-core (3.8.0)
|
36
|
+
rspec-support (~> 3.8.0)
|
37
|
+
rspec-expectations (3.8.2)
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
+
rspec-support (~> 3.8.0)
|
40
|
+
rspec-mocks (3.8.0)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.8.0)
|
43
|
+
rspec-support (3.8.0)
|
44
|
+
rspec_junit_formatter (0.4.1)
|
45
|
+
rspec-core (>= 2, < 4, != 2.12.0)
|
46
|
+
safe_yaml (1.0.5)
|
47
|
+
thor (0.20.3)
|
48
|
+
vcr (4.0.0)
|
49
|
+
webmock (3.5.1)
|
50
|
+
addressable (>= 2.3.6)
|
51
|
+
crack (>= 0.3.2)
|
52
|
+
hashdiff
|
53
|
+
|
54
|
+
PLATFORMS
|
55
|
+
ruby
|
56
|
+
|
57
|
+
DEPENDENCIES
|
58
|
+
appraisal
|
59
|
+
bundler (~> 1.17)
|
60
|
+
dotenv
|
61
|
+
pry
|
62
|
+
rake (~> 12.0)
|
63
|
+
remove_bg!
|
64
|
+
rspec (~> 3.8)
|
65
|
+
rspec_junit_formatter
|
66
|
+
vcr
|
67
|
+
webmock
|
68
|
+
|
69
|
+
BUNDLED WITH
|
70
|
+
1.17.3
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019 remove.bg
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
[![RemoveBG](resources/logo_black.png)](https://www.remove.bg/)
|
2
|
+
|
3
|
+
# Ruby library
|
4
|
+
|
5
|
+
[![CircleCI](https://circleci.com/gh/remove-bg/ruby/tree/master.svg?style=shield)](https://circleci.com/gh/remove-bg/ruby/tree/master)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
- Add `gem "remove_bg"` to your application's Gemfile and then execute `bundle`.
|
10
|
+
- Or install it yourself as: `gem install remove_bg`
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
### Configuring an API key
|
15
|
+
|
16
|
+
To configure a global API key (used by default unless overridden per request):
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
RemoveBg.configure do |config|
|
20
|
+
config.api_key = "<api-key>"
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
It's not recommended to commit your API key to version control. You may want to
|
25
|
+
read the API key from an environment variable (e.g.
|
26
|
+
`ENV.fetch("REMOVE_BG_API_KEY")`) or find an alternative method.
|
27
|
+
|
28
|
+
### Removing the background from an image
|
29
|
+
|
30
|
+
Currently the gem supports removing the background from a file or a URL:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
RemoveBg.from_file("image.png")
|
34
|
+
RemoveBg.from_url("http://example.com/image.png")
|
35
|
+
```
|
36
|
+
|
37
|
+
#### Request options
|
38
|
+
|
39
|
+
The processing options outlined in the [API reference](https://www.remove.bg/api)
|
40
|
+
can be specified per request:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
RemoveBg.from_file("image.png", size: "hd", type: "product", channels: "rgba")
|
44
|
+
```
|
45
|
+
|
46
|
+
The API key can also be specified per request:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
RemoveBg.from_file("image.png", api_key: "<api-key>")
|
50
|
+
```
|
51
|
+
|
52
|
+
#### Handling the result
|
53
|
+
|
54
|
+
Background removal requests return a result object which includes the processed
|
55
|
+
image data and the metadata about the operation.
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
result = RemoveBg.from_file("image.png")
|
59
|
+
result.data # => "\x89PNG..."
|
60
|
+
result.height # => 333
|
61
|
+
result.width # => 500
|
62
|
+
result.credits_charged # => 1
|
63
|
+
```
|
64
|
+
|
65
|
+
There's also a `#save` convenience method:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
result.save("processed/image.png")
|
69
|
+
result.save("image.png", overwrite: true) # Careful!
|
70
|
+
```
|
71
|
+
|
72
|
+
## Examples
|
73
|
+
|
74
|
+
- [Bulk processing][bulk-processing] a directory of JPG and PNG files
|
75
|
+
|
76
|
+
[bulk-processing]: https://github.com/remove-bg/ruby/blob/master/examples/bulk_process.rb
|
77
|
+
|
78
|
+
## License
|
79
|
+
|
80
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
81
|
+
|
82
|
+
## Contributing
|
83
|
+
|
84
|
+
Bug reports and pull requests are welcome on GitHub at [remove-bg/ruby](https://github.com/remove-bg/ruby).
|
85
|
+
|
86
|
+
## Development
|
87
|
+
|
88
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
89
|
+
`rake spec` to run the tests.
|
90
|
+
|
91
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
92
|
+
release a new version, update the version number in `version.rb`, and then run
|
93
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
94
|
+
git commits and tags, and push the `.gem` file to
|
95
|
+
[rubygems.org](https://rubygems.org).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/lib/remove_bg.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "remove_bg/version"
|
2
|
+
require "remove_bg/api_client"
|
3
|
+
require "remove_bg/configuration"
|
4
|
+
require "remove_bg/request_options"
|
5
|
+
|
6
|
+
module RemoveBg
|
7
|
+
def self.from_file(image_path, raw_options = {})
|
8
|
+
options = RemoveBg::RequestOptions.new(raw_options)
|
9
|
+
ApiClient.new.remove_from_file(image_path, options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.from_url(image_url, raw_options = {})
|
13
|
+
options = RemoveBg::RequestOptions.new(raw_options)
|
14
|
+
ApiClient.new.remove_from_url(image_url, options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.configure
|
18
|
+
yield RemoveBg::Configuration.configuration
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module RemoveBg
|
2
|
+
module Api
|
3
|
+
URL = "https://api.remove.bg"
|
4
|
+
|
5
|
+
V1_REMOVE_BG = "/v1.0/removebg"
|
6
|
+
private_constant :V1_REMOVE_BG
|
7
|
+
|
8
|
+
HEADER_API_KEY = "X-Api-Key"
|
9
|
+
private_constant :HEADER_API_KEY
|
10
|
+
|
11
|
+
HEADER_WIDTH = "X-Width"
|
12
|
+
private_constant :HEADER_WIDTH
|
13
|
+
|
14
|
+
HEADER_HEIGHT = "X-Height"
|
15
|
+
private_constant :HEADER_HEIGHT
|
16
|
+
|
17
|
+
HEADER_CREDITS_CHARGED = "X-Credits-Charged"
|
18
|
+
private_constant :HEADER_CREDITS_CHARGED
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require "json"
|
2
|
+
require_relative "api"
|
3
|
+
require_relative "error"
|
4
|
+
require_relative "http_connection"
|
5
|
+
require_relative "result"
|
6
|
+
require_relative "upload"
|
7
|
+
require_relative "url_validator"
|
8
|
+
|
9
|
+
module RemoveBg
|
10
|
+
class ApiClient
|
11
|
+
include RemoveBg::Api
|
12
|
+
|
13
|
+
def initialize(connection: RemoveBg::HttpConnection.build)
|
14
|
+
@connection = connection
|
15
|
+
end
|
16
|
+
|
17
|
+
def remove_from_file(image_path, options)
|
18
|
+
data = options.data.merge(image_file: Upload.for_file(image_path))
|
19
|
+
request_remove_bg(data, options.api_key)
|
20
|
+
end
|
21
|
+
|
22
|
+
def remove_from_url(image_url, options)
|
23
|
+
RemoveBg::UrlValidator.validate(image_url)
|
24
|
+
data = options.data.merge(image_url: image_url)
|
25
|
+
request_remove_bg(data, options.api_key)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
attr_reader :connection
|
31
|
+
|
32
|
+
def request_remove_bg(data, api_key)
|
33
|
+
response = connection.post(V1_REMOVE_BG, data) do |req|
|
34
|
+
req.headers[HEADER_API_KEY] = api_key
|
35
|
+
end
|
36
|
+
|
37
|
+
case response.status
|
38
|
+
when 200
|
39
|
+
parse_result(response)
|
40
|
+
when 400..499
|
41
|
+
error_message = parse_error_message(response)
|
42
|
+
raise RemoveBg::ClientHttpError.new(error_message, response)
|
43
|
+
when 500..599
|
44
|
+
error_message = parse_error_message(response)
|
45
|
+
raise RemoveBg::ServerHttpError.new(error_message, response)
|
46
|
+
else
|
47
|
+
raise RemoveBg::HttpError.new("An unknown error occurred", response)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def parse_result(response)
|
52
|
+
RemoveBg::Result.new(
|
53
|
+
data: response.body,
|
54
|
+
width: response.headers[HEADER_WIDTH]&.to_i,
|
55
|
+
height: response.headers[HEADER_HEIGHT]&.to_i,
|
56
|
+
credits_charged: response.headers[HEADER_CREDITS_CHARGED]&.to_i,
|
57
|
+
)
|
58
|
+
end
|
59
|
+
|
60
|
+
def parse_error_message(response)
|
61
|
+
parse_errors(response).first["title"]
|
62
|
+
end
|
63
|
+
|
64
|
+
def parse_errors(response)
|
65
|
+
JSON.parse(response.body)["errors"] || []
|
66
|
+
rescue JSON::ParserError
|
67
|
+
[{ "title" => "Unable to parse response" }]
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module RemoveBg
|
2
|
+
class Error < StandardError; end
|
3
|
+
|
4
|
+
class HttpError < Error
|
5
|
+
attr_reader :http_response
|
6
|
+
|
7
|
+
def initialize(message, http_response)
|
8
|
+
@http_response = http_response
|
9
|
+
super(message)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class ClientHttpError < HttpError; end
|
14
|
+
class ServerHttpError < HttpError; end
|
15
|
+
|
16
|
+
class FileError < Error
|
17
|
+
attr_reader :file_path
|
18
|
+
|
19
|
+
def initialize(message, file_path)
|
20
|
+
@file_path = file_path
|
21
|
+
super(message)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class FileMissingError < FileError
|
26
|
+
def initialize(file_path)
|
27
|
+
super("The file doesn't exist: '#{file_path}'", file_path)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class FileOverwriteError < FileError
|
32
|
+
def initialize(file_path)
|
33
|
+
super("The file already exists: '#{file_path}' (specify #save(overwrite: true) to ignore)", file_path)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class InvalidUrlError < Error
|
38
|
+
attr_reader :url
|
39
|
+
|
40
|
+
def initialize(url)
|
41
|
+
@url = url
|
42
|
+
super("Invalid URL: #{url.inspect}")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "faraday"
|
2
|
+
require_relative "api"
|
3
|
+
require_relative "version"
|
4
|
+
|
5
|
+
module RemoveBg
|
6
|
+
class HttpConnection
|
7
|
+
USER_AGENT = "remove-bg-ruby-#{RemoveBg::VERSION}"
|
8
|
+
HTTP_BASE_TIMEOUT = 10
|
9
|
+
HTTP_WRITE_TIMEOUT = 120
|
10
|
+
|
11
|
+
def self.build(api_url = RemoveBg::Api::URL)
|
12
|
+
retry_options = {
|
13
|
+
max: 2,
|
14
|
+
interval: 0.2,
|
15
|
+
backoff_factor: 2,
|
16
|
+
methods: [:post],
|
17
|
+
exceptions: Faraday::Request::Retry::DEFAULT_EXCEPTIONS +
|
18
|
+
[Faraday::ConnectionFailed]
|
19
|
+
}
|
20
|
+
|
21
|
+
request_options = Faraday::RequestOptions.new.tap do |req_options|
|
22
|
+
req_options.timeout = HTTP_BASE_TIMEOUT
|
23
|
+
req_options.open_timeout = HTTP_BASE_TIMEOUT
|
24
|
+
req_options.write_timeout = HTTP_WRITE_TIMEOUT
|
25
|
+
end
|
26
|
+
|
27
|
+
http_options = {
|
28
|
+
headers: { "User-Agent" => USER_AGENT },
|
29
|
+
request: request_options,
|
30
|
+
}
|
31
|
+
|
32
|
+
Faraday.new(api_url, **http_options) do |f|
|
33
|
+
f.request :multipart
|
34
|
+
f.request :url_encoded
|
35
|
+
f.request :retry, retry_options
|
36
|
+
f.adapter :net_http
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative "error"
|
2
|
+
|
3
|
+
module RemoveBg
|
4
|
+
class RequestOptions
|
5
|
+
SIZE_REGULAR = "regular"
|
6
|
+
SIZE_MEDIUM = "medium"
|
7
|
+
SIZE_HD = "hd"
|
8
|
+
SIZE_4K = "4k"
|
9
|
+
SIZE_AUTO = "auto"
|
10
|
+
|
11
|
+
FOREGROUND_TYPE_AUTO = "auto"
|
12
|
+
FOREGROUND_TYPE_PERSON = "person"
|
13
|
+
FOREGROUND_TYPE_PRODUCT = "product"
|
14
|
+
|
15
|
+
CHANNELS_RGBA = "rgba"
|
16
|
+
CHANNELS_ALPHA = "alpha"
|
17
|
+
|
18
|
+
attr_reader :api_key, :data
|
19
|
+
|
20
|
+
def initialize(raw_options = {})
|
21
|
+
options = raw_options.dup
|
22
|
+
options[:size] ||= SIZE_AUTO
|
23
|
+
@api_key = resolve_api_key(options.delete(:api_key))
|
24
|
+
@data = options
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def resolve_api_key(request_api_key)
|
30
|
+
api_key = request_api_key || global_api_key
|
31
|
+
|
32
|
+
if api_key.nil? || api_key.empty?
|
33
|
+
raise RemoveBg::Error, <<~MSG
|
34
|
+
Please configure an API key or specify one per request. API key was:
|
35
|
+
#{api_key.inspect}
|
36
|
+
MSG
|
37
|
+
end
|
38
|
+
|
39
|
+
api_key
|
40
|
+
end
|
41
|
+
|
42
|
+
def global_api_key
|
43
|
+
RemoveBg::Configuration.configuration.api_key
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative "error"
|
2
|
+
|
3
|
+
module RemoveBg
|
4
|
+
class Result
|
5
|
+
attr_reader :data, :width, :height, :credits_charged
|
6
|
+
|
7
|
+
def initialize(data:, width:, height:, credits_charged:)
|
8
|
+
@data = data
|
9
|
+
@width = width
|
10
|
+
@height = height
|
11
|
+
@credits_charged = credits_charged
|
12
|
+
end
|
13
|
+
|
14
|
+
def save(file_path, overwrite: false)
|
15
|
+
if File.exist?(file_path) && !overwrite
|
16
|
+
raise FileOverwriteError.new(file_path)
|
17
|
+
end
|
18
|
+
|
19
|
+
File.write(file_path, data)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "faraday/upload_io"
|
2
|
+
require_relative "error"
|
3
|
+
|
4
|
+
module RemoveBg
|
5
|
+
class Upload
|
6
|
+
def self.for_file(file_path)
|
7
|
+
if !File.exist?(file_path)
|
8
|
+
raise RemoveBg::FileMissingError.new(file_path)
|
9
|
+
end
|
10
|
+
|
11
|
+
content_type = determine_content_type(file_path)
|
12
|
+
Faraday::UploadIO.new(file_path, content_type)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.determine_content_type(file_path)
|
16
|
+
case File.extname(file_path)
|
17
|
+
when ".jpg", ".jpeg" then "image/jpeg"
|
18
|
+
when ".png" then "image/png"
|
19
|
+
else
|
20
|
+
raise RemoveBg::Error, "Unsupported file type (#{file_path})"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private_class_method :determine_content_type
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "uri"
|
2
|
+
require_relative "error"
|
3
|
+
|
4
|
+
module RemoveBg
|
5
|
+
class UrlValidator
|
6
|
+
PERMITTED_SCHEMES = ["http", "https"].freeze
|
7
|
+
|
8
|
+
def self.validate(url)
|
9
|
+
parsed = URI.parse(url)
|
10
|
+
|
11
|
+
unless parsed.absolute? && PERMITTED_SCHEMES.include?(parsed.scheme)
|
12
|
+
raise RemoveBg::InvalidUrlError.new(url)
|
13
|
+
end
|
14
|
+
rescue URI::InvalidURIError
|
15
|
+
raise RemoveBg::InvalidUrlError.new(url)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/remove_bg.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "remove_bg/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "remove_bg"
|
7
|
+
spec.version = RemoveBg::VERSION
|
8
|
+
spec.authors = ["Oliver Peate"]
|
9
|
+
spec.email = ["team@remove.bg"]
|
10
|
+
|
11
|
+
spec.summary = "Remove image background - 100% automatically"
|
12
|
+
spec.homepage = "https://www.remove.bg/"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.metadata["source_code_uri"] = "https://github.com/remove-bg/ruby"
|
15
|
+
spec.metadata["changelog_uri"] = "https://github.com/remove-bg/ruby/blob/master/CHANGELOG.md"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|examples)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_dependency "faraday", "~> 0.14"
|
27
|
+
|
28
|
+
spec.add_development_dependency "appraisal"
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
30
|
+
spec.add_development_dependency "dotenv"
|
31
|
+
spec.add_development_dependency "pry"
|
32
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
33
|
+
spec.add_development_dependency "rspec_junit_formatter"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.8"
|
35
|
+
spec.add_development_dependency "vcr"
|
36
|
+
spec.add_development_dependency "webmock"
|
37
|
+
end
|
Binary file
|
metadata
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: remove_bg
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Oliver Peate
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-04-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.14'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: appraisal
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.17'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.17'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: dotenv
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
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'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '12.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '12.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec_junit_formatter
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.8'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '3.8'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: vcr
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: webmock
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description:
|
154
|
+
email:
|
155
|
+
- team@remove.bg
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- ".circleci/config.yml"
|
161
|
+
- ".env.test"
|
162
|
+
- ".gitignore"
|
163
|
+
- ".rspec"
|
164
|
+
- Appraisals
|
165
|
+
- CHANGELOG.md
|
166
|
+
- Gemfile
|
167
|
+
- Gemfile.lock
|
168
|
+
- LICENSE
|
169
|
+
- README.md
|
170
|
+
- Rakefile
|
171
|
+
- bin/console
|
172
|
+
- bin/setup
|
173
|
+
- gemfiles/faraday_0_14.gemfile
|
174
|
+
- gemfiles/faraday_0_15.gemfile
|
175
|
+
- lib/remove_bg.rb
|
176
|
+
- lib/remove_bg/api.rb
|
177
|
+
- lib/remove_bg/api_client.rb
|
178
|
+
- lib/remove_bg/configuration.rb
|
179
|
+
- lib/remove_bg/error.rb
|
180
|
+
- lib/remove_bg/http_connection.rb
|
181
|
+
- lib/remove_bg/request_options.rb
|
182
|
+
- lib/remove_bg/result.rb
|
183
|
+
- lib/remove_bg/upload.rb
|
184
|
+
- lib/remove_bg/url_validator.rb
|
185
|
+
- lib/remove_bg/version.rb
|
186
|
+
- remove_bg.gemspec
|
187
|
+
- resources/logo_black.png
|
188
|
+
homepage: https://www.remove.bg/
|
189
|
+
licenses:
|
190
|
+
- MIT
|
191
|
+
metadata:
|
192
|
+
source_code_uri: https://github.com/remove-bg/ruby
|
193
|
+
changelog_uri: https://github.com/remove-bg/ruby/blob/master/CHANGELOG.md
|
194
|
+
post_install_message:
|
195
|
+
rdoc_options: []
|
196
|
+
require_paths:
|
197
|
+
- lib
|
198
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
199
|
+
requirements:
|
200
|
+
- - ">="
|
201
|
+
- !ruby/object:Gem::Version
|
202
|
+
version: '0'
|
203
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
204
|
+
requirements:
|
205
|
+
- - ">="
|
206
|
+
- !ruby/object:Gem::Version
|
207
|
+
version: '0'
|
208
|
+
requirements: []
|
209
|
+
rubygems_version: 3.0.3
|
210
|
+
signing_key:
|
211
|
+
specification_version: 4
|
212
|
+
summary: Remove image background - 100% automatically
|
213
|
+
test_files: []
|