Skip to content

Instantly share code, notes, and snippets.

@mileszs
Created April 9, 2015 13:44
Show Gist options
  • Save mileszs/260a5483bf0792ef747e to your computer and use it in GitHub Desktop.
Save mileszs/260a5483bf0792ef747e to your computer and use it in GitHub Desktop.
Code Scot, Steve, Ross, and Miles wrote at Indy.rb, April 2015
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
source 'https://rubygems.org'
gem 'grape'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment