§2023-02-27

2.3. Creating Our First Sinatra API

$ mkdir master_ruby_web_apis && cd $_
$ bundler init
$ nano Gemfile --> gem 'sinatra' 
$ bundler install
Fetching gem metadata from https://rubygems.org/....
Resolving dependencies...
Using bundler 2.4.6
Using rack 2.2.6.2
Using tilt 2.1.0
Using ruby2_keywords 0.0.5
Using rack-protection 3.0.5
Using mustermann 3.0.0
Using sinatra 3.0.5
Bundle complete! 1 Gemfile dependency, 7 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
# webapi.rb
require 'sinatra'

get '/' do
  'Master Ruby Web APIs - Chapter 2'
end

2.4.1. URL, URI, IRI, URN, …

# webapi.rb
require 'sinatra'
require 'json'

users = {
  'thibault': { first_name: 'Thibault', last_name: 'Denizet', age: 25 },
  'simon':    { first_name: 'Simon', last_name: 'Random', age: 26 },
  'john':     { first_name: 'John', last_name: 'Smith', age: 28 }
}

get '/' do
  'Master Ruby Web APIs - Chapter 2'
end

get '/users' do
  users.map { |name, data| data.merge(id: name) }.to_json
end