Class: GroqApi

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

Overview

GroqApi

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ GroqApi

Returns a new instance of GroqApi.



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

def initialize(options)
  @request = Request.new(options)
  @syntax_highlighter = SyntaxHighlighter.new
end

Instance Attribute Details

#requestObject

Returns the value of attribute request.



11
12
13
# File 'lib/groq_api.rb', line 11

def request
  @request
end

#syntax_highlighterObject

Returns the value of attribute syntax_highlighter.



11
12
13
# File 'lib/groq_api.rb', line 11

def syntax_highlighter
  @syntax_highlighter
end

Instance Method Details

#callObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/groq_api.rb', line 22

def call 
  message_content = request.call
  File.open(File.expand_path('~/.gchat.log'), 'a') do |file|
    file.puts("#{message_content}\n")
  end
  # puts ''
  # puts message_content
  matches = Parser.new(message_content).parse
  if matches
    command = matches[0]
  else
    puts "\033[31m No command found in the message content. \033[0m"
    return
  end

  puts ''
  puts ''
  puts "\033[32m====================================== \033[0m"
  puts ''
  puts syntax_highlighter.highlight(command)
  puts ''
  puts "\033[32mrun(r) copy(o) cancel(c) \033[0m"
  prompt = gets.chomp
  exit if prompt == 'c'
  system(command) if prompt == 'r'
  if prompt == 'o'
    if xclip_installed?
      IO.popen('xclip -selection clipboard', 'w') { |f| f << command }
    else
      puts "\033[31m xclip is not installed. Please install xclip to use the copy feature. \033[0m"
    end
  end
end

#xclip_installed?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/groq_api.rb', line 18

def xclip_installed?
  system('which xclip > /dev/null 2>&1')
end