matheus 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c05656b0745325e8c8f1335de5aa607be0352e39c973cd120b904986e213308d
4
- data.tar.gz: de0ae216763330fc255bb5ca5290da2f60e8434feebf432eeb0273cb43aa676b
3
+ metadata.gz: 72cfeba7ddd84ef4fd7c7c80144a1a8821eb4c3d03fd33f3d718109b08d39bae
4
+ data.tar.gz: add8482702c9e6af603e6ea2ba5cb7cefa589c57bf133385f2682db7532b8324
5
5
  SHA512:
6
- metadata.gz: 9e8de67cd9b1a0d1b87d2900701f4d09014e5fd18f790b85bf69ba71cd731f784e8fafff53ae22ca44f92fab1c71bf5281336ccbb69790b9fa0647572ddcd507
7
- data.tar.gz: a49214eacc6b2135b1e1b7a903b68026a5fee9776d316270e8d8ea9f8c36ac3b06400fe540413a9c84ba402d29a3b293e59bc092956cdf96e3c9aa37018f2b97
6
+ metadata.gz: 222e09a48119ad9aa15daa86b26f8159dd45dc9f7b1a8743e6bc8e1255399562e4a4ed2ee3a8ac2539b61c15d61b5d79532913cd2710b2a9f1657dae82de4a44
7
+ data.tar.gz: c46798f32fc8a73cd99fb47372bdb61060718e1f0366f5d1623215e397bdb0ae5074666fc145a7957350f80bcdd4eeb459fe47809f4e656fa242423d07e28d9c
data/CHANGELOG.md CHANGED
@@ -1,6 +1,21 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.1.0] - 2024-02-19
3
+ ## [0.2.0]
4
+
5
+ _Released 2024-08-26_
6
+
7
+ - Add `q` command to ask an question to an LLM
8
+
9
+ ```sh
10
+ $ q "What is the capital of France?"
11
+ The capital of France is **Paris**
12
+ ```
13
+
14
+ - Automatically generate executables for the commands when the gem builds.
15
+
16
+ ## [0.1.0]
17
+
18
+ _Released 2024-02-19_
4
19
 
5
20
  - Initial release. It includes the commands:
6
21
 
data/Rakefile CHANGED
@@ -2,7 +2,46 @@
2
2
 
3
3
  require "bundler/gem_tasks"
4
4
  require "rspec/core/rake_task"
5
+ require "active_support"
6
+ require "active_support/core_ext"
5
7
 
6
8
  RSpec::Core::RakeTask.new(:spec)
7
9
 
8
10
  task default: :spec
11
+
12
+ desc "Generate executables for command classes"
13
+ task :generate_executables do
14
+ require "fileutils"
15
+
16
+ # Load the gem's library files
17
+ $LOAD_PATH.unshift("#{__dir__}/lib")
18
+
19
+ require "matheus"
20
+
21
+ Zeitwerk::Loader.eager_load_all
22
+
23
+ exe_dir = File.expand_path("#{__dir__}/exe")
24
+
25
+ Matheus::Command.descendants.each do |command_class|
26
+ script_path = File.join(exe_dir, command_class.name.demodulize.underscore.dasherize)
27
+ next if File.exist?(script_path)
28
+
29
+ script_content = <<~SCRIPT
30
+ #!/usr/bin/env ruby
31
+
32
+ $LOAD_PATH.unshift("\#{__dir__}/../lib")
33
+
34
+ require "matheus"
35
+
36
+ Matheus::#{command_class.name.demodulize}.call ARGV
37
+ SCRIPT
38
+
39
+ File.write(script_path, script_content)
40
+ FileUtils.chmod("+x", script_path)
41
+
42
+ puts "Generated executable: #{script_path}"
43
+ end
44
+ end
45
+
46
+ # Ensure the generate_executables task runs before the build task
47
+ task build: :generate_executables
data/exe/date-of-last CHANGED
@@ -4,4 +4,4 @@ $LOAD_PATH.unshift("#{__dir__}/../lib")
4
4
 
5
5
  require "matheus"
6
6
 
7
- Matheus::DateOfLast.call(ARGV[0])
7
+ Matheus::DateOfLast.call ARGV
data/exe/q ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift("#{__dir__}/../lib")
4
+
5
+ require "matheus"
6
+
7
+ Matheus::Q.call ARGV
@@ -3,15 +3,15 @@
3
3
  require "date"
4
4
 
5
5
  module Matheus
6
+ # Usage:
7
+ # $ date-of-last monday
8
+ # 2022-01-24
6
9
  class DateOfLast < Command
