commit f678a776776d41144cf06e7861165a93259fe8cf Author: Andrew Tomaka Date: Mon Oct 12 16:30:43 2015 -0400 Quick RESTful API diff --git a/.bundle/config b/.bundle/config new file mode 100644 index 0000000..1c9fb93 --- /dev/null +++ b/.bundle/config @@ -0,0 +1,2 @@ +--- +BUNDLE_WITHOUT: production diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2e274d9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +db/*.db diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..d01d958 --- /dev/null +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..b234b50 --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..085a864 --- /dev/null +++ b/Rakefile @@ -0,0 +1,7 @@ +require './app' +require 'sinatra/activerecord/rake' + +desc 'Starts the development server' +task :serve do + `bundle exec rerun -b rackup` +end diff --git a/app.rb b/app.rb new file mode 100644 index 0000000..60cfbeb --- /dev/null +++ b/app.rb @@ -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 diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..76a6edf --- /dev/null +++ b/config.ru @@ -0,0 +1,2 @@ +require './app' +run Sinatra::Application diff --git a/config/environments.rb b/config/environments.rb new file mode 100644 index 0000000..0f7386a --- /dev/null +++ b/config/environments.rb @@ -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 diff --git a/db/migrate/20151012185754_create_jokes.rb b/db/migrate/20151012185754_create_jokes.rb new file mode 100644 index 0000000..222bc1f --- /dev/null +++ b/db/migrate/20151012185754_create_jokes.rb @@ -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 diff --git a/db/migrate/20151012201008_add_punchline_to_jokes.rb b/db/migrate/20151012201008_add_punchline_to_jokes.rb new file mode 100644 index 0000000..524853f --- /dev/null +++ b/db/migrate/20151012201008_add_punchline_to_jokes.rb @@ -0,0 +1,5 @@ +class AddPunchlineToJokes < ActiveRecord::Migration + def change + add_column :jokes, :punchline, :string + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..82ce4de --- /dev/null +++ b/db/schema.rb @@ -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 diff --git a/model/joke.rb b/model/joke.rb new file mode 100644 index 0000000..3fc5c90 --- /dev/null +++ b/model/joke.rb @@ -0,0 +1,3 @@ +class Joke < ActiveRecord::Base + +end