Files

82 lines
1.4 KiB
TeX

\section{Ruby SDK}
\subsection{Gem Structure}
\begin{lstlisting}[caption=Ruby SDK Structure]
hanzo-ruby/
lib/
hanzo.rb
hanzo/
client.rb
resources/
product.rb
order.rb
models/
product.rb
errors.rb
utils/
pagination.rb
hanzo.gemspec
Gemfile
\end{lstlisting}
\subsection{Client Implementation}
\begin{lstlisting}[language=Ruby, caption=Ruby Client]
require 'hanzo'
Hanzo.api_key = 'sk_live_...'
# List products
products = Hanzo::Product.list(limit: 10)
products.each do |product|
puts product.name
end
# Create a product
product = Hanzo::Product.create(
name: 'Widget',
price: 1999,
description: 'A useful widget'
)
# Update a product
product.price = 2499
product.save
\end{lstlisting}
\subsection{Active Record Pattern}
\begin{lstlisting}[language=Ruby, caption=Ruby Active Record Style]
# Retrieve and modify
product = Hanzo::Product.retrieve('prod_123')
product.name = 'Updated Widget'
product.save
# Delete
product.delete
# Reload from API
product.refresh
\end{lstlisting}
\subsection{Block Syntax}
\begin{lstlisting}[language=Ruby, caption=Ruby Block Syntax]
# Pagination with blocks
Hanzo::Product.list.auto_paging_each do |product|
puts product.name
end
# Error handling with blocks
Hanzo::Product.create(name: 'Widget', price: 1999) do |product, error|
if error
puts "Error: #{error.message}"
else
puts "Created: #{product.id}"
end
end
\end{lstlisting}