Created
February 20, 2012 06:55
-
-
Save noomerikal/1868181 to your computer and use it in GitHub Desktop.
kodagraph api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'mongoid' | |
require 'json' | |
require 'jsonp' | |
require 'rack/throttle' | |
require 'dalli' | |
set :cache, Dalli::Client.new("localhost:11211") | |
set :short_ttl, 400 | |
set :long_ttl, 4600 | |
enable :enable_cache | |
# need to subclass throttling strategy to override #client_identifier with username + api_key | |
use Rack::Throttle::Hourly, :max => 100, :cache => settings.cache, :key_prefix => :throttle | |
# Accepts: application/vnd.kodagraph.VERSION.SPECIFIC_RESOURCE_FORMAT+MEDIA_TYPE | |
# application/json - latest and greatest | |
# application/vnd.kodagraph+json - latest and greatest | |
# application/vnd.kodagraph.v1+json - pegged to version 1 | |
# application/vnd.kodagraph.v1.raw+json - pegged to version 1 and raw formatting (raw,text,html,full) | |
configure do | |
Mongoid.configure do |config| | |
name = "kodagraph_development" | |
host = "localhost" | |
config.master = Mongo::Connection.new.db(name) | |
config.persist_in_safe_mode = false | |
end | |
end | |
# Models | |
class Player | |
include Mongoid::Document | |
end | |
class AmericanFootballPlayerStatistic | |
include Mongoid::Document | |
end | |
class Team | |
include Mongoid::Document | |
end | |
class Caller | |
include Mongoid::Document | |
end | |
# Helpers | |
helpers do | |
def request_headers | |
env.inject({}){|acc, (k,v)| acc[k] = v; acc} | |
end | |
def protected! | |
unless authorized? | |
response['WWW-Authenticate'] = %(Basic realm="Restricted Area") | |
throw(:halt, [401, "Not authenticated\n"]) | |
end | |
end | |
def authorized? | |
@accepts = request.accept[0] | |
@api_key = request.env['HTTP_X_KODAGRAPH_API_KEY'] | |
@version = @accepts.match(/v(\d+)/) | |
@auth ||= Rack::Auth::Basic::Request.new(request.env) | |
if @auth.provided? && @auth.basic? && @auth.credentials | |
caller = Caller.where(login: @auth.credentials[0], password: @auth.credentials[1]) | |
caller.api_key == @api_key | |
else | |
false | |
end | |
end | |
def json_status(code, reason) | |
status code { :status => code, :reason => reason }.to_json | |
end | |
def deslug(slug) | |
slug.gsub(/\.json|\.xml/,'').split('-') | |
end | |
end | |
# Filters | |
before do | |
content_type :json | |
protected! | |
end | |
get '/' do | |
"hi!" | |
end | |
################### | |
# PLAYERS # | |
################### | |
# /nfl/players | |
# /cfb/players | |
get '/:type/players' do | |
(Player.where(type: params[:type])).to_json | |
end | |
# /nfl/players/joe-flacco | |
# /cfb/players/foo-lattimore | |
get '/:type/players/:id' do | |
(Player.where( | |
type: params[:type], | |
first_name: deslug(params[:id])[0], | |
last_name: deslug(params[:id])[1]) | |
).to_json | |
end | |
# /nfl/baltimore-ravens/players/qb/joe-flacco | |
# /cfb/maryland-terrapins/players/rb/foo-lattimore | |
get '/nfl/:team/players/:pos/:id' do | |
(Player.where( | |
current_team: deslug(params[:team]).join(' '), | |
pos: params[:pos], | |
first_name: deslug(params[:id])[0], | |
last_name: deslug(params[:id])[1]) | |
)[0].to_json | |
end | |
# /nfl/baltimore-ravens/players/joe-flacco | |
# /cfb/maryland-terrapins/players/foo-lattimore | |
get '/nfl/:team/players/:id' do | |
(Player.where( | |
current_team: deslug(params[:team]).join(' '), | |
first_name: deslug(params[:id])[0], | |
last_name: deslug(params[:id])[1]) | |
)[0].to_json | |
end | |
# /nfl/baltimore-ravens/players/qb/joe-flacco/stats/2010 | |
get '/nfl/:team/players/:pos/:player/stats/:season' do | |
(AmericanFootballPlayerStatistic.where( | |
team: deslug(params[:team]).join(' '), | |
pos: params[:pos], | |
first_name: deslug(params[:player])[0], | |
last_name: deslug(params[:player])[1], | |
season: params[:season]) | |
).to_json | |
end | |
# /nfl/baltimore-ravens/players/qb/joe-flacco/stats/steelers-vs-ravens-week-1-2010 | |
# /nfl/baltimore-ravens/players/qb/joe-flacco/stats/2010/week-1/steelers-vs-ravens | |
['/nfl/:team/players/:pos/:player/stats/:event','/nfl/:team/players/:jersey/:player/stats/:year/:week/:game'].each do |path| | |
get path do | |
(AmericanFootballPlayerStatistic.where( | |
team: deslug(params[:team]).join(' '), | |
pos: params[:pos], | |
first_name: deslug(params[:player])[0], | |
last_name: deslug(params[:player])[1], | |
american_football_event_id: params[:event]) | |
).to_json | |
end | |
end | |
put '*' do | |
json_status 501, "Not implemented." | |
end | |
post '*' do | |
json_status 501, "Not implemented." | |
end | |
delete '*' do | |
json_status 501, "Not implemented." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment