Skip to content

Instantly share code, notes, and snippets.

@panupan
Created December 6, 2011 18:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panupan/1439361 to your computer and use it in GitHub Desktop.
Save panupan/1439361 to your computer and use it in GitHub Desktop.
Sinatra ActiveRecord Mock Server
source 'http://rubygems.org'
gem 'sinatra'
gem 'sinatra-activerecord'
gem 'sqlite3'
gem 'require_all'
require 'sinatra'
require 'sinatra/activerecord'
require 'require_all'
# Require ActiveRecord models
require_all '../app/models/*.rb'
# Use UTC timezone
Time.zone = "UTC"
ActiveRecord::Base.default_timezone = :utc
# Disable Concurrency
set :lock, true
before do
ActiveRecord::Base.logger.level = Logger::ERROR
# Setup in-memory Database
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
ActiveRecord::Migration.verbose = false
ActiveRecord::Migrator.up('../db/migrate')
ActiveRecord::Base.logger.level = Logger::DEBUG
end
after do
#nothing yet
end
post '/' do
"Hello World!"
end
# Example POST User
post '/users' do
post = JSON.parse(request.body.read)
puts post.inspect
begin
raise if User.find_by_id(post["user"]["id"])
@user = User.create!(post["user"])
content_type 'application/json'
status 201
response = @user.to_json
puts response
response
rescue
status 400
end
end
@panupan
Copy link
Author

panupan commented Dec 6, 2011

These instructions assume the files are put in your Rails application's spec folder.

Install gems:
$ cd spec
$ bundle install

Start server:
$ ruby -rubygems mock-server.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment