Created
April 9, 2015 13:44
-
-
Save mileszs/260a5483bf0792ef747e to your computer and use it in GitHub Desktop.
Code Scot, Steve, Ross, and Miles wrote at Indy.rb, April 2015
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'api' | |
run Notey::API |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'grape' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment