Back to SDKs
Rubyv1.5.0
Ruby SDK
The official Pharmako SDK for Ruby applications. Requires Ruby 3.0+.
Installation
gem install pharmakoOr add to your Gemfile:
gem 'pharmako'Quick Start
require 'pharmako'
# Initialize the client
client = Pharmako::Client.new(api_key: ENV['PHARMAKO_API_KEY'])
# Fetch a patient
patient = client.patients.get('patient_123')
puts patient.name
# List lab results
labs = client.labs.list(patient_id: 'patient_123', limit: 10)
labs.each do |lab|
puts "#{lab.test_name}: #{lab.result}"
end
# Create a webhook
webhook = client.webhooks.create(
url: 'https://your-app.com/webhooks',
events: ['patient.updated', 'lab.completed']
)Configuration
Configure the SDK with various options for your application.
# Basic configuration client = Pharmako::Client.new( api_key: ENV['PHARMAKO_API_KEY'], base_url: 'https://api.pharmako.dev/v1', timeout: 30, max_retries: 3 ) # Use sandbox environment sandbox_client = Pharmako::Client.new( api_key: ENV['PHARMAKO_SANDBOX_KEY'], base_url: 'https://sandbox.pharmako.dev/v1' )
Patient Management
Create, retrieve, update, and delete patient records.
# Create a new patient
patient = client.patients.create(
first_name: 'John',
last_name: 'Doe',
date_of_birth: '1990-01-15',
gender: 'male',
email: 'john.doe@example.com',
phone: '+15555551234'
)
# Retrieve a patient
patient = client.patients.get('patient_123')
# Update patient information
updated_patient = client.patients.update('patient_123',
email: 'newemail@example.com',
phone: '+15555559999'
)
# List patients with pagination
patients = client.patients.list(limit: 20, offset: 0)
# Delete a patient
client.patients.delete('patient_123')Lab Orders & Results
Order lab tests and retrieve results programmatically.
# Browse available tests
catalog = client.labs.catalog(category: 'hematology')
# Create a lab order
order = client.labs.create_order(
patient_id: 'patient_123',
test_codes: ['CBC', 'CMP'],
priority: 'routine',
lab_facility_id: 'lab_xyz789'
)
# Track order status
status = client.labs.get_order('order_456')
puts "Order status: #{status.status}"
# Retrieve results when ready
results = client.labs.get_results('order_456')
results.tests.each do |test|
puts "#{test.name}: #{test.value} #{test.unit}"
puts " Abnormal: #{test.abnormal_flag}" if test.abnormal_flag
endError Handling
Handle errors gracefully with typed exceptions.
begin
patient = client.patients.get('invalid_id')
rescue Pharmako::NotFoundError => e
puts "Patient not found: #{e.message}"
rescue Pharmako::AuthenticationError => e
puts "Authentication failed: #{e.message}"
rescue Pharmako::ValidationError => e
puts "Validation error: #{e.message}"
e.errors.each do |field, messages|
puts " #{field}: #{messages.join(', ')}"
end
rescue Pharmako::RateLimitError => e
puts "Rate limit exceeded. Retry after #{e.retry_after} seconds"
rescue Pharmako::APIError => e
puts "API error (#{e.status_code}): #{e.message}"
puts "Request ID: #{e.request_id}"
endRails Integration
Seamlessly integrate with Ruby on Rails applications.
# config/initializers/pharmako.rb
Pharmako.configure do |config|
config.api_key = Rails.application.credentials.pharmako_api_key
config.base_url = Rails.env.production? ?
'https://api.pharmako.dev/v1' :
'https://sandbox.pharmako.dev/v1'
config.logger = Rails.logger
end
# app/services/patient_service.rb
class PatientService
def initialize
@client = Pharmako::Client.new
end
def sync_patient(patient_data)
@client.patients.create(patient_data)
rescue Pharmako::ValidationError => e
Rails.logger.error("Patient validation failed: #{e.message}")
raise
end
endWebhooks
Subscribe to real-time events and receive notifications.
# Create a webhook
webhook = client.webhooks.create(
url: 'https://your-app.com/webhooks/pharmako',
events: ['patient.created', 'patient.updated', 'lab.completed'],
secret: 'your_webhook_secret'
)
# List all webhooks
webhooks = client.webhooks.list
# Verify webhook signature (in your webhook endpoint)
def verify_webhook(request)
signature = request.headers['X-Pharmako-Signature']
payload = request.body.read
secret = ENV['PHARMAKO_WEBHOOK_SECRET']
expected_signature = OpenSSL::HMAC.hexdigest(
OpenSSL::Digest.new('sha256'),
secret,
payload
)
signature == expected_signature
end
# Handle webhook events
post '/webhooks/pharmako' do
return 401 unless verify_webhook(request)
event = JSON.parse(request.body.read)
case event['type']
when 'patient.created'
# Handle new patient
patient_id = event['data']['id']
puts "New patient created: #{patient_id}"
when 'lab.completed'
# Handle lab completion
order_id = event['data']['order_id']
results = client.labs.get_results(order_id)
# Process results...
end
200
end
# Delete a webhook
client.webhooks.delete('webhook_abc123')Document Management
Upload, retrieve, and manage patient documents securely.
# Upload a document
document = client.documents.create(
patient_id: 'patient_123',
file: File.open('lab_report.pdf', 'rb'),
type: 'lab_report',
description: 'Blood test results from 2024-11-30'
)
# Upload with metadata
document = client.documents.create(
patient_id: 'patient_123',
file: File.open('prescription.pdf', 'rb'),
type: 'prescription',
metadata: {
provider: 'Dr. Smith',
date: '2024-11-30',
prescriptions: ['Medication A', 'Medication B']
}
)
# List documents for a patient
documents = client.documents.list(
patient_id: 'patient_123',
type: 'lab_report',
limit: 20
)
documents.each do |doc|
puts "#{doc.type}: #{doc.description}"
puts " Uploaded: #{doc.created_at}"
puts " Size: #{doc.size_bytes} bytes"
end
# Retrieve a document
document = client.documents.get('doc_xyz789')
# Download document content
content = client.documents.download('doc_xyz789')
File.write('downloaded_report.pdf', content)
# Delete a document
client.documents.delete('doc_xyz789')