mokkku 0.0.1 → 0.0.2

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: dea0bbaeb26c51558aa530f63a7a57ea1118d5d858c46b284b0aa0eefa43fe1c
4
- data.tar.gz: 63c3ff3581a61bae6f77c9f20a11a56cd7bca662c4722f7284c8ac1523b0b4fb
3
+ metadata.gz: 234a86d569711e52239d937a81d56f24f2b15df2fc70d3bdc534cb95775e1d7b
4
+ data.tar.gz: 2f25efb4d4351948b99991f698621cb9a44ba512146ecf2834bb60cf2c9db818
5
5
  SHA512:
6
- metadata.gz: dc4da5ff3401d84a7c6340da54caa95a334fb548b7d33eb7194487492276c202c61bf17dc9eea471d982ea134076d5f821de7d4d23f248929cdbf67fb7a67369
7
- data.tar.gz: a8a65b0ce61a7317d43261ec44e83a47344524c4226fa8f6d78d69f27544d703b0f8b501fbb6001765ff555aec60b27f1b34d637de1960cdc8fe697ce2902f76
6
+ metadata.gz: b4f7e2e15f31417967cd38fa3141197e8515b9e58fe867a79a5a8e0fbe826579002afb16676dfdaba0aa3fca8d3d75dac162ba516b14036b4d12c87800d0ebad
7
+ data.tar.gz: 9bf25b64bcc41d563c8b5f64f395b6d2325f42fb2599b5c2d16c6ed5810544f7ea461646e61b12d7b02b22436216680a1b50310b8a65dae25be37b6096447a4f
data/bin/mokkku ADDED
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'mokkku'
5
+ require 'optionparser'
6
+
7
+ options = {}
8
+
9
+ OptionParser.new do |opts|
10
+ opts.banner = 'Usage: mokkku [options]'
11
+
12
+ opts.on('-m', '--model Model', 'Provide model name to generate mocks for') do |opt|
13
+ options[:models] = [opt]
14
+ end
15
+
16
+ opts.on('--models Model1,Model2', 'Provide model names to generate mocks for') do |opt|
17
+ options[:models] = opt.split(',')
18
+ end
19
+
20
+ opts.on('-a', '--api-key value', 'API key for LLM') do |opt|
21
+ options[:llm_api_key] = opt
22
+ end
23
+
24
+ opts.on('-llm', '--llm-model value', 'Type of LLM: GPT, Gemini, or Claude') do |opt|
25
+ options[:llm_model] = opt
26
+ end
27
+
28
+ opts.on('-p', '--mocks-path value', 'Location where files will be created, default to ./spec/mocks') do |opt|
29
+ options[:mocks_path] = opt
30
+ end
31
+
32
+ opts.on('-c', '--count value', 'Number of entries to generate in mock file, 10 by default') do |opt|
33
+ options[:mocks_count] = opt
34
+ end
35
+
36
+ opts.on('-l', '--language value', 'The language of mocks, english by default') do |opt|
37
+ options[:language] = opt
38
+ end
39
+ end.parse!
40
+
41
+ Mokkku::Cli::Runner.start(options)
@@ -0,0 +1,27 @@
1
+ module Mokkku
2
+ module Cli
3
+ class Prompt
4
+ IGNORED_COLUMNS = %w[id created_at updated_at].freeze
5
+
6
+ def self.generate(model_name, columns, mocks_count, language)
7
+ columns_for_prompt = columns.reject { |col| IGNORED_COLUMNS.include?(col.name) || col.name.match?(/.*\_id$/) }
8
+ .inject({}) { |hash, col| hash[col.name] = col.sql_type_metadata.type; hash }
9
+
10
+ prompt_start = "Act like a virtual assistant that generates YAML file with the test data for a given Ruby on Rails model. Generate #{mocks_count.to_i} entries for the following model named #{model_name}:"
11
+
12
+ yaml_attributes = columns_for_prompt.map do |name, type|
13
+ " #{name}: #{type}\n"
14
+ end
15
+
16
+ prompt_yaml_part = "- #{yaml_attributes.join}"
17
+ prompt_summary = 'Reply only with the content of the YAML file.'
18
+
19
+ if language != 'english'
20
+ prompt_summary += " Translate test data to #{language} language."
21
+ end
22
+
23
+ "#{prompt_start}\n\n#{prompt_yaml_part}\n\n#{prompt_summary}"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'validator'
2
+ require_relative 'prompt'
3
+ require_relative '../generators/builder'
4
+
5
+ module Mokkku
6
+ module Cli
7
+ class Runner
8
+ def self.start(options = {})
9
+ ::Mokkku::Cli::Validator.validate_options!(options)
10
+
11
+ options[:models].each do |model_name|
12
+ columns = Object.const_get(model_name).columns
13
+ prompt = ::Mokkku::Cli::Prompt.generate(model_name, columns, options.fetch(:mocks_count, 10), options.fetch(:language, 'english'))
14
+ generator = ::Mokkku::Generators::Builder.call(
15
+ generator: options[:llm_model],
16
+ api_key: options[:llm_api_key],
17
+ prompt: prompt
18
+ )
19
+
20
+ yaml_mock = generator.call(api_key: options[:llm_api_key], prompt: prompt)
21
+ mock_file_name = model_name.to_s.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
22
+
23
+ puts "Write mocks for #{model_name} model in #{mock_file_name}.yml"
24
+ File.write(File.join(options.fetch(:mocks_path, './spec/mocks'), "#{mock_file_name}.yml"), yaml_mock)
25
+ end
26
+
27
+ puts 'Done!'
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ module Mokkku
2
+ module Cli
3
+ module Validator
4
+ ModelsNotProvided = Class.new(StandardError)
5
+ InvalidLlmModel = Class.new(StandardError)
6
+ LlmApiKeyNotProvided = Class.new(StandardError)
7
+
8
+ def self.validate_options!(options)
9
+ raise ModelsNotProvided if options.fetch(:models, []).empty?
10
+ raise InvalidLlmModel unless %w[gpt gemini claude].include?(options[:llm_model].to_s.downcase)
11
+ raise LlmApiKeyNotProvided if options[:llm_api_key].to_s == ''
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'gpt'
2
+
3
+ module Mokkku
4
+ module Generators
5
+ class Builder
6
+ UnsupportedGenerator = Class.new(StandardError)
7
+
8
+ def self.call(generator:, api_key:, prompt:)
9
+ case generator
10
+ when 'gpt'
11
+ Mokkku::Generators::Gpt
12
+ else
13
+ raise UnsupportedGenerator
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ require 'openai'
2
+
3
+ module Mokkku
4
+ module Generators
5
+ class Gpt
6
+ def self.call(api_key:, prompt:)
7
+ client = OpenAI::Client.new(access_token: api_key)
8
+
9
+ response = client.completions(parameters: {
10
+ prompt: prompt,
11
+ model: 'gpt-3.5-turbo-instruct',
12
+ temperature: 0,
13
+ max_tokens: 1_000,
14
+ top_p: 1.0,
15
+ frequency_penalty: 0.0,
16
+ presence_penalty: 0.0
17
+ })
18
+
19
+ response['choices'].first['text']
20
+ end
21
+ end
22
+ end
23
+ end
@@ -3,7 +3,7 @@ module Mokkku
3
3
  module_function
4
4
 
5
5
  def to_s
6
- '0.0.1'
6
+ '0.0.2'
7
7
  end
8
8
  end
9
9
  end
data/lib/mokkku.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require_relative 'mokkku/utils'
2
+ require_relative 'mokkku/cli/runner'
2
3
 
3
4
  module Mokkku
4
5
  extend Mokkku::Utils
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mokkku
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paweł Dąbrowski
@@ -44,13 +44,34 @@ dependencies:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: ruby-openai
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 7.1.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 7.1.0
47
61
  description: Generate mock data that is unique for your application
48
62
  email: contact@paweldabrowski.com
49
- executables: []
63
+ executables:
64
+ - mokkku
50
65
  extensions: []
51
66
  extra_rdoc_files: []
52
67
  files:
68
+ - bin/mokkku
53
69
  - lib/mokkku.rb
70
+ - lib/mokkku/cli/prompt.rb
71
+ - lib/mokkku/cli/runner.rb
72
+ - lib/mokkku/cli/validator.rb
73
+ - lib/mokkku/generators/builder.rb
74
+ - lib/mokkku/generators/gpt.rb
54
75
  - lib/mokkku/mocked_object.rb
55
76
  - lib/mokkku/utils.rb
56
77
  - lib/mokkku/version.rb