Skip to content

Instantly share code, notes, and snippets.

@ufo2mstar
Created February 10, 2018 04:30
Show Gist options
  • Save ufo2mstar/885846814f6c19e439571a79817c0ddc to your computer and use it in GitHub Desktop.
Save ufo2mstar/885846814f6c19e439571a79817c0ddc to your computer and use it in GitHub Desktop.
Quick Sinatra API
# server.rb
require 'sinatra'
require "sinatra/namespace"
require 'json'
# require 'mongoid'
# DB Setup
# Mongoid.load! "mongoid.config"
# Models
class Book
attr_accessor :title, :author, :isbn
def self.all
{
1 => {title: 'kod1', author:'kodAuth1', isbn: 111 },
2 => {title: 'kod2', author:'kodAuth2', isbn: 222 },
3 => {title: 'kod3', author:'kodAuth3', isbn: 333 },
}
end
# include Mongoid::Document
# field :title, type: String
# field :author, type: String
# field :isbn, type: String
#
# validates :title, presence: true
# validates :author, presence: true
# validates :isbn, presence: true
#
# index({ title: 'text' })
# index({ isbn: 1 }, { unique: true, name: "isbn_index" })
# scope :title, -> (title) { where(title: /^#{title}/) }
# scope :isbn, -> (isbn) { where(isbn: isbn) }
# scope :author, -> (author) { where(author: author) }
end
# Serializers
class BookSerializer
def initialize(book)
@book = book.to_s
end
def as_json(*)
data = {
id: @book.id.to_s,
title: @book.title,
author: @book.author,
isbn: @book.isbn
}
data[:errors] = @book.errors if @book.errors.any?
data
end
end
# Endpoints
get '/' do
'Welcome to BookList!'
end
namespace '/api/v1' do
before do
content_type 'application/json'
end
helpers do
def base_url
@base_url ||= "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}"
end
def json_params
begin
JSON.parse(request.body.read)
rescue
halt 400, { message: 'Invalid JSON' }.to_json
end
end
def book
@book ||= Book.where(id: params[:id]).first
end
def halt_if_not_found!
halt(404, { message: 'Book Not Found'}.to_json) unless book
end
def serialize(book)
BookSerializer.new(book).to_json
end
end
get '/books' do
books = Book.all
# [:title, :isbn, :author].each do |filter|
# books = books.send(filter, params[filter]) if params[filter]
# end
# books.map { |book| BookSerializer.new(book) }.to_json
books.to_json
end
get '/books/:id' do |id|
halt_if_not_found!
serialize(book)
end
post '/books' do
book = Book.new(json_params)
halt 422, serialize(book) unless book.save
response.headers['Location'] = "#{base_url}/api/v1/books/#{book.id}"
status 201
end
patch '/books/:id' do |id|
halt_if_not_found!
halt 422, serialize(book) unless book.update_attributes(json_params)
serialize(book)
end
delete '/books/:id' do |id|
book.destroy if book
status 204
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment