Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add setup to use a database, add endpoint to create users

  • Loading branch information...
commit d9b2bc323712fe40ab752fe54b9ecee955679ec3 1 parent 20f0e89
@chischaschos authored
View
1  .rspec
@@ -0,0 +1 @@
+--color
View
9 Gemfile
@@ -1,6 +1,8 @@
source 'https://rubygems.org'
gem 'coffee-script'
+gem 'data_mapper'
+gem 'dm-sqlite-adapter'
gem 'ejs'
gem 'haml'
gem 'sinatra'
@@ -13,4 +15,11 @@ end
group :development, :test do
gem 'capybara-webkit'
gem 'cucumber'
+ gem 'debugger'
+end
+
+group :test do
+ gem 'database_cleaner'
+ gem 'json_spec'
+ gem 'rspec'
end
View
79 Gemfile.lock
@@ -1,6 +1,8 @@
GEM
remote: https://rubygems.org/
specs:
+ addressable (2.3.5)
+ bcrypt-ruby (3.1.2)
builder (3.2.2)
capybara (2.1.0)
mime-types (>= 1.16)
@@ -15,21 +17,82 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.7.0)
+ columnize (0.3.6)
cucumber (1.3.10)
builder (>= 2.1.2)
diff-lcs (>= 1.1.3)
gherkin (~> 2.12)
multi_json (>= 1.7.5, < 2.0)
multi_test (>= 0.0.2)
+ data_mapper (1.2.0)
+ dm-aggregates (~> 1.2.0)
+ dm-constraints (~> 1.2.0)
+ dm-core (~> 1.2.0)
+ dm-migrations (~> 1.2.0)
+ dm-serializer (~> 1.2.0)
+ dm-timestamps (~> 1.2.0)
+ dm-transactions (~> 1.2.0)
+ dm-types (~> 1.2.0)
+ dm-validations (~> 1.2.0)
+ data_objects (0.10.13)
+ addressable (~> 2.1)
+ database_cleaner (1.2.0)
+ debugger (1.6.5)
+ columnize (>= 0.3.1)
+ debugger-linecache (~> 1.2.0)
+ debugger-ruby_core_source (~> 1.3.1)
+ debugger-linecache (1.2.0)
+ debugger-ruby_core_source (1.3.1)
diff-lcs (1.2.5)
+ dm-aggregates (1.2.0)
+ dm-core (~> 1.2.0)
+ dm-constraints (1.2.0)
+ dm-core (~> 1.2.0)
+ dm-core (1.2.1)
+ addressable (~> 2.3)
+ dm-do-adapter (1.2.0)
+ data_objects (~> 0.10.6)
+ dm-core (~> 1.2.0)
+ dm-migrations (1.2.0)
+ dm-core (~> 1.2.0)
+ dm-serializer (1.2.2)
+ dm-core (~> 1.2.0)
+ fastercsv (~> 1.5)
+ json (~> 1.6)
+ json_pure (~> 1.6)
+ multi_json (~> 1.0)
+ dm-sqlite-adapter (1.2.0)
+ dm-do-adapter (~> 1.2.0)
+ do_sqlite3 (~> 0.10.6)
+ dm-timestamps (1.2.0)
+ dm-core (~> 1.2.0)
+ dm-transactions (1.2.0)
+ dm-core (~> 1.2.0)
+ dm-types (1.2.2)
+ bcrypt-ruby (~> 3.0)
+ dm-core (~> 1.2.0)
+ fastercsv (~> 1.5)
+ json (~> 1.6)
+ multi_json (~> 1.0)
+ stringex (~> 1.4)
+ uuidtools (~> 2.1)
+ dm-validations (1.2.0)
+ dm-core (~> 1.2.0)
+ do_sqlite3 (0.10.13)
+ data_objects (= 0.10.13)
ejs (1.1.1)
execjs (2.0.2)
+ fastercsv (1.5.5)
gherkin (2.12.2)
multi_json (~> 1.3)
haml (4.0.5)
tilt
hike (1.2.3)
json (1.8.1)
+ json_pure (1.8.1)
+ json_spec (1.1.1)
+ multi_json (~> 1.0)
+ rspec (~> 2.0)
mime-types (2.1)
mini_portile (0.5.2)
multi_json (1.8.4)
@@ -41,6 +104,14 @@ GEM
rack
rack-test (0.6.2)
rack (>= 1.0)
+ rspec (2.14.1)
+ rspec-core (~> 2.14.0)
+ rspec-expectations (~> 2.14.0)
+ rspec-mocks (~> 2.14.0)
+ rspec-core (2.14.7)
+ rspec-expectations (2.14.4)
+ diff-lcs (>= 1.1.3, < 2.0)
+ rspec-mocks (2.14.4)
shotgun (0.9)
rack (>= 1.0)
sinatra (1.3.6)
@@ -52,7 +123,9 @@ GEM
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
+ stringex (1.5.1)
tilt (1.4.1)
+ uuidtools (2.1.4)
xpath (2.0.0)
nokogiri (~> 1.3)
@@ -63,8 +136,14 @@ DEPENDENCIES
capybara-webkit
coffee-script
cucumber
+ data_mapper
+ database_cleaner
+ debugger
+ dm-sqlite-adapter
ejs
haml
+ json_spec
+ rspec
shotgun
sinatra
sprockets
View
3  config.ru
@@ -2,9 +2,6 @@ $LOAD_PATH << File.expand_path('lib')
require 'todo'
-
-Todo::Application.root = File.realpath(File.dirname(__FILE__))
-
map '/assets' do
environment = Sprockets::Environment.new Todo::Application.root
environment.append_path 'vendor/js'
View
1  lib/todo.rb
@@ -4,4 +4,5 @@
module Todo
autoload :Application, 'todo/application'
+ autoload :Models, 'todo/models'
end
View
16 lib/todo/application.rb
@@ -3,9 +3,25 @@
module Todo
class Application < Sinatra::Base
+ set :root, File.realpath(File.join(File.dirname(__FILE__), '..', '..'))
+
get '/' do
haml :index
end
+ post '/api/users' do
+ content_type :json
+ user = Models::User.new(params[:user])
+
+ if user.valid?
+ user.save!
+ user.to_json
+
+ else
+ status 404
+ { errors: user.errors.to_hash }.to_json
+ end
+ end
+
end
end
View
9 lib/todo/models.rb
@@ -0,0 +1,9 @@
+module Todo
+ module Models
+ require 'todo/models/db'
+ require 'todo/models/user'
+
+ DataMapper.finalize
+ DataMapper.auto_upgrade!
+ end
+end
View
2  lib/todo/models/db.rb
@@ -0,0 +1,2 @@
+DataMapper::Logger.new($stdout, :debug)
+DataMapper.setup(:default, "sqlite://#{File.join(Todo::Application.root, 'todos.db')}")
View
23 lib/todo/models/user.rb
@@ -0,0 +1,23 @@
+
+module Todo
+ module Models
+ class User
+ include DataMapper::Resource
+
+ property :id, Serial
+ property :email, String
+ property :password, String
+
+ validates_presence_of :email
+ validates_format_of :email, as: :email_address
+ validates_uniqueness_of :email
+
+ validates_presence_of :password
+
+ def to_json
+ { id: self.id }.to_json
+ end
+ end
+
+ end
+end
View
3  spec/api/sessions_spec.rb
@@ -0,0 +1,3 @@
+describe 'Sessions API' do
+ it 'a user can create a session'
+end
View
34 spec/api/users_spec.rb
@@ -0,0 +1,34 @@
+require 'spec_helper'
+
+describe 'Users API', api: true do
+
+ context 'when 200 response' do
+ it 'should success creating a user' do
+ expect(Todo::Models::User.count).to eq 0
+
+ params = { user: { email: 'test@test.com', password: '123test123' } }
+ post '/api/users', params
+
+ expect(last_response.headers['Content-Type']).to eq 'application/json;charset=utf-8'
+ expect(last_response.body).to have_json_path 'id'
+ expect(last_response.body).not_to have_json_path 'email'
+ expect(last_response.body).not_to have_json_path 'password'
+ expect(last_response.status).to eq 200
+ expect(Todo::Models::User.count).to eq 1
+ end
+ end
+
+ context 'when 404 response' do
+ it 'should fail because of empty password' do
+ expect(Todo::Models::User.count).to eq 0
+
+ params = { user: { email: 'test@test.com' } }
+ post '/api/users', params
+
+ expect(last_response.headers['Content-Type']).to eq 'application/json;charset=utf-8'
+ expect(last_response.body).to have_json_path 'errors/password'
+ expect(last_response.status).to eq 404
+ expect(Todo::Models::User.count).to eq 0
+ end
+ end
+end
View
23 spec/spec_helper.rb
@@ -0,0 +1,23 @@
+$LOAD_PATH << File.expand_path('lib')
+
+require 'todo'
+require 'database_cleaner'
+require 'json_spec'
+
+DatabaseCleaner[:data_mapper].strategy = :truncation
+
+Dir[File.join(Todo::Application.root, 'spec', 'support', '*')].each do |file|
+ require file
+end
+
+RSpec.configure do |config|
+
+ config.before(:each) do
+ DatabaseCleaner.start
+ end
+
+ config.after(:each) do
+ DatabaseCleaner.clean
+ end
+
+end
View
10 spec/support/shared_api_context.rb
@@ -0,0 +1,10 @@
+require 'rack/test'
+
+shared_context 'shared_api_context', api: true do
+ include Rack::Test::Methods
+
+ def app
+ Todo::Application
+ end
+
+end
View
BIN  todos.db
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.