Networking and web
A Ruby program talks to the network with the standard library first. HTTP clients and sockets open the part, then a REST client that retries. The web half builds a server: WEBrick, the Rack interface behind every Ruby web application, a small application with Sinatra, and Rails from its models through controllers and views to a JSON API. JSON is the format all of them speak.
10.1
HTTP clients
Fetch pages and send JSON with the Net::HTTP standard library.
Open
10.2
Sockets
Accept TCP clients with TCPServer and bind IPv4 explicitly.
Open
10.3
REST APIs
Read and write JSON over REST with small reusable Ruby code.
Open
10.4
WEBrick server
Serve files and JSON with WEBrick, mount small handlers, and stop the server cleanly.
Open
10.5
Rails in one lesson
Meet Rails MVC, routes, and models in one conceptual lesson with small file sketches.
Open
10.6
Net::HTTP in depth
A GET with a status, the timeouts and SSL settings a real client needs, reading a redirect instead of following it blindly, and a POST body.
Open
10.7
A REST client that survives
A small client with a retry limit, exponential backoff and a total time budget, tested against httpbin and against a local server that fails twice.
Open
10.8
Rack: what a Ruby web app is
The call(env) interface that every Ruby web framework implements, a config.ru with a middleware, rackup, and one request read end to end.
Open
10.9
A web app with Sinatra
Install Sinatra, define three routes in one file, start the server and read the responses: text, a path parameter and JSON.
Open
10.10
Rails models without the whole app
ActiveRecord on its own against SQLite: a migration, a model with a validation, and the queries that replace hand-written SQL.
Open
10.11
Rails controllers and views
rails new, a scaffolded resource, the controller actions Rails writes, and the ERB view that renders the list.
Open
10.12
A JSON API in Rails
API mode, a model with two routes, a controller that renders JSON and maps validation errors to a 422, and two curl requests.
Open