Skip to content

Instantly share code, notes, and snippets.

@StephanieSunshine
Created July 9, 2014 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StephanieSunshine/8c729d96d056f3383872 to your computer and use it in GitHub Desktop.
Save StephanieSunshine/8c729d96d056f3383872 to your computer and use it in GitHub Desktop.
Openshift Ruby Sinatra Mongo / Mongomapper Example / Skeleton
# This can easily be snapped into the Openshift Sinatra Quickstart to drop the information from a HTML Form Post into Mongodb.
# You do need to have the Mongodb cartridge installed for this to work correctly.
require 'sinatra'
require 'mongo_mapper'
require 'json'
class User
include MongoMapper::Document
key :firstName, String
key :nickName, String
key :lastName, String
key :email, String
key :phone, String
key :message, String
key :consumers, Boolean
key :charities, Boolean
key :merchants, Boolean
key :teammates, Boolean
end
configure do
MongoMapper.setup({'production' => {
'database' => ENV['OPENSHIFT_GEAR_NAME'],
'host' => ENV['OPENSHIFT_MONGODB_DB_HOST'],
'port' => ENV['OPENSHIFT_MONGODB_DB_PORT'],
'username' => ENV['OPENSHIFT_MONGODB_DB_USERNAME'],
'password' => ENV['OPENSHIFT_MONGODB_DB_PASSWORD']
}}, 'production')
end
# Easily export the data any number of ways. JSON was the best for our purposes
# get '/info' do
# User.all.to_json
# end
# Quite destructive
# get '/dropall' do
# User.delete_all
# end
# MongoMapper is the shit. Create a new record and then save it to the database.
post '/send_message' do
user = User.new( :firstName => params[:firstName] || '',
:nickName => params[:nickName] || '',
:lastName => params[:lastName] || '',
:email => params[:email] || '',
:phone => params[:phone] || '',
:message => params[:message] || '',
:consumers => params[:consumers] || false,
:charities => params[:charities] || false,
:merchants => params[:merchants] || false,
:teammates => params[:teammates] || false )
user.save!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment