class RemoveBg::ApiClient
Attributes
connection[R]
Public Class Methods
new(connection: RemoveBg::HttpConnection.build)
click to toggle source
@param connection [Faraday::Connection]
# File lib/remove_bg/api_client.rb, line 21 def initialize(connection: RemoveBg::HttpConnection.build) @connection = connection end
Public Instance Methods
account_info(options)
click to toggle source
Fetches account information @param options [RemoveBg::BaseRequestOptions] @return [RemoveBg::AccountInfo] @raise [RemoveBg::Error]
# File lib/remove_bg/api_client.rb, line 53 def account_info(options) request_account_info(options.api_key) end
remove_from_file(image_path, options)
click to toggle source
Removes the background from an image on the local file system @param image_path [String] @param options [RemoveBg::RequestOptions] @return [RemoveBg::Result|RemoveBg::CompositeResult] @raise [RemoveBg::Error]
# File lib/remove_bg/api_client.rb, line 31 def remove_from_file(image_path, options) data = options.data.merge(image_file: Upload.for_file(image_path)) request_remove_bg(data, options.api_key) end
remove_from_url(image_url, options)
click to toggle source
Removes the background from the image at the URL specified @param image_url [String] @param options [RemoveBg::RequestOptions] @return [RemoveBg::Result|RemoveBg::CompositeResult] @raise [RemoveBg::Error]
# File lib/remove_bg/api_client.rb, line 42 def remove_from_url(image_url, options) RemoveBg::UrlValidator.validate(image_url) data = options.data.merge(image_url: image_url) request_remove_bg(data, options.api_key) end
Private Instance Methods
handle_http_error(response:, body:)
click to toggle source
# File lib/remove_bg/api_client.rb, line 108 def handle_http_error(response:, body:) error_message = parse_error_message(body) case response.status when 429 rate_limit = RateLimitInfo.new(response.headers) raise RemoveBg::RateLimitError.new(error_message, response, body, rate_limit) when 400..499 raise RemoveBg::ClientHttpError.new(error_message, response, body) when 500..599 raise RemoveBg::ServerHttpError.new(error_message, response, body) else raise RemoveBg::HttpError.new("An unknown error occurred", response, body) end end
parse_account_result(response)
click to toggle source
# File lib/remove_bg/api_client.rb, line 140 def parse_account_result(response) attributes = JSON.parse(response.body, symbolize_names: true) .fetch(:data) .fetch(:attributes) RemoveBg::AccountInfo.new(attributes) end
parse_error_message(response_body)
click to toggle source
# File lib/remove_bg/api_client.rb, line 148 def parse_error_message(response_body) parse_errors(response_body).first["title"] end
parse_errors(response_body)
click to toggle source
# File lib/remove_bg/api_client.rb, line 152 def parse_errors(response_body) JSON.parse(response_body)["errors"] || [] rescue JSON::ParserError [{ "title" => "Unable to parse response" }] end
parse_image_result(headers:, download:)
click to toggle source
# File lib/remove_bg/api_client.rb, line 124 def parse_image_result(headers:, download:) result_for_content_type(headers["Content-Type"]).new( download: download, metadata: ResultMetadata.new(headers), rate_limit: RateLimitInfo.new(headers) ) end
request_account_info(api_key)
click to toggle source
# File lib/remove_bg/api_client.rb, line 96 def request_account_info(api_key) response = connection.get(V1_ACCOUNT) do |req| req.headers[HEADER_API_KEY] = api_key end if response.status == 200 parse_account_result(response) else handle_http_error(response: response, body: response.body) end end
request_remove_bg(data, api_key)
click to toggle source
# File lib/remove_bg/api_client.rb, line 61 def request_remove_bg(data, api_key) download = Tempfile.new("remove-bg-download") download.binmode streaming = false response = connection.post(V1_REMOVE_BG, data) do |req| req.headers[HEADER_API_KEY] = api_key # Faraday v0.16 & v1.0+ support streaming, v0.17 did not (rollback release) if req.options.respond_to?(:on_data) streaming = true req.options.on_data = Proc.new do |chunk, _| download.write(chunk) end end end # Faraday v0.15 / v0.17 if !streaming download.write(response.body) end download.rewind if response.status == 200 parse_image_result(headers: response.headers, download: download) else response_body = download.read download.close download.unlink handle_http_error(response: response, body: response_body) end end
result_for_content_type(content_type)
click to toggle source
# File lib/remove_bg/api_client.rb, line 132 def result_for_content_type(content_type) if content_type&.include?("application/zip") CompositeResult else Result end end