Skip to content

Instantly share code, notes, and snippets.

@mbround18
Forked from hendrikswan/app.rb
Created June 16, 2016 23:10
Show Gist options
  • Save mbround18/3cd86cd0a9379c1a090ad56afdca8b5c to your computer and use it in GitHub Desktop.
Save mbround18/3cd86cd0a9379c1a090ad56afdca8b5c 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