Code Scot, Steve, Ross, and Miles wrote at Indy.rb, April 2015
source 'https://rubygems.org'
gem 'grape'
require 'grape'
require 'json'
require 'securerandom'
class Note
attr_accessor :id, :title, :body
def initialize(attrs = {})
self.id = SecureRandom.uuid
self.title = attrs[:title]
self.body = attrs[:body]
end
def to_json(an_argument = nil)
::JSON.generate(
{
id: id,
title: title,
body: body
}
)
end
end
module Notey
class API < Grape::API
version 'v1', using: :path
format :json
# [domain]/api/v1/...
prefix :api
resource :notes do
desc 'create a note'
post do
Note.new(params)
end
desc 'edit a note'
put ':id' do
puts "You updated. Trust us. Here are your params: #{params}"
Note.new(title: 'first', body: 'totally new content that you sent us.')
end
desc 'delete a note'
delete ':id' do
puts "Yup, totally deleted a note, too. Totally did it. Congrats!"
Note.new(title: 'deleted', body: 'bam')
end
desc 'return all notes'
get do
[Note.new(title: 'First', body: 'All of the contents'), Note.new(title: 'Second', body: 'Some of the contents')]
end
desc "Return a note."
get ':id' do
Note.new(title: 'First', body: 'All of the contents')
end
end
end
end
require_relative 'api'
run Notey::API