Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Gemfile.lock
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'
gem 'sinatra'

gem 'json'
gem 'json', '1.8.5'
gem 'data_mapper'

# When developing an app locally you can use SQLite which is a relational
Expand Down
51 changes: 30 additions & 21 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.2.8)
bcrypt-ruby (3.0.1)
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
bcrypt (3.1.11)
bcrypt-ruby (3.1.5)
bcrypt (>= 3.1.3)
data_mapper (1.2.0)
dm-aggregates (~> 1.2.0)
dm-constraints (~> 1.2.0)
Expand All @@ -13,14 +16,14 @@ GEM
dm-transactions (~> 1.2.0)
dm-types (~> 1.2.0)
dm-validations (~> 1.2.0)
data_objects (0.10.12)
data_objects (0.10.17)
addressable (~> 2.1)
dm-aggregates (1.2.0)
dm-core (~> 1.2.0)
dm-constraints (1.2.0)
dm-core (~> 1.2.0)
dm-core (1.2.0)
addressable (~> 2.2.6)
dm-core (1.2.1)
addressable (~> 2.3)
dm-do-adapter (1.2.0)
data_objects (~> 0.10.6)
dm-core (~> 1.2.0)
Expand Down Expand Up @@ -52,24 +55,27 @@ GEM
uuidtools (~> 2.1)
dm-validations (1.2.0)
dm-core (~> 1.2.0)
do_postgres (0.10.12)
data_objects (= 0.10.12)
do_sqlite3 (0.10.12)
data_objects (= 0.10.12)
do_postgres (0.10.17)
data_objects (= 0.10.17)
do_sqlite3 (0.10.17)
data_objects (= 0.10.17)
fastercsv (1.5.5)
json (1.7.7)
json_pure (1.7.7)
multi_json (1.6.1)
rack (1.5.2)
rack-protection (1.3.2)
json (1.8.5)
json_pure (1.8.6)
multi_json (1.12.2)
mustermann (1.0.1)
public_suffix (3.0.0)
rack (2.0.3)
rack-protection (2.0.0)
rack
sinatra (1.3.5)
rack (~> 1.4)
rack-protection (~> 1.3)
tilt (~> 1.3, >= 1.3.3)
sinatra (2.0.0)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.0)
tilt (~> 2.0)
stringex (1.5.1)
tilt (1.3.3)
uuidtools (2.1.3)
tilt (2.0.8)
uuidtools (2.1.5)

PLATFORMS
ruby
Expand All @@ -78,5 +84,8 @@ DEPENDENCIES
data_mapper
dm-postgres-adapter
dm-sqlite-adapter
json
json (= 1.8.5)
sinatra

BUNDLED WITH
1.15.4
108 changes: 6 additions & 102 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,107 +3,11 @@
require 'bundler'
Bundler.require

# Setup DataMapper with a database URL. On Heroku, ENV['DATABASE_URL'] will be
# set, when working locally this line will fall back to using SQLite in the
# current directory.
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite://#{Dir.pwd}/development.sqlite")

# Define a simple DataMapper model.
class Thing
include DataMapper::Resource

property :id, Serial, :key => true
property :created_at, DateTime
property :title, String, :length => 255
property :description, Text
end

# Finalize the DataMapper models.
DataMapper.finalize

# Tell DataMapper to update the database according to the definitions above.
DataMapper.auto_upgrade!

get '/' do
send_file './public/index.html'
end

# Route to show all Things, ordered like a blog
get '/things' do
content_type :json
@things = Thing.all(:order => :created_at.desc)

@things.to_json
end

# CREATE: Route to create a new Thing
post '/things' do
content_type :json

# These next commented lines are for if you are using Backbone.js
# JSON is sent in the body of the http request. We need to parse the body
# from a string into JSON
# params_json = JSON.parse(request.body.read)

# If you are using jQuery's ajax functions, the data goes through in the
# params.
@thing = Thing.new(params)

