socialforge/app/api/mobile/api.rb

50 lines
1.2 KiB
Ruby
Raw Normal View History

2014-11-27 19:43:04 +08:00
module Mobile
2015-01-28 09:25:58 +08:00
require_relative 'middleware/error_handler'
2015-01-28 10:31:30 +08:00
require_relative 'apis/auth'
require_relative 'apis/users'
require_relative 'apis/courses'
require_relative 'apis/watches'
require_relative 'apis/upgrade'
require_relative 'apis/homeworks'
2015-02-04 13:47:52 +08:00
require_relative 'apis/comments'
2014-11-27 19:43:04 +08:00
class API < Grape::API
version 'v1', using: :path
format :json
2014-12-03 17:28:19 +08:00
content_type :json, "application/json;charset=UTF-8"
2014-12-10 17:29:39 +08:00
use Mobile::Middleware::ErrorHandler
2014-11-27 19:43:04 +08:00
helpers do
def logger
API.logger
end
2014-12-10 11:56:33 +08:00
def authenticate!
raise('Unauthorized. Invalid or expired token.') unless current_user
2014-11-27 19:43:04 +08:00
end
def current_user
token = ApiKey.where(access_token: params[:token]).first
if token && !token.expired?
@current_user = User.find(token.user_id)
else
nil
end
end
end
mount Apis::Auth
mount Apis::Users
mount Apis::Courses
2014-12-10 16:23:56 +08:00
mount Apis::Watches
2015-01-04 11:53:16 +08:00
mount Apis::Upgrade
2015-01-05 19:53:25 +08:00
mount Apis::Homeworks
mount Apis::Comments
2014-11-27 19:43:04 +08:00
#add_swagger_documentation ({api_version: 'v1', base_path: 'http://u06.shellinfo.cn/trustie/api'})
2015-03-06 15:37:24 +08:00
#add_swagger_documentation ({api_version: 'v1', base_path: '/api'}) if Rails.env.development?
2015-01-05 10:15:14 +08:00
2014-11-27 19:43:04 +08:00
end
end