remove_bg 1.2.1 → 1.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ef796828448e7ea7be4a637c714439ee0cc13a3fcd018a6789fbf36761b3b8d
4
- data.tar.gz: c0ce6e0782c56318c92f83263c5c99ee13923248b680af9db3bb9d5e18650da4
3
+ metadata.gz: 85d740935b627c71987091c6b9476d49a17fa2a63cea2844f53a3bb0e7d9621b
4
+ data.tar.gz: d9ff54349e69003ecea536d4b669ca4479d4a7cd1c0e2a8577e0b8467cbbcdb2
5
5
  SHA512:
6
- metadata.gz: 8df9c7d09a27bf7a8ad83cddaebebb497cc13970db2779a578bc815b7637c94c61a49229efd3eae0e009c2f25171327f83890dff4a8cad6d34bf47677f1850a2
7
- data.tar.gz: fb007096bffd1f48d65ffa8db7a38ff6c6b60d2c3804d6b8ec4e941b607cabc40d900ef9fd275976369e924ef14421fac46b32228007a4b0b1b191d72283765f
6
+ metadata.gz: 18b21fbf48e49447b44eb950b5846e3de1e3ebf52c49aae4ae207870a8367aad61d7eded97709e371c06538830c9a73e86f4af7d7106c62c66d938a86a525bae
7
+ data.tar.gz: '079fb2498670102258b88d47d93a665ee47b318b8b307316b8c093f3e47572ec975c814c9d4f237d48dc38e5a196ade29eaa4452db976ed7a238487a3ec6878c'
data/Appraisals CHANGED
@@ -1,11 +1,14 @@
1
- appraise "faraday-0-14" do
2
- gem "faraday", "~> 0.14"
1
+ # Oldest supported Faraday version
2
+ appraise "faraday-0-15" do
3
+ gem "faraday", "~> 0.15.0"
3
4
  end
4
5
 
5
- appraise "faraday-0-15" do
6
- gem "faraday", "~> 0.15"
6
+ # Latest in Faraday 0.x series
7
+ appraise "faraday-0-x" do
8
+ gem "faraday", ">= 0.15", "<= 1.0"
7
9
  end
8
10
 
