Skip to content

Instantly share code, notes, and snippets.

@devotdev
devotdev / gist:d3c38d973c72f02ca38ed622f6d6e098
Created July 7, 2026 09:53
Service object implementation
ArchiveUser.call(user:)
user.update!(
archived: true,
archived_at: Time.current
)
Project Task
Understand requirements
Identify unknowns
Implementation planning
@devotdev
devotdev / application.rb
Created May 15, 2026 13:09
app/services/partner_api/application.rb - execute_request!
def execute_request!(method, path, body: {}, query: {})
start_time = Time.current
request_id = SecureRandom.hex(8)
Rails.logger.info("[PartnerApi][#{request_id}][REQUEST] #{method.upcase} #{path}")
response = self.class.public_send(
method,
"#{base_url}#{path}",
headers: default_headers,
@devotdev
devotdev / pool_benchmark.rb
Created May 15, 2026 13:08
pool_benchmark.rb
require 'benchmark'
require 'connection_pool'
require 'net/http/persistent'
URL = URI('https://example.com/healthcheck')
N = 100
Benchmark.bm(22) do |x|
x.report('fresh client per call') do
N.times do
@devotdev
devotdev / orders.rb
Created May 15, 2026 13:07
app/services/partner_api/orders.rb
module PartnerApi
class Orders
def show(id)
PARTNER_API_POOL.with do |client|
response = client.get("/api/v1/orders/#{id}", headers: auth_headers)
JSON.parse(response.body)
end
end
private
@devotdev
devotdev / partner_api_pool.rb
Created May 15, 2026 13:07
config/initializers/partner_api_pool.rb
require 'connection_pool'
POOL_SIZE = ENV.fetch('PARTNER_API_POOL_SIZE', 10).to_i
POOL_TIMEOUT = ENV.fetch('PARTNER_API_POOL_TIMEOUT', 5).to_i
PARTNER_API_POOL = ConnectionPool.new(size: POOL_SIZE, timeout: POOL_TIMEOUT) do
PartnerApi::Client.new(base_url: ENV.fetch('PARTNER_API_BASE_URL'))
end
@devotdev
devotdev / client.rb
Last active May 15, 2026 13:06
app/services/partner_api/client.rb
require 'net/http/persistent'
module PartnerApi
class Client
def initialize(base_url:)
@base_url = base_url
@http = Net::HTTP::Persistent.new(name: 'partner_api')
@http.open_timeout = ENV.fetch('PARTNER_API_OPEN_TIMEOUT', 5).to_i
@http.read_timeout = ENV.fetch('PARTNER_API_REQUEST_TIMEOUT', 15).to_i
end
@devotdev
devotdev / Gemfile
Created May 15, 2026 13:05
Gemfile
gem 'httparty', '~> 0.19.0'
gem 'connection_pool', '~> 2.5'
@devotdev
devotdev / application.rb
Created May 15, 2026 13:04
app/services/partner_api/application.rb
module PartnerApi
class Application
class NetTimeout < StandardError; end
include HTTParty
REQUEST_TIMEOUT = ENV.fetch('PARTNER_API_REQUEST_TIMEOUT', 15).to_i
OPEN_TIMEOUT = ENV.fetch('PARTNER_API_OPEN_TIMEOUT', 5).to_i
attr_reader :token