daily pastebin goal
37%
SHARE
TWEET

Untitled

a guest Sep 12th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'sinatra'
  2. require 'mongoid'
  3. require 'json'
  4. require 'jsonp'
  5. require 'rack/throttle'
  6. require 'dalli'
  7.  
  8. set :cache, Dalli::Client.new("localhost:11211")
  9. set :short_ttl, 400
  10. set :long_ttl, 4600
  11.  
  12. enable :enable_cache
  13.  
  14. # need to subclass throttling strategy to override #client_identifier with username + api_key
  15. use Rack::Throttle::Hourly, :max => 100, :cache => settings.cache, :key_prefix => :throttle
  16.  
  17. # Accepts: application/vnd.kodagraph.VERSION.SPECIFIC_RESOURCE_FORMAT+MEDIA_TYPE
  18. # application/json - latest and greatest
  19. # application/vnd.kodagraph+json - latest and greatest
  20. # application/vnd.kodagraph.v1+json - pegged to version 1
  21. # application/vnd.kodagraph.v1.raw+json - pegged to version 1 and raw formatting (raw,text,html,full)
  22.  
  23. configure do
  24.   Mongoid.configure do |config|
  25.     name = "kodagraph_development"
  26.     host = "localhost"
  27.     config.master = Mongo::Connection.new.db(name)
  28.     config.persist_in_safe_mode = false
  29.   end
  30. end
  31.  
  32. # Models
  33. class Player
  34.   include Mongoid::Document
  35. end
  36.  
  37. class AmericanFootballPlayerStatistic
  38.   include Mongoid::Document
  39. end
  40.  
  41. class Team
  42.   include Mongoid::Document
  43. end
  44.  
  45. class Caller
  46.   include Mongoid::Document
  47. end
  48.  
  49.  
  50. # Helpers
  51. helpers do
  52.  
  53.   def request_headers
  54.     env.inject({}){|acc, (k,v)| acc[k] = v; acc}
  55.   end
  56.  
  57.   def protected!
  58.     unless authorized?
  59.       response['WWW-Authenticate'] = %(Basic realm="Restricted Area")
  60.       throw(:halt, [401, "Not authenticated\n"])
  61.     end
  62.   end
  63.  
  64.   def authorized?
  65.     @accepts = request.accept[0]
  66.     @api_key = request.env['HTTP_X_KODAGRAPH_API_KEY']
  67.     @version = @accepts.match(/v(\d+)/)
  68.     @auth ||= Rack::Auth::Basic::Request.new(request.env)
  69.  
  70.     if @auth.provided? && @auth.basic? && @auth.credentials
  71.       caller = Caller.where(login: @auth.credentials[0], password: @auth.credentials[1])
  72.       caller.api_key == @api_key
  73.     else
  74.       false
  75.     end
  76.   end
  77.  
  78.   def json_status(code, reason)
  79.     status code { :status => code, :reason => reason }.to_json
  80.   end
  81.  
  82.   def deslug(slug)
  83.     slug.gsub(/\.json|\.xml/,'').split('-')
  84.   end
  85. end
  86.  
  87. # Filters
  88. before do
  89.   content_type :json
  90.   protected!
  91. end
  92.  
  93. get '/' do
  94.   "hi!"
  95. end
  96.  
  97. ###################
  98. #     PLAYERS     #
  99. ###################
  100.  
  101. # /nfl/players
  102. # /cfb/players
  103. get '/:type/players' do
  104.   (Player.where(type: params[:type])).to_json
  105. end
  106.  
  107. # /nfl/players/joe-flacco
  108. # /cfb/players/foo-lattimore
  109. get '/:type/players/:id' do
  110.   (Player.where(
  111.     type:       params[:type],
  112.     first_name: deslug(params[:id])[0],
  113.     last_name:  deslug(params[:id])[1])
  114.   ).to_json
  115. end
  116.  
  117. # /nfl/baltimore-ravens/players/qb/joe-flacco
  118. # /cfb/maryland-terrapins/players/rb/foo-lattimore
  119. get '/nfl/:team/players/:pos/:id' do
  120.   (Player.where(
  121.     current_team: deslug(params[:team]).join(' '),
  122.     pos:          params[:pos],
  123.     first_name:   deslug(params[:id])[0],
  124.     last_name:    deslug(params[:id])[1])
  125.   )[0].to_json
  126. end
  127.  
  128. # /nfl/baltimore-ravens/players/joe-flacco
  129. # /cfb/maryland-terrapins/players/foo-lattimore
  130. get '/nfl/:team/players/:id' do
  131.   (Player.where(
  132.     current_team: deslug(params[:team]).join(' '),
  133.     first_name:   deslug(params[:id])[0],
  134.     last_name:    deslug(params[:id])[1])
  135.   )[0].to_json
  136. end
  137.  
  138. # /nfl/baltimore-ravens/players/qb/joe-flacco/stats/2010
  139. get '/nfl/:team/players/:pos/:player/stats/:season' do
  140.   (AmericanFootballPlayerStatistic.where(
  141.     team:       deslug(params[:team]).join(' '),
  142.     pos:        params[:pos],
  143.     first_name: deslug(params[:player])[0],
  144.     last_name:  deslug(params[:player])[1],
  145.     season:     params[:season])
  146.   ).to_json
  147. end
  148.  
  149. # /nfl/baltimore-ravens/players/qb/joe-flacco/stats/steelers-vs-ravens-week-1-2010
  150. # /nfl/baltimore-ravens/players/qb/joe-flacco/stats/2010/week-1/steelers-vs-ravens
  151. ['/nfl/:team/players/:pos/:player/stats/:event','/nfl/:team/players/:jersey/:player/stats/:year/:week/:game'].each do |path|
  152.   get path do
  153.     (AmericanFootballPlayerStatistic.where(
  154.       team:                       deslug(params[:team]).join(' '),
  155.       pos:                        params[:pos],
  156.       first_name:                 deslug(params[:player])[0],
  157.       last_name:                  deslug(params[:player])[1],
  158.       american_football_event_id: params[:event])
  159.     ).to_json
  160.   end
  161. end
  162.  
  163. put '*' do
  164.   json_status 501, "Not implemented."
  165. end
  166.  
  167. post '*' do
  168.   json_status 501, "Not implemented."
  169. end
  170.  
  171. delete '*' do
  172.   json_status 501, "Not implemented."
  173. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top