7
- def call(day_name)
8
- day_name = day_name.to_s
10
+ def call(args)
11
+ day_name = args[0].to_s
9
12
 
10
- target_wday = wday_for(day_name) or return Failure("invalid day name: #{day_name}")
11
- date = Date.today
12
- one_day_ago = (date - 1)
13
- one_week_ago = date - 7
14
- date = one_day_ago.downto(one_week_ago).find { _1.wday == target_wday }
13
+ target_wday = wday_for(day_name) or return Failure("invalid day name: #{day_name.inspect}")
14
+ date = Enumerator.produce(Date.today - 1, &:prev_day).find { _1.wday == target_wday }
15
15
 
16
16
  puts date
17
17
  end
data/lib/matheus/puts.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "date"
4
- require "active_support"
5
- require "active_support/core_ext"
6
4
 
7
5
  module Matheus
6
+ # Usage:
7
+ # $ puts "Date.today"
8
+ # 2024-08-26
8
9
  class Puts < Command
9
10
  def call(argv)
10
11
  puts eval(argv.join(" ")) # standard:disable Security/Eval
data/lib/matheus/q.rb ADDED
@@ -0,0 +1,42 @@
1
+ require "openai"
2
+ require "tty-markdown"
3
+
4
+ module Matheus
5
+ # Usage:
6
+ # $ q "What is the capital of France?"
7
+ # The capital of France is Paris.
8
+ class Q < Command
9
+ BASE_PROMPT = "Answer this question in a short and concise way. You can use markdown in the response: "
10
+
11
+ def call(question)
12
+ ask_llm(question).then { print_markdown _1 }
13
+ rescue => e
14
+ Failure(e.message)
15
+ end
16
+
17
+ private
18
+
19
+ def ask_llm(question)
20
+ raise "Question can't be blank." if question.blank?
21
+
22
+ response = client.chat(
23
+ parameters: {
24
+ model: "gpt-4o-mini", # using gpt-4o model
25
+ messages: [{role: "user", content: "#{BASE_PROMPT}#{question}"}]
26
+ }
27
+ )
28
+
29
+ raise response["error"]["message"] if response.has_key?("error")
30
+
31
+ response.dig("choices", 0, "message", "content")
32
+ end
33
+
34
+ def print_markdown(text)
35
+ puts TTY::Markdown.parse(text)
36
+ end
37
+
38
+ def client
39
+ OpenAI::Client.new(access_token: ENV.fetch("OPENAI_API_KEY"))
40
+ end
41
+ end
42
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Matheus
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/matheus.rb CHANGED
@@ -1,7 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "dotenv"
4
+ require "active_support"
5
+ require "active_support/core_ext"
3
6
  require "zeitwerk"
4
7
 
8
+ Dotenv.load("~/.env")
5
9
  Zeitwerk::Loader.for_gem.setup
6
10
 
7
11
  module Matheus
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: matheus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matheus Richard
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-19 00:00:00.000000000 Z
11
+ date: 2024-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk
@@ -16,16 +16,30 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 2.6.13
19
+ version: 2.6.17
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 2.6.13
26
+ version: 2.6.17
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 7.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 7.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: ruby-openai
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - "~>"
@@ -38,12 +52,41 @@ dependencies:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
54
  version: 7.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: tty-markdown
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.7.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.7.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: dotenv
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.1.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.1.2
41
83
  description: A bunch of CLI tools I made for my own use.
42
84
  email:
43
85
  - matheusrichardt@gmail.com
44
86
  executables:
45
87
  - date-of-last
46
88
  - puts
89
+ - q
47
90
  extensions: []
48
91
  extra_rdoc_files: []
49
92
  files:
@@ -55,10 +98,12 @@ files:
55
98
  - Rakefile
56
99
  - exe/date-of-last
57
100
  - exe/puts
101
+ - exe/q
58
102
  - lib/matheus.rb
59
103
  - lib/matheus/command.rb
60
104
  - lib/matheus/date_of_last.rb
61
105
  - lib/matheus/puts.rb
106
+ - lib/matheus/q.rb
62
107
  - lib/matheus/result.rb
63
108
  - lib/matheus/string_format.rb
64
109
  - lib/matheus/version.rb
@@ -84,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
129
  - !ruby/object:Gem::Version
85
130
  version: '0'
86
131
  requirements: []
87
- rubygems_version: 3.4.1
132
+ rubygems_version: 3.5.17
88
133
  signing_key:
89
134
  specification_version: 4
90
135
  summary: A bunch of CLI tools I made for my own use.