Sha256: 47d8eba49596da4301b0ec7d835f364aff8b3f4e6935dc3a297d5c75be1a789f

Contents?: true

Size: 1.66 KB

Versions: 6

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

require_relative '../rest_client'
require 'exception/conversion_error'

module Uploadcare
  module Client
    module Conversion
      # This is a base client for conversion operations
      #
      # @see https://uploadcare.com/api-refs/rest-api/v0.6.0/#tag/Conversion
      class BaseConversionClient < RestClient
        API_VERSION_HEADER_VALUE = 'application/vnd.uploadcare-v0.5+json'

        def headers
          {
            'Content-Type': 'application/json',
            'Accept': API_VERSION_HEADER_VALUE,
            'User-Agent': Uploadcare::Param::UserAgent.call
          }
        end

        private

        def send_convert_request(arr, options, url_builder_class)
          body = build_body_for_many(arr, options, url_builder_class)
          post(uri: convert_uri, content: body)
        end

        def success(response)
          body = response.body.to_s
          extract_result(body)
        end

        def extract_result(response_body)
          return if response_body.empty?

          parsed_body = JSON.parse(response_body, symbolize_names: true)
          errors = parsed_body[:error] || parsed_body[:problems]
          return Dry::Monads::Failure(errors) unless errors.nil? || errors.empty?

          Dry::Monads::Success(parsed_body)
        end

        # Prepares body for convert_many method
        def build_body_for_many(arr, options, url_builder_class)
          {
            paths: arr.map do |params|
              url_builder_class.call(
                **build_paths_body(params)
              )
            end,
            store: options[:store]
          }.compact.to_json
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
uploadcare-ruby-3.3.2 lib/uploadcare/client/conversion/base_conversion_client.rb
uploadcare-ruby-3.3.1 lib/uploadcare/client/conversion/base_conversion_client.rb
uploadcare-ruby-3.3.0 lib/uploadcare/client/conversion/base_conversion_client.rb
uploadcare-ruby-3.2.0 lib/uploadcare/client/conversion/base_conversion_client.rb
uploadcare-ruby-3.1.1 lib/uploadcare/client/conversion/base_conversion_client.rb
uploadcare-ruby-3.1.0 lib/uploadcare/client/conversion/base_conversion_client.rb