9
- appraise "faraday-1rc1" do
10
- gem "faraday", "1.0.0.pre.rc1"
11
+ # Latest in Faraday 1.x series
12
+ appraise "faraday-1-x" do
13
+ gem "faraday", "~> 1.0"
11
14
  end
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.0
4
+
5
+ - Add `RemoveBg.account_info` which includes available credits - via [#9](https://github.com/remove-bg/ruby/pull/9)
6
+ - Fix support for Faraday `1.0` - via [#7](https://github.com/remove-bg/ruby/pull/7)
7
+ - Raise minimum Faraday version to `0.15.0`
8
+
3
9
  ## 1.2.1
4
10
 
5
11
  - Add `type` attribute to result object (`X-Type` header from API) - via [#2](https://github.com/remove-bg/ruby/pull/2)
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- remove_bg (1.2.1)
5
- faraday (>= 0.14, < 2)
4
+ remove_bg (1.3.0)
5
+ faraday (>= 0.15, < 2)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
@@ -18,7 +18,7 @@ GEM
18
18
  safe_yaml (~> 1.0.0)
19
19
  diff-lcs (1.3)
20
20
  dotenv (2.7.1)
21
- faraday (0.17.0)
21
+ faraday (1.0.1)
22
22
  multipart-post (>= 1.2, < 3)
23
23
  hashdiff (0.3.8)
24
24
  method_source (0.9.2)
data/README.md CHANGED
@@ -69,6 +69,22 @@ result.save("processed/image.png")
69
69
  result.save("image.png", overwrite: true) # Careful!
70
70
  ```
71
71
 
72
+ ### Fetching account information
73
+
74
+ To display the [account information][account-info] for the currently configured
75
+ API key:
76
+
77
+ [account-info]: https://www.remove.bg/api#operations-tag-Fetch_account_info
78
+
79
+ ```ruby
80
+ account = RemoveBg.account_info # If an API key is set via RemoveBg.configuration
81
+ # or
82
+ account = RemoveBg.account_info(api_key: "<api_key>")
83
+
84
+ account.api.free_calls # => 50
85
+ account.credits.total # => 200
86
+ ```
87
+
72
88
  ## Examples
73
89
 
74
90
  - [Bulk processing][bulk-processing] a directory of JPG and PNG files
@@ -2,6 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "faraday", "~> 0.15"
5
+ gem "faraday", "~> 0.15.0"
6
6
 
7
7
  gemspec path: "../"
@@ -2,6 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "faraday", "1.0.0.pre.rc1"
5
+ gem "faraday", ">= 0.15", "<= 1.0"
6
6
 
7
7
  gemspec path: "../"
@@ -2,6 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "faraday", "~> 0.14"
5
+ gem "faraday", "~> 1.0"
6
6
 
7
7
  gemspec path: "../"
@@ -14,6 +14,11 @@ module RemoveBg
14
14
  ApiClient.new.remove_from_url(image_url, options)
15
15
  end
16
16
 
17
+ def self.account_info(raw_options = {})
18
+ options = RemoveBg::BaseRequestOptions.new(raw_options)
19
+ ApiClient.new.account_info(options)
20
+ end
21
+
17
22
  def self.configure
18
23
  yield RemoveBg::Configuration.configuration
19
24
  end
@@ -0,0 +1,30 @@
1
+ module RemoveBg
2
+ class AccountInfo
3
+ attr_reader :api, :credits
4
+
5
+ def initialize(attributes)
6
+ @api = ApiInfo.new(**attributes.fetch(:api))
7
+ @credits = CreditsInfo.new(**attributes.fetch(:credits))
8
+ end
9
+
10
+ class ApiInfo
11
+ attr_reader :free_calls, :sizes
12
+
13
+ def initialize(free_calls:, sizes:)
14
+ @free_calls = free_calls
15
+ @sizes = sizes
16
+ end
17
+ end
18
+
19
+ class CreditsInfo
20
+ attr_reader :total, :subscription, :payg, :enterprise
21
+
22
+ def initialize(total:, subscription:, payg:, enterprise:)
23
+ @total = total
24
+ @subscription = subscription
25
+ @payg = payg
26
+ @enterprise = enterprise
27
+ end
28
+ end
29
+ end
30
+ end
@@ -2,6 +2,9 @@ module RemoveBg
2
2
  module Api
3
3
  URL = "https://api.remove.bg"
4
4
 
5
+ V1_ACCOUNT = "/v1.0/account"
6
+ private_constant :V1_ACCOUNT
7
+
5
8
  V1_REMOVE_BG = "/v1.0/removebg"
6
9
  private_constant :V1_REMOVE_BG
7
10
 
@@ -1,4 +1,5 @@
1
1
  require "json"
2
+ require_relative "account_info"
2
3
  require_relative "api"
3
4
  require_relative "error"
4
5
  require_relative "http_connection"
@@ -25,6 +26,10 @@ module RemoveBg
25
26
  request_remove_bg(data, options.api_key)
26
27
  end
27
28
 
29
+ def account_info(options)
30
+ request_account_info(options.api_key)
31
+ end
32
+
28
33
  private
29
34
 
30
35
  attr_reader :connection
@@ -34,9 +39,27 @@ module RemoveBg
34
39
  req.headers[HEADER_API_KEY] = api_key
35
40
  end
36
41
 
42
+ if response.status == 200
43
+ parse_image_result(response)
44
+ else
45
+ handle_http_error(response)
46
+ end
47
+ end
48
+
49
+ def request_account_info(api_key)
50
+ response = connection.get(V1_ACCOUNT) do |req|
51
+ req.headers[HEADER_API_KEY] = api_key
52
+ end
53
+
54
+ if response.status == 200
55
+ parse_account_result(response)
56
+ else
57
+ handle_http_error(response)
58
+ end
59
+ end
60
+
61
+ def handle_http_error(response)
37
62
  case response.status
38
- when 200
39
- parse_result(response)
40
63
  when 400..499
41
64
  error_message = parse_error_message(response)
42
65
  raise RemoveBg::ClientHttpError.new(error_message, response)
@@ -48,7 +71,7 @@ module RemoveBg
48
71
  end
49
72
  end
50
73
 
51
- def parse_result(response)
74
+ def parse_image_result(response)
52
75
  RemoveBg::Result.new(
53
76
  data: response.body,
54
77
  type: response.headers[HEADER_TYPE],
@@ -58,6 +81,14 @@ module RemoveBg
58
81
  )
59
82
  end
60
83
 
84
+ def parse_account_result(response)
85
+ attributes = JSON.parse(response.body, symbolize_names: true)
86
+ .fetch(:data)
87
+ .fetch(:attributes)
88
+
89
+ RemoveBg::AccountInfo.new(attributes)
90
+ end
91
+
61
92
  def parse_error_message(response)
62
93
  parse_errors(response).first["title"]
63
94
  end
@@ -0,0 +1,32 @@
1
+ require_relative "error"
2
+
3
+ module RemoveBg
4
+ class BaseRequestOptions
5
+ attr_reader :api_key, :data
6
+
7
+ def initialize(raw_options = {})
8
+ options = raw_options.dup
9
+ @api_key = resolve_api_key(options.delete(:api_key))
10
+ @data = options
11
+ end
12
+
13
+ private
14
+
15
+ def resolve_api_key(request_api_key)
16
+ api_key = request_api_key || global_api_key
17
+
18
+ if api_key.nil? || api_key.empty?
19
+ raise RemoveBg::Error, <<~MSG
20
+ Please configure an API key or specify one per request. API key was:
21
+ #{api_key.inspect}
22
+ MSG
23
+ end
24
+
25
+ api_key
26
+ end
27
+
28
+ def global_api_key
29
+ RemoveBg::Configuration.configuration.api_key
30
+ end
31
+ end
32
+ end
@@ -1,7 +1,7 @@
1
- require_relative "error"
1
+ require_relative "base_request_options"
2
2
 
3
3
  module RemoveBg
4
- class RequestOptions
4
+ class RequestOptions < BaseRequestOptions
5
5
  SIZE_REGULAR = "regular"
6
6
  SIZE_MEDIUM = "medium"
7
7
  SIZE_HD = "hd"
@@ -15,32 +15,10 @@ module RemoveBg
15
15
  CHANNELS_RGBA = "rgba"
16
16
  CHANNELS_ALPHA = "alpha"
17
17
 
18
- attr_reader :api_key, :data
19
-
20
18
  def initialize(raw_options = {})
21
19
  options = raw_options.dup
22
20
  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
21
+ super(options)
44
22
  end
45
23
  end
46
24
  end
@@ -1,4 +1,4 @@
1
- require "faraday/upload_io"
1
+ require "faraday"
2
2
  require_relative "error"
3
3
 
4
4
  module RemoveBg
@@ -9,7 +9,7 @@ module RemoveBg
9
9
  end
10
10
 
11
11
  content_type = determine_content_type(file_path)
12
- Faraday::UploadIO.new(file_path, content_type)
12
+ FARADAY_FILE.new(file_path, content_type)
13
13
  end
14
14
 
15
15
  def self.determine_content_type(file_path)
@@ -22,5 +22,9 @@ module RemoveBg
22
22
  end
23
23
 
24
24
  private_class_method :determine_content_type
25
+
26
+ # UploadIO for Faraday < 0.16.0
27
+ FARADAY_FILE = defined?(Faraday::FilePart) ? Faraday::FilePart : Faraday::UploadIO
28
+ private_constant :FARADAY_FILE
25
29
  end
26
30
  end
@@ -1,3 +1,3 @@
1
1
  module RemoveBg
2
- VERSION = "1.2.1"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
24
  spec.require_paths = ["lib"]
25
25
 
26
- spec.add_dependency "faraday", ">= 0.14", "< 2"
26
+ spec.add_dependency "faraday", ">= 0.15", "< 2"
27
27
 
28
28
  spec.add_development_dependency "appraisal"
29
29
  spec.add_development_dependency "bundler", "~> 1.17"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remove_bg
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oliver Peate
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-08 00:00:00.000000000 Z
11
+ date: 2020-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0.14'
19
+ version: '0.15'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '2'
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '0.14'
29
+ version: '0.15'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '2'
@@ -190,12 +190,14 @@ files:
190
190
  - Rakefile
191
191
  - bin/console
192
192
  - bin/setup
193
- - gemfiles/faraday_0_14.gemfile
194
193
  - gemfiles/faraday_0_15.gemfile
195
- - gemfiles/faraday_1rc1.gemfile
194
+ - gemfiles/faraday_0_x.gemfile
195
+ - gemfiles/faraday_1_x.gemfile
196
196
  - lib/remove_bg.rb
197
+ - lib/remove_bg/account_info.rb
197
198
  - lib/remove_bg/api.rb
198
199
  - lib/remove_bg/api_client.rb
200
+ - lib/remove_bg/base_request_options.rb
199
201
  - lib/remove_bg/configuration.rb
200
202
  - lib/remove_bg/error.rb
201
203
  - lib/remove_bg/http_connection.rb
@@ -227,7 +229,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
227
229
  - !ruby/object:Gem::Version
228
230
  version: '0'
229
231
  requirements: []
230
- rubygems_version: 3.0.6
232
+ rubygems_version: 3.0.3
231
233
  signing_key:
232
234
  specification_version: 4
233
235
  summary: Remove image background - 100% automatically