Class: OpenaiImageGenerator::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/openai_image_generator.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.



16
17
18
# File 'lib/openai_image_generator.rb', line 16

def initialize(api_key)
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



14
15
16
# File 'lib/openai_image_generator.rb', line 14

def api_key
  @api_key
end

Instance Method Details

#generate(prompt, number_images, size) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/openai_image_generator.rb', line 20

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