Create a gist now

Instantly share code, notes, and snippets.

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