Ruby
Cliente minimalista en Ruby usando Net::HTTP de stdlib (sin gemas externas). Cubre emisión, consulta y descarga de PDF; ampliá el método request privado para los demás endpoints siguiendo el mismo patrón.
# lxs_client.rb
require 'net/http'
require 'json'
require 'securerandom'
class LxsClient
BASE_URL = 'https://api.lxsyscr.com'
def initialize(api_key)
@api_key = api_key
end
def emitir(data, idempotency_key: nil)
request(:post, '/api/v1/facturas', body: data,
headers: { 'Idempotency-Key' => idempotency_key || SecureRandom.uuid })
end
def consultar(clave)
request(:get, "/api/v1/facturas/#{clave}")
end
def descargar_pdf(clave)
request(:get, "/api/v1/facturas/#{clave}/pdf", raw: true)
end
private
def request(method, path, body: nil, headers: {}, raw: false)
uri = URI("#{BASE_URL}#{path}")
req_class = { get: Net::HTTP::Get, post: Net::HTTP::Post, delete: Net::HTTP::Delete }[method]
req = req_class.new(uri)
req['X-Api-Key'] = @api_key
req['Content-Type'] = 'application/json'
headers.each { |k, v| req[k] = v }
req.body = body.to_json if body
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
raise "HTTP #{res.code}: #{res.body}" if res.code.to_i >= 400
raw ? res.body : JSON.parse(res.body)
end
end
# Uso:
client = LxsClient.new(ENV['LXS_API_KEY'])
factura = client.emitir({
tipo: 'FacturaElectronica',
cliente: { idCliente: 42 },
condicionVenta: '01',
medioPago: '01',
moneda: 'CRC',
lineas: [{ idProducto: 100, cantidad: 2 }]
})
puts "Clave: #{factura['clave']}"