From 078cb0c513056ed742ffb48b0a7e1a4942a2a149 Mon Sep 17 00:00:00 2001 From: billybonks Date: Sat, 8 Mar 2025 18:37:16 +0800 Subject: [PATCH] refactor: replace HTTParty with standard Ruby Net::HTTP Remove HTTParty dependency and refactor Claude client to use Ruby's standard Net::HTTP library. This change reduces external dependencies which were causing errors on some systems while maintaining the same functionality. The HTTP request handling is now implemented using built-in Ruby libraries, making the application more stable across different environments and reducing the dependency surface area. --- Gemfile.lock | 2 +- committer.gemspec | 1 - lib/clients/claude_client.rb | 30 +++++++++++++++++------------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 32bc879..02ba397 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -9,7 +9,7 @@ GEM crack (1.0.0) bigdecimal rexml - csv (3.3.0) + csv (3.3.2) diff-lcs (1.6.0) hashdiff (1.1.2) httparty (0.22.0) diff --git a/committer.gemspec b/committer.gemspec index 25c7af7..3d5db83 100644 --- a/committer.gemspec +++ b/committer.gemspec @@ -19,6 +19,5 @@ Gem::Specification.new do |spec| spec.executables = %w[committer] spec.require_paths = ['lib'] - spec.add_dependency 'httparty', '~> 0.20' spec.metadata['rubygems_mfa_required'] = 'true' end diff --git a/lib/clients/claude_client.rb b/lib/clients/claude_client.rb index 6835c4f..c53ac3b 100644 --- a/lib/clients/claude_client.rb +++ b/lib/clients/claude_client.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true require 'json' -require 'httparty' +require 'net/http' +require 'uri' require_relative '../committer/config/accessor' module Clients @@ -11,6 +12,8 @@ class OverloadError < StandardError; end class UnknownError < StandardError; end class ConfigError < StandardError; end + API_ENDPOINT = 'https://api.anthropic.com/v1/messages' + def initialize @config = Committer::Config::Accessor.instance @@ -38,19 +41,20 @@ def post(message) private def send_request(body) - options = build_request_options(body) - HTTParty.post('https://api.anthropic.com/v1/messages', options) - end + uri = URI.parse(API_ENDPOINT) + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true - def build_request_options(body) - { - headers: { - 'anthropic-version': '2023-06-01', - 'content-type': 'application/json', - 'x-api-key': @config['api_key'] - }, - body: body.to_json - } + request = Net::HTTP::Post.new(uri.request_uri) + request['anthropic-version'] = '2023-06-01' + request['content-type'] = 'application/json' + request['x-api-key'] = @config['api_key'] + request.body = body.to_json + + response = http.request(request) + JSON.parse(response.body) + rescue JSON::ParserError + { 'type' => 'error', 'error' => { 'type' => 'unknown_error' } } end def handle_error_response(response)