Created
May 12, 2014 20:19
-
-
Save laser/2995f4fd25f171f7186a to your computer and use it in GitHub Desktop.
Basic Server - Ruby
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
#!/usr/bin/env ruby | |
require 'forwardable' | |
require 'barrister' | |
require 'sinatra' | |
require './store.rb' | |
class TodoManager | |
def initialize(store) | |
@store = store | |
end | |
def readTodos | |
@store.get_all() | |
end | |
def createTodo(properties) | |
@store.save(properties) | |
end | |
def updateTodo(todo) | |
@store.update(todo['id'], todo) | |
end | |
def deleteTodo(todo) | |
@store.delete(todo['id']) | |
end | |
end | |
store = Store.new | |
todo_manager = TodoManager.new store | |
contract = Barrister::contract_from_file('../todo_manager.v1.json') | |
server = Barrister::Server.new(contract) | |
server.add_handler('TodoManager', todo_manager) | |
set :port, 3000 | |
post '/v1/todos' do | |
request.body.rewind | |
resp = server.handle_json(request.body.read) | |
status 200 | |
headers 'Content-Type' => 'application/json' | |
resp | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment