diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b844b14 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Gemfile.lock diff --git a/Gemfile b/Gemfile index ea7dea8..a067170 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index ec7324f..9026f7a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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 @@ -78,5 +84,8 @@ DEPENDENCIES data_mapper dm-postgres-adapter dm-sqlite-adapter - json + json (= 1.8.5) sinatra + +BUNDLED WITH + 1.15.4 diff --git a/app.rb b/app.rb index ced54dd..7c89c1c 100644 --- a/app.rb +++ b/app.rb @@ -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' diff --git a/config.ru b/config.ru index 785bf5c..665f125 100644 --- a/config.ru +++ b/config.ru @@ -1,2 +1,2 @@ -require './app' -run Sinatra::Application \ No newline at end of file +require_relative 'app' +run Sinatra::Application diff --git a/development.sqlite b/development.sqlite new file mode 100644 index 0000000..68f0631 Binary files /dev/null and b/development.sqlite differ diff --git a/models.rb b/models.rb new file mode 100644 index 0000000..11887b2 --- /dev/null +++ b/models.rb @@ -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 diff --git a/routes.rb b/routes.rb new file mode 100644 index 0000000..7784c9c --- /dev/null +++ b/routes.rb @@ -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 diff --git a/public/index.html b/views/index.erb similarity index 100% rename from public/index.html rename to views/index.erb