Skip to content

Instantly share code, notes, and snippets.

@hendrikswan
Created December 20, 2011 21:29
Show Gist options
  • Save hendrikswan/1503366 to your computer and use it in GitHub Desktop.
Save hendrikswan/1503366 to your computer and use it in GitHub Desktop.
sample sinatra mongodb service
require 'sinatra'
require 'mongoid'
require 'json'
require "sinatra/reloader" if development?
Mongoid.load!("mongoid.yml")
class Price
include Mongoid::Document
field :share_id, type: Integer
field :date, type: Time
field :open, type: Integer
field :close, type: Integer
field :high, type: Integer
field :low, type: Integer
field :volume, type: Integer
end
class Company
include Mongoid::Document
field :code, type: String
field :sector, type: String
field :share_id, type: Integer
field :jse_code, type: Integer
end
get '/companies.json' do
content_type :json
all_companies = Company.all
all_companies.to_json
end
get '/companies/:code/prices.json' do
content_type :json
company = Company.where(code: params[:code])[0]
Price.where('share_id' => company.share_id)
.order_by('date', :desc)
.to_a
.reverse
.to_json
end
require 'sinatra'
require './app'
run Sinatra::Application
Dir["resources/*.rb"].each {|file| load file }
development:
host: localhost
database: cool_demo
production:
host: prod_host
port: 1000
username: main
password: blabla
database: cool_demo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment