Class: AI::Items

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

Overview

NOTE: This is intentionally not a SimpleDelegator.

IRB’s default inspector uses PP, and PP has special handling for Delegator instances that unwraps them (via __getobj__) before printing. That causes IRB to display the underlying OpenAI cursor page instead of our custom formatted output.

By using a plain wrapper + method_missing delegation, ‘chat.get_items` displays nicely in IRB/Rails console while still forwarding API helpers like `.data`, `.has_more`, etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, conversation_id:) ⇒ Items

Returns a new instance of Items.



15
16
17
18
# File 'lib/ai/items.rb', line 15

def initialize(response, conversation_id:)
  @response = response
  @conversation_id = conversation_id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



42
43
44
45
46
# File 'lib/ai/items.rb', line 42

def method_missing(method_name, *args, &block)
  return super unless response.respond_to?(method_name)

  response.public_send(method_name, *args, &block)
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



20
21
22
# File 'lib/ai/items.rb', line 20

def response
  @response
end

Instance Method Details

#dataObject



22
23
24
# File 'lib/ai/items.rb', line 22

def data
  response.data
end

#inspectObject



30
31
32
# File 'lib/ai/items.rb', line 30

def inspect
  build_output(html: false, plain: !$stdout.tty?)
end

#pretty_inspectObject



34
35
36
# File 'lib/ai/items.rb', line 34

def pretty_inspect
  "#{inspect}\n"
end

#pretty_print(q) ⇒ Object



38
39
40
# File 'lib/ai/items.rb', line 38

def pretty_print(q)
  q.output << inspect
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ai/items.rb', line 48

def respond_to_missing?(method_name, include_private = false)
  response.respond_to?(method_name, include_private) || super
end

#to_htmlObject



26
27
28
# File 'lib/ai/items.rb', line 26

def to_html
  AI.wrap_html(build_output(html: true))
end