Skip to content

Instantly share code, notes, and snippets.

@fooqri
Last active December 11, 2015 05: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 fooqri/4555788 to your computer and use it in GitHub Desktop.
Save fooqri/4555788 to your computer and use it in GitHub Desktop.
require 'goliath'
require 'em-synchrony'
require 'em-synchrony/em-mongo'
require 'grape'
require './app/models/mission.rb'
require './app/models/player.rb'
module MyAppName
class API_v1 < Grape::API
prefix 'api'
version 'v1', :using => :header, :vendor => 'company_id' do
format :json
resource 'missions' do
# All mission API endpoints go here...
# ...
desc "Get a mission record using its id as key", {
:params => {
"id" => { :id => "Mission id.", :required => true }
}
}
get "/:id" do
error! 'Unauthorized', 401 unless env['HTTP_SECRETK'] == 'a secret key'
missions_json = env.missions_coll.find({'_id' => params[:id]})
error! 'Mission not found', 404 unless missions_json != []
mission = missions_json.map {|i| Mission.from_json(i)}.first
# map returns an array of 1 item, so get first as the mission object
# do something with the mission object here
# then return object to caller as json
mission.first.to_json
end
#...
resource 'players' do
# All player API endpoints go here...
# ...
desc "Get a player record using its id as key", {
:params => {
"id" => { :id => "Player id.", :required => true }
}
}
get "/:id" do
error! 'Unauthorized', 401 unless env['HTTP_SECRETK'] == 'a secret key'
players_json = env.players_coll.find({'_id' => params[:id]})
error! 'Player not found', 404 unless players_json != []
players = players_json.map {|i| Player.from_json(i)}
players.first.to_json
end
#...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment