Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement basic user endpoint #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ build-iPhoneSimulator/
# .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
.rvmrc

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should create a comment informing the purpose of db.json here

db.json
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
source "https://rubygems.org"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alphabetize dependencies here

gem "roda"
gem "puma"
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
puma (3.12.0)
rack (2.0.5)
roda (3.13.0)
rack
Expand All @@ -9,6 +10,7 @@ PLATFORMS
ruby

DEPENDENCIES
puma
roda

BUNDLED WITH
Expand Down
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -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 }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No exception handling here or on Post.

Try . . .

begin
  # user = users.find . . .
rescue => e
  # return exception / response code here 

{ 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
3 changes: 3 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# config.ru

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try like this according to Roda best practices:

# config.ru -- configuration file for rackup
require_relative './app'

map '/' do
  run App.freeze.app
end

require './app'
run App