diff --git a/.gitignore b/.gitignore index f40aa6a..431e652 100644 --- a/.gitignore +++ b/.gitignore @@ -49,4 +49,6 @@ build-iPhoneSimulator/ # .ruby-gemset # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: -.rvmrc \ No newline at end of file +.rvmrc + +db.json \ No newline at end of file diff --git a/Gemfile b/Gemfile index 7227c9d..7a3036b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,3 @@ source "https://rubygems.org" gem "roda" +gem "puma" diff --git a/Gemfile.lock b/Gemfile.lock index ab0569e..f6c3af8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,7 @@ GEM remote: https://rubygems.org/ specs: + puma (3.12.0) rack (2.0.5) roda (3.13.0) rack @@ -9,6 +10,7 @@ PLATFORMS ruby DEPENDENCIES + puma roda BUNDLED WITH diff --git a/README.md b/README.md index 6c21ee3..6490ad6 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,53 @@ fufill all your goalsetting and project completion needs. Whether you need to finish a research paper or learn a new language. ## Usage +Download the repository +```bash +git clone https://github.com/coolfriends/goforthegoal-backend.git +``` + +Install ruby deps +```bash +bundle install +``` + +Check the available Rake tasks +```bash +rake -T +``` + +Create an empty DB +```bash +rake db.json +``` + +Start the server +```bash +sudo bundle exec puma config.ru -p 80 +``` + +Create a new user (this stores in plaintext for now) +``` +curl -d '{"email":"kyri", "password":" wow"}' \ + -H "Content-Type: application/json" \ + -X POST http://localhost:80/api/v1/users +``` + +Get all users +``` +curl http://localhost:80/api/v1/users +``` + +Get one user +``` +curl http://localhost:80/api/v1/users/kyri +``` + +Regenerate the DB +``` +rm db.json +rake db.json +``` ## Development diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..a3d130a --- /dev/null +++ b/Rakefile @@ -0,0 +1,15 @@ +#!/usr/bin/env rake +# Rakefile +require 'json' + +task :app do + require "./app" +end + +desc 'Create a DB file' +file 'db.json' do + empty_db = { + users: [] + } + File.write 'db.json', empty_db.to_json +end diff --git a/app.rb b/app.rb new file mode 100644 index 0000000..31adc74 --- /dev/null +++ b/app.rb @@ -0,0 +1,42 @@ +# app.rb +require 'roda' +require 'logger' +require 'json' + + +class App < Roda + plugin :json + + db = JSON.parse(File.read('db.json')) + users = db['users'] + route do |r| + # /api + r.on 'api' do + # /api/v1 + r.on 'v1' do + # /api/v1/users + r.on 'users' do + # GET /api/v1/users/{email} + r.get String do |email| + user = users.find { |u| u['email'] == email } + { user: user } + end + # GET /api/v1/users + r.get do + users + end + # POST /api/v1/users + r.post do + j = JSON.parse r.body.read + users << { + email: j['email'], + password: j['password'] + } + File.write('db.json', db.to_json) + { status: 'Success', email: j['email'] } + end + end + end + end + end +end diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..01702c9 --- /dev/null +++ b/config.ru @@ -0,0 +1,3 @@ +# config.ru +require './app' +run App