Skip to content

Commit 29d3c7e

Browse files
authored
rubolint all the things (#2)
* rubolint all the things * fix syntax errors from rubocop autocorrect
1 parent 1ed692f commit 29d3c7e

22 files changed

+342
-227
lines changed

.rubocop.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require:
2+
- rubocop-rake
3+
- rubocop-rspec
4+
5+
AllCops:
6+
NewCops: enable
7+
8+
Exclude:
9+
- 'lib/hawksi.rb'
10+
11+
Style/HashSyntax:
12+
Enabled: false
13+
14+
Layout/HeredocIndentation:
15+
Exclude:
16+
- 'lib/hawksi.rb'
17+
18+
Metrics/BlockLength:
19+
Exclude:
20+
- 'spec/**/*'
21+
- '*.gemspec'

Gemfile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
source 'https://rubygems.org'
44

5-
gem 'json'
65
gem 'httpx', '~> 1.3'
7-
gem "puma", ">= 6.4.3"
6+
gem 'json'
7+
gem 'puma', '>= 6.4.3'
88
gem 'rack'
99
gem 'thor'
1010

1111
group :development, :test do
12+
gem 'bundler'
13+
gem 'rake'
1214
gem 'rspec', '~> 3.13'
15+
gem 'rubocop', '1.66.1'
16+
gem 'rubocop-rake'
17+
gem 'rubocop-rspec'
18+
gem 'ruby-lsp'
1319
end
1420

1521
group :test do

Gemfile.lock

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4+
ast (2.4.2)
45
diff-lcs (1.5.1)
56
http-2 (1.0.1)
67
httpx (1.3.0)
78
http-2 (>= 1.0.0)
89
json (2.7.1)
10+
language_server-protocol (3.17.0.3)
11+
logger (1.6.1)
912
nio4r (2.7.3)
13+
parallel (1.26.3)
14+
parser (3.3.5.0)
15+
ast (~> 2.4.1)
16+
racc
17+
prism (1.0.0)
1018
puma (6.4.3)
1119
nio4r (~> 2.0)
20+
racc (1.8.1)
1221
rack (2.2.9)
1322
rack-test (1.1.0)
1423
rack (>= 1.0, < 3)
24+
rainbow (3.1.1)
25+
rake (13.2.1)
26+
rbs (3.5.3)
27+
logger
28+
regexp_parser (2.9.2)
1529
rspec (3.13.0)
1630
rspec-core (~> 3.13.0)
1731
rspec-expectations (~> 3.13.0)
@@ -25,19 +39,49 @@ GEM
2539
diff-lcs (>= 1.2.0, < 2.0)
2640
rspec-support (~> 3.13.0)
2741
rspec-support (3.13.1)
42+
rubocop (1.66.1)
43+
json (~> 2.3)
44+
language_server-protocol (>= 3.17.0)
45+
parallel (~> 1.10)
46+
parser (>= 3.3.0.2)
47+
rainbow (>= 2.2.2, < 4.0)
48+
regexp_parser (>= 2.4, < 3.0)
49+
rubocop-ast (>= 1.32.2, < 2.0)
50+
ruby-progressbar (~> 1.7)
51+
unicode-display_width (>= 2.4.0, < 3.0)
52+
rubocop-ast (1.32.3)
53+
parser (>= 3.3.1.0)
54+
rubocop-rake (0.6.0)
55+
rubocop (~> 1.0)
56+
rubocop-rspec (3.0.5)
57+
rubocop (~> 1.61)
58+
ruby-lsp (0.18.2)
59+
language_server-protocol (~> 3.17.0)
60+
prism (~> 1.0)
61+
rbs (>= 3, < 4)
62+
sorbet-runtime (>= 0.5.10782)
63+
ruby-progressbar (1.13.0)
64+
sorbet-runtime (0.5.11577)
2865
thor (1.3.1)
66+
unicode-display_width (2.5.0)
2967

3068
PLATFORMS
3169
arm64-darwin-23
3270
ruby
3371

3472
DEPENDENCIES
73+
bundler
3574
httpx (~> 1.3)
3675
json
3776
puma (>= 6.4.3)
3877
rack
3978
rack-test (~> 1.1)
79+
rake
4080
rspec (~> 3.13)
81+
rubocop (= 1.66.1)
82+
rubocop-rake
83+
rubocop-rspec
84+
ruby-lsp
4185
thor
4286

4387
BUNDLED WITH

Rakefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'bundler/gem_tasks'
24
require 'rspec/core/rake_task'
35

@@ -11,11 +13,11 @@ task :build do
1113
end
1214

1315
desc 'Install the Hawksi gem locally'
14-
task :install => :build do
16+
task install: :build do
1517
system "gem install ./hawksi-#{Hawksi::VERSION}.gem"
1618
end
1719

1820
desc 'Release the Hawksi gem to RubyGems'
19-
task :release => :build do
21+
task release: :build do
2022
system "gem push ./hawksi-#{Hawksi::VERSION}.gem"
2123
end

bin/hawksi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
24
require_relative '../lib/hawksi'
35
CLI.start(ARGV)

hawksi.gemspec

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# hawksi.gemspec
24
require_relative 'lib/version'
35

@@ -8,7 +10,8 @@ Gem::Specification.new do |spec|
810
spec.email = ['[email protected]']
911

1012
spec.summary = 'Hawksi: Rack middleware to the Mocksi API.'
11-
spec.description = 'Hawksi sits between your application and the Mocksi API, allowing our agents to learn from your app to simulate whatever you can imagine.'
13+
spec.description = 'Hawksi sits between your application and the Mocksi API,\n'
14+
spec.description += 'allowing our agents to learn from your app to simulate whatever you can imagine.'
1215
spec.homepage = 'https://github.com/Mocksi/hawksi'
1316
spec.license = 'MIT'
1417
spec.required_ruby_version = Gem::Requirement.new('>= 3.2.0')
@@ -20,22 +23,19 @@ Gem::Specification.new do |spec|
2023
spec.files = Dir.chdir(File.expand_path(__dir__)) do
2124
`git ls-files -z`.split("\x0").reject do |f|
2225
%w[test spec features .gitignore hawksi.gemspec].include?(f) ||
23-
f.match?(/(^\.|\/\.\.|\.\.\/|\.git|\.hg|CVS|\.svn|\.lock|~$)/) ||
24-
f.end_with?('.gem') # Exclude gem files
26+
f.match?(%r{(^\.|/\.\.|\.\./|\.git|\.hg|CVS|\.svn|\.lock|~$)}) ||
27+
f.end_with?('.gem') # Exclude gem files
2528
end
2629
end
2730
spec.bindir = 'bin'
2831
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
2932
spec.require_paths = ['lib']
3033

31-
spec.add_dependency 'rack', '~> 2.2'
34+
spec.add_dependency 'httpx', '~> 1.3'
35+
spec.add_dependency 'json', '~> 2.5'
3236
spec.add_dependency 'puma', '~> 5.0'
37+
spec.add_dependency 'rack', '~> 2.2'
3338
spec.add_dependency 'thor', '~> 1.1'
34-
spec.add_dependency 'json', '~> 2.5'
35-
spec.add_dependency 'httpx', '~> 1.3'
36-
3739

38-
spec.add_development_dependency 'bundler', '~> 2.2'
39-
spec.add_development_dependency 'rake', '~> 13.0'
40-
spec.add_development_dependency 'rspec', '~> 3.10'
40+
spec.metadata['rubygems_mfa_required'] = 'true'
4141
end

lib/captures_cli.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
# frozen_string_literal: true
2+
13
require 'thor'
24
require_relative 'file_storage'
35

6+
# CLI for listing captured requests and responses
47
class CapturesCLI < Thor
5-
desc "list", "Lists recent captured requests and responses"
6-
option :base_dir, type: :string, desc: 'Base directory for storing intercepted data. Defaults to ./tmp/intercepted_data'
7-
def list(*args)
8+
desc 'list', 'Lists recent captured requests and responses'
9+
option :base_dir, type: :string,
10+
desc: 'Base directory for storing intercepted data. Defaults to ./tmp/intercepted_data'
11+
def list(*_args) # rubocop:disable Metrics/MethodLength
812
base_dir = FileStorage.base_dir
913
FileStorage.base_dir = options[:base_dir] if options[:base_dir]
1014

@@ -14,7 +18,7 @@ def list(*args)
1418
files = Dir.glob(glob_pattern)
1519

1620
if files.empty?
17-
puts "No captured requests or responses found."
21+
puts 'No captured requests or responses found.'
1822
return
1923
end
2024

lib/cli.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'thor'
24
require 'puma'
35
require 'puma/cli'
@@ -6,33 +8,35 @@
68
require_relative 'captures_cli'
79
require_relative 'uploads_cli'
810

11+
# CLI for starting and stopping the Hawksi Interceptor server
912
class CLI < Thor
10-
desc "start", "Starts the Hawksi Interceptor server"
11-
option :base_dir, type: :string, desc: 'Base directory for storing intercepted data. Defaults to ./tmp/intercepted_data'
13+
desc 'start', 'Starts the Hawksi Interceptor server'
14+
option :base_dir, type: :string,
15+
desc: 'Base directory for storing intercepted data. Defaults to ./tmp/intercepted_data'
1216
def start(*args)
1317
FileStorage.base_dir = options[:base_dir] if options[:base_dir]
14-
puts "Starting HawksiInterceptor server..."
18+
puts 'Starting HawksiInterceptor server...'
1519
Puma::CLI.new(args).run
1620
end
1721

1822
map 'serve' => 'start'
1923

20-
desc "stop", "Stops the HawksiInterceptor server"
24+
desc 'stop', 'Stops the HawksiInterceptor server'
2125
def stop
22-
puts "Stopping Hawksi Interceptor server..."
23-
system("pkill -f puma")
26+
puts 'Stopping Hawksi Interceptor server...'
27+
system('pkill -f puma')
2428
end
2529

26-
desc "captures", "Manage captures"
27-
subcommand "captures", CapturesCLI
30+
desc 'captures', 'Manage captures'
31+
subcommand 'captures', CapturesCLI
2832

29-
desc "uploads", "Uploads captured requests and responses"
30-
subcommand "uploads", UploadsCLI
33+
desc 'uploads', 'Uploads captured requests and responses'
34+
subcommand 'uploads', UploadsCLI
3135

32-
desc "clear", "Clears stored request/response data"
36+
desc 'clear', 'Clears stored request/response data'
3337
def clear
3438
FileUtils.rm_rf('./intercepted_data/requests')
3539
FileUtils.rm_rf('./intercepted_data/responses')
36-
puts "Cleared stored data."
40+
puts 'Cleared stored data.'
3741
end
3842
end

lib/command_executor.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
# frozen_string_literal: true
2+
13
require 'httpx'
24
require 'json'
35
require 'logger'
46

7+
# Generates and sends commands to the Reactor endpoint.
58
class CommandExecutor
69
attr_reader :logger, :client_uuid, :endpoint_url
710

@@ -11,12 +14,12 @@ def initialize(logger, client_uuid)
1114
@endpoint_url = Hawksi.configuration.reactor_url
1215
end
1316

14-
def execute_command(command, params)
17+
def execute_command(command, params) # rubocop:disable Metrics/MethodLength
1518
request_body = build_request_body(command, params)
1619
response = send_request(request_body)
1720

1821
if response.nil?
19-
logger.error "Failed to execute command due to a request error."
22+
logger.error 'Failed to execute command due to a request error.'
2023
elsif response.is_a?(HTTPX::ErrorResponse)
2124
logger.error "HTTPX Error: #{response.error.message}"
2225
elsif response.status == 200
@@ -39,8 +42,9 @@ def build_request_body(command, params)
3942
def send_request(request_body)
4043
logger.info "sending request to #{endpoint_url}"
4144
logger.info "request body: #{request_body}"
42-
HTTPX.post(endpoint_url, headers: { "Content-Type" => "application/json", "x-client-id" => client_uuid }, body: request_body)
43-
rescue => e
45+
HTTPX.post(endpoint_url, headers: { 'Content-Type' => 'application/json', 'x-client-id' => client_uuid },
46+
body: request_body)
47+
rescue StandardError => e
4448
logger.error "Failed to send request: #{e.message}"
4549
nil
4650
end

lib/file_handler.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
# frozen_string_literal: true
2+
13
require 'fileutils'
24
require 'securerandom'
35

6+
# Handles file operations.
47
class FileHandler
58
def initialize(base_dir, logger)
69
@base_dir = base_dir
@@ -25,7 +28,7 @@ def generate_client_uuid
2528
client_uuid
2629
end
2730

28-
def create_tar_gz_files(files)
31+
def create_tar_gz_files(files) # rubocop:disable Metrics/MethodLength
2932
tar_gz_files = []
3033
files.each do |file|
3134
tar_file = "#{file}.tar"
@@ -39,7 +42,6 @@ def create_tar_gz_files(files)
3942
system("gzip #{tar_file}")
4043
end
4144

42-
4345
tar_gz_files << tar_gz_file
4446
end
4547
tar_gz_files

0 commit comments

Comments
 (0)