Sha256: 437efd89f630925e4fa538582a481e07c9b17959a10bf48e0742290c17fac112

Contents?: true

Size: 1002 Bytes

Versions: 1

Compression:

Stored size: 1002 Bytes

Contents

# frozen_string_literal: true

require_relative "openai_image_generator/version"

module OpenaiImageGenerator
  class Error < StandardError; end

  class Client
    API_URL = URI.parse("https://api.openai.com/v1/images/generations".freeze)

    attr_accessor :api_key, :prompt, :number_images, :size

    def initialize(api_key)
      @api_key = api_key
    end

    def generate(prompt, number_images, size)

      response = Net::HTTP.start(API_URL.host, API_URL.port, use_ssl: true) do |http|
        request = Net::HTTP::Post.new(API_URL.request_uri, header)
        request.body = body(prompt, number_images, size).to_json
        http.request(request)
      end

      JSON.parse(response.body)
    end

    private

    def body(prompt, number_images, size)
      {
        prompt: prompt,
        n: number_images || 1,
        size: size
      }
    end

    def header
      {
        'Content-Type': 'application/json',
        'Authorization': "Bearer #{api_key}"
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
openai_image_generator-0.0.1 lib/openai_image_generator.rb