Sha256: 1fbd641bd5833d999ccd71981716965f4fb78004e74196b5fb1392c72e67d25f

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require "roseflow/stabilityai/responses/text_to_image_response"
require "roseflow/stabilityai/responses/image_to_image_response"
require "roseflow/stabilityai/responses/upscale_response"
require "roseflow/stabilityai/responses/masking_response"
require "roseflow/stabilityai/responses/error_response"

module Roseflow
  module StabilityAI
    class ResponseHandler
      def initialize(operation, response)
        @operation = operation
        @response = response
      end

      def call
        if response.success?
          handle_successful_response
        else
          handle_error_response
        end
      end

      private

      attr_reader :operation, :response

      def handle_successful_response
        json = JSON.parse(response.body)

        case operation.type
        when :text_to_image
          Responses::TextToImageResponse.new(json)
        when :image_to_image
          Responses::ImageToImageResponse.new(json)
        when :upscale
          Responses::UpscaleResponse.new(json)
        when :masking
          Responses::MaskingResponse.new(json)
        else
          raise ArgumentError, "Invalid operation: #{operation.type}"
        end
      end

      def handle_error_response
        puts response.status
        puts JSON.parse(response.body)
        Responses::ErrorResponse.from(
          code: response.status,
          operation: operation.type,
          body: JSON.parse(response.body),
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
roseflow-stabilityai-0.1.0 lib/roseflow/stabilityai/response_handler.rb