Skip to content

Instantly share code, notes, and snippets.

@johnrc
Forked from hendrikswan/app.rb
Created April 26, 2014 14:59
Show Gist options
  • Save johnrc/11322248 to your computer and use it in GitHub Desktop.
Save johnrc/11322248 to your computer and use it in GitHub Desktop.
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