if @thing.save
@thing.to_json
else
halt 500
end
configure do
set :root, File.dirname('name')
end

# READ: Route to show a specific Thing based on its `id`
get '/things/:id' do
content_type :json
@thing = Thing.get(params[:id].to_i)

if @thing
@thing.to_json
else
halt 404
end
end

# UPDATE: Route to update a Thing
put '/things/:id' do
content_type :json

# These next commented lines are for if you are using Backbone.js
# JSON is sent in the body of the http request. We need to parse the body
# from a string into JSON
# params_json = JSON.parse(request.body.read)

# If you are using jQuery's ajax functions, the data goes through in the
# params.

@thing = Thing.get(params[:id].to_i)
@thing.update(params)

if @thing.save
@thing.to_json
else
halt 500
end
end

# DELETE: Route to delete a Thing
delete '/things/:id/delete' do
content_type :json
@thing = Thing.get(params[:id].to_i)

if @thing.destroy
{:success => "ok"}.to_json
else
halt 500
end
end

# If there are no Things in the database, add a few.
if Thing.count == 0
Thing.create(:title => "Test Thing One", :description => "Sometimes I eat pizza.")
Thing.create(:title => "Test Thing Two", :description => "Other times I eat cookies.")
end
# Models
require_relative 'models'
# Routes
require_relative 'routes'
4 changes: 2 additions & 2 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require './app'
run Sinatra::Application
require_relative 'app'
run Sinatra::Application
Binary file added development.sqlite
Binary file not shown.
25 changes: 25 additions & 0 deletions models.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Setup DataMapper with a database URL. On Heroku, ENV['DATABASE_URL'] will be
# set, when working locally this line will fall back to using SQLite in the
# current directory.
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite://#{Dir.pwd}/development.sqlite")

# Define a simple DataMapper model.
class Thing
include DataMapper::Resource

property :id, Serial, :key => true
property :created_at, DateTime
property :title, String, :length => 255
property :description, Text
end

# Finalize the DataMapper models.
DataMapper.finalize

# Tell DataMapper to update the database according to the definitions above.
DataMapper.auto_upgrade!

if Thing.count == 0
Thing.create(:title => "Test Thing One", :description => "Sometimes I eat pizza.")
Thing.create(:title => "Test Thing Two", :description => "Other times I eat cookies.")
end
77 changes: 77 additions & 0 deletions routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
get '/' do
erb :index
end

# Route to show all Things, ordered like a blog
get '/things' do
content_type :json
@things = Thing.all(:order => :created_at.desc)

@things.to_json
end

# CREATE: Route to create a new Thing
post '/things' do
content_type :json

# These next commented lines are for if you are using Backbone.js
# JSON is sent in the body of the http request. We need to parse the body
# from a string into JSON
# params_json = JSON.parse(request.body.read)

# If you are using jQuery's ajax functions, the data goes through in the
# params.
@thing = Thing.new(params)

if @thing.save
@thing.to_json
else
halt 500
end
end

# READ: Route to show a specific Thing based on its `id`
get '/things/:id' do
content_type :json
@thing = Thing.get(params[:id].to_i)

if @thing
@thing.to_json
else
halt 404
end
end

# UPDATE: Route to update a Thing
put '/things/:id' do
content_type :json

# These next commented lines are for if you are using Backbone.js
# JSON is sent in the body of the http request. We need to parse the body
# from a string into JSON
# params_json = JSON.parse(request.body.read)

# If you are using jQuery's ajax functions, the data goes through in the
# params.

@thing = Thing.get(params[:id].to_i)
@thing.update(params)

if @thing.save
@thing.to_json
else
halt 500
end
end

# DELETE: Route to delete a Thing
delete '/things/:id/delete' do
content_type :json
@thing = Thing.get(params[:id].to_i)

if @thing.destroy
{:success => "ok"}.to_json
else
halt 500
end
end
File renamed without changes.