Quick RESTful API

This commit is contained in:
Andrew Tomaka 2015-10-12 16:30:43 -04:00
commit f678a77677
12 changed files with 208 additions and 0 deletions

2
.bundle/config Normal file
View file

@ -0,0 +1,2 @@
---
BUNDLE_WITHOUT: production

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
db/*.db

16
Gemfile Normal file
View file

@ -0,0 +1,16 @@
source 'https://rubygems.org'
ruby "2.2.2"
gem 'activerecord'
gem 'sinatra'
gem 'sinatra-activerecord'
group :production do
gem 'pg'
end
group :development do
gem 'sqlite3'
gem 'rerun'
end

60
Gemfile.lock Normal file
View file

@ -0,0 +1,60 @@
GEM
remote: https://rubygems.org/
specs:
activemodel (4.2.4)
activesupport (= 4.2.4)
builder (~> 3.1)
activerecord (4.2.4)
activemodel (= 4.2.4)
activesupport (= 4.2.4)
arel (~> 6.0)
activesupport (4.2.4)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.3)
builder (3.2.2)
ffi (1.9.10)
i18n (0.7.0)
json (1.8.3)
listen (3.0.3)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
minitest (5.8.1)
pg (0.18.3)
rack (1.6.4)
rack-protection (1.5.3)
rack
rb-fsevent (0.9.6)
rb-inotify (0.9.5)
ffi (>= 0.5.0)
rerun (0.11.0)
listen (~> 3.0)
sinatra (1.4.6)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
sinatra-activerecord (2.0.9)
activerecord (>= 3.2)
sinatra (~> 1.0)
sqlite3 (1.3.11)
thread_safe (0.3.5)
tilt (2.0.1)
tzinfo (1.2.2)
thread_safe (~> 0.1)
PLATFORMS
ruby
DEPENDENCIES
activerecord
pg
rerun
sinatra
sinatra-activerecord
sqlite3
BUNDLED WITH
1.10.6

7
Rakefile Normal file
View file

@ -0,0 +1,7 @@
require './app'
require 'sinatra/activerecord/rake'
desc 'Starts the development server'
task :serve do
`bundle exec rerun -b rackup`
end

64
app.rb Normal file
View file

@ -0,0 +1,64 @@
require 'rubygems'
require 'bundler'
require 'bundler/setup'
Bundler.require
require './config/environments'
require './model/joke'
before do
content_type :json
response.headers['Access-Control-Allow-Origin'] = '*'
end
get '/jokes' do
Joke.all.to_json
end
post '/jokes' do
@joke = Joke.new(params[:joke])
if @joke.save
status 204
else
status 422
end
end
get '/jokes/:id' do
begin
@joke = Joke.find(params[:id])
rescue Exception
status 404
return
end
@joke.to_json
end
put '/jokes/:id' do
begin
@joke = Joke.find(params[:id])
rescue Exception
status 404
return
end
if @joke.update(params[:joke])
status 204
else
status 422
end
end
delete '/jokes/:id' do
begin
@joke = Joke.find(params[:id])
@joke.delete
rescue Exception
end
status 204
end

2
config.ru Normal file
View file

@ -0,0 +1,2 @@
require './app'
run Sinatra::Application

16
config/environments.rb Normal file
View file

@ -0,0 +1,16 @@
configure :development do
set :database, 'sqlite3:db/development.db'
end
configure :production do
db = URI.parse(ENV['DATABASE_URL'] || 'postgres:///localhost/mydb')
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
end

View file

@ -0,0 +1,9 @@
class CreateJokes < ActiveRecord::Migration
def change
create_table :jokes do |t|
t.string :joke
t.timestamps null: false
end
end
end

View file

@ -0,0 +1,5 @@
class AddPunchlineToJokes < ActiveRecord::Migration
def change
add_column :jokes, :punchline, :string
end
end

23
db/schema.rb Normal file
View file

@ -0,0 +1,23 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20151012201008) do
create_table "jokes", force: :cascade do |t|
t.string "joke"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "punchline"
end
end

3
model/joke.rb Normal file
View file

@ -0,0 +1,3 @@
class Joke < ActiveRecord::Base
end