Merge branch 'szzh' of http://repository.trustie.net/xianbo/trustie2 into szzh
Conflicts: Gemfilezh
commit
2acca17e27
@ -0,0 +1,345 @@
|
||||
GIT
|
||||
remote: https://github.com/guange2015/grape-swagger-ui.git
|
||||
revision: 4c33439f236c174ae0e774b3435ef2547995c21d
|
||||
specs:
|
||||
grape-swagger-ui (0.0.4)
|
||||
railties (>= 3.1)
|
||||
|
||||
PATH
|
||||
remote: lib/better_errors
|
||||
specs:
|
||||
better_errors (1.1.0)
|
||||
coderay (>= 1.0.0)
|
||||
erubis (>= 2.6.6)
|
||||
|
||||
PATH
|
||||
remote: lib/rack-mini-profiler
|
||||
specs:
|
||||
rack-mini-profiler (0.9.1)
|
||||
rack (>= 1.1.3)
|
||||
|
||||
PATH
|
||||
remote: lib/seems_rateable
|
||||
specs:
|
||||
seems_rateable (1.0.13)
|
||||
jquery-rails
|
||||
rails
|
||||
|
||||
GEM
|
||||
remote: http://rubygems.org/
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
actionmailer (3.2.13)
|
||||
actionpack (= 3.2.13)
|
||||
mail (~> 2.5.3)
|
||||
actionpack (3.2.13)
|
||||
activemodel (= 3.2.13)
|
||||
activesupport (= 3.2.13)
|
||||
builder (~> 3.0.0)
|
||||
erubis (~> 2.7.0)
|
||||
journey (~> 1.0.4)
|
||||
rack (~> 1.4.5)
|
||||
rack-cache (~> 1.2)
|
||||
rack-test (~> 0.6.1)
|
||||
sprockets (~> 2.2.1)
|
||||
activemodel (3.2.13)
|
||||
activesupport (= 3.2.13)
|
||||
builder (~> 3.0.0)
|
||||
activerecord (3.2.13)
|
||||
activemodel (= 3.2.13)
|
||||
activesupport (= 3.2.13)
|
||||
arel (~> 3.0.2)
|
||||
tzinfo (~> 0.3.29)
|
||||
activeresource (3.2.13)
|
||||
activemodel (= 3.2.13)
|
||||
activesupport (= 3.2.13)
|
||||
activesupport (3.2.13)
|
||||
i18n (= 0.6.1)
|
||||
multi_json (~> 1.0)
|
||||
acts-as-taggable-on (2.4.1)
|
||||
rails (>= 3, < 5)
|
||||
arel (3.0.3)
|
||||
axiom-types (0.1.1)
|
||||
descendants_tracker (~> 0.0.4)
|
||||
ice_nine (~> 0.11.0)
|
||||
thread_safe (~> 0.3, >= 0.3.1)
|
||||
builder (3.0.0)
|
||||
capybara (2.4.1)
|
||||
mime-types (>= 1.16)
|
||||
nokogiri (>= 1.3.3)
|
||||
rack (>= 1.0.0)
|
||||
rack-test (>= 0.5.4)
|
||||
xpath (~> 2.0)
|
||||
celluloid (0.16.0)
|
||||
timers (~> 4.0.0)
|
||||
childprocess (0.5.3)
|
||||
ffi (~> 1.0, >= 1.0.11)
|
||||
climate_control (0.0.3)
|
||||
activesupport (>= 3.0)
|
||||
cocaine (0.5.4)
|
||||
climate_control (>= 0.0.3, < 1.0)
|
||||
coderay (1.0.9)
|
||||
coercible (1.0.0)
|
||||
descendants_tracker (~> 0.0.1)
|
||||
coffee-rails (3.2.2)
|
||||
coffee-script (>= 2.2.0)
|
||||
railties (~> 3.2.0)
|
||||
coffee-script (2.3.0)
|
||||
coffee-script-source
|
||||
execjs
|
||||
coffee-script-source (1.7.1)
|
||||
descendants_tracker (0.0.4)
|
||||
thread_safe (~> 0.3, >= 0.3.1)
|
||||
diff-lcs (1.2.5)
|
||||
equalizer (0.0.9)
|
||||
erubis (2.7.0)
|
||||
execjs (2.2.1)
|
||||
factory_girl (4.4.0)
|
||||
activesupport (>= 3.0.0)
|
||||
fastercsv (1.5.5)
|
||||
ffi (1.9.3)
|
||||
ffi (1.9.3-x86-mingw32)
|
||||
formatador (0.2.5)
|
||||
grape (0.9.0)
|
||||
activesupport
|
||||
builder
|
||||
hashie (>= 2.1.0)
|
||||
multi_json (>= 1.3.2)
|
||||
multi_xml (>= 0.5.2)
|
||||
rack (>= 1.3.0)
|
||||
rack-accept
|
||||
rack-mount
|
||||
virtus (>= 1.0.0)
|
||||
grape-entity (0.4.4)
|
||||
activesupport
|
||||
multi_json (>= 1.3.2)
|
||||
grape-swagger (0.8.0)
|
||||
grape
|
||||
grape-entity
|
||||
guard (2.11.1)
|
||||
formatador (>= 0.2.4)
|
||||
listen (~> 2.7)
|
||||
lumberjack (~> 1.0)
|
||||
nenv (~> 0.1)
|
||||
notiffany (~> 0.0)
|
||||
pry (>= 0.9.12)
|
||||
shellany (~> 0.0)
|
||||
thor (>= 0.18.1)
|
||||
guard-rspec (2.5.0)
|
||||
guard (>= 1.1)
|
||||
rspec (~> 2.11)
|
||||
hashie (3.3.1)
|
||||
hike (1.2.3)
|
||||
hitimes (1.2.2)
|
||||
hitimes (1.2.2-x86-mingw32)
|
||||
htmlentities (4.3.2)
|
||||
i18n (0.6.1)
|
||||
ice_nine (0.11.0)
|
||||
journey (1.0.4)
|
||||
jquery-rails (2.0.3)
|
||||
railties (>= 3.1.0, < 5.0)
|
||||
thor (~> 0.14)
|
||||
json (1.8.1)
|
||||
kaminari (0.16.1)
|
||||
actionpack (>= 3.0.0)
|
||||
activesupport (>= 3.0.0)
|
||||
libv8 (3.16.14.3)
|
||||
listen (2.8.5)
|
||||
celluloid (>= 0.15.2)
|
||||
rb-fsevent (>= 0.9.3)
|
||||
rb-inotify (>= 0.9)
|
||||
lumberjack (1.0.9)
|
||||
mail (2.5.4)
|
||||
mime-types (~> 1.16)
|
||||
treetop (~> 1.4.8)
|
||||
metaclass (0.0.4)
|
||||
method_source (0.8.2)
|
||||
mime-types (1.25.1)
|
||||
mini_portile (0.6.0)
|
||||
mocha (1.1.0)
|
||||
metaclass (~> 0.0.1)
|
||||
multi_json (1.10.1)
|
||||
multi_xml (0.5.5)
|
||||
mysql2 (0.3.11)
|
||||
mysql2 (0.3.11-x86-mingw32)
|
||||
nenv (0.2.0)
|
||||
net-ldap (0.3.1)
|
||||
nokogiri (1.6.3)
|
||||
mini_portile (= 0.6.0)
|
||||
nokogiri (1.6.3-x86-mingw32)
|
||||
mini_portile (= 0.6.0)
|
||||
notiffany (0.0.3)
|
||||
nenv (~> 0.1)
|
||||
shellany (~> 0.0)
|
||||
paperclip (3.5.4)
|
||||
activemodel (>= 3.0.0)
|
||||
activesupport (>= 3.0.0)
|
||||
cocaine (~> 0.5.3)
|
||||
mime-types
|
||||
polyglot (0.3.5)
|
||||
pry (0.9.12.6)
|
||||
coderay (~> 1.0)
|
||||
method_source (~> 0.8)
|
||||
slop (~> 3.4)
|
||||
pry (0.9.12.6-x86-mingw32)
|
||||
coderay (~> 1.0)
|
||||
method_source (~> 0.8)
|
||||
slop (~> 3.4)
|
||||
win32console (~> 1.3)
|
||||
rack (1.4.5)
|
||||
rack-accept (0.4.5)
|
||||
rack (>= 0.4)
|
||||
rack-cache (1.2)
|
||||
rack (>= 0.4)
|
||||
rack-mount (0.8.3)
|
||||
rack (>= 1.0.0)
|
||||
rack-openid (1.4.2)
|
||||
rack (>= 1.1.0)
|
||||
ruby-openid (>= 2.1.8)
|
||||
rack-raw-upload (1.1.1)
|
||||
multi_json
|
||||
rack-ssl (1.3.4)
|
||||
rack
|
||||
rack-test (0.6.2)
|
||||
rack (>= 1.0)
|
||||
rails (3.2.13)
|
||||
actionmailer (= 3.2.13)
|
||||
actionpack (= 3.2.13)
|
||||
activerecord (= 3.2.13)
|
||||
activeresource (= 3.2.13)
|
||||
activesupport (= 3.2.13)
|
||||
bundler (~> 1.0)
|
||||
railties (= 3.2.13)
|
||||
railties (3.2.13)
|
||||
actionpack (= 3.2.13)
|
||||
activesupport (= 3.2.13)
|
||||
rack-ssl (~> 1.3.2)
|
||||
rake (>= 0.8.7)
|
||||
rdoc (~> 3.4)
|
||||
thor (>= 0.14.6, < 2.0)
|
||||
rake (10.3.2)
|
||||
rb-fsevent (0.9.4)
|
||||
rb-inotify (0.9.5)
|
||||
ffi (>= 0.5.0)
|
||||
rdoc (3.12.2)
|
||||
json (~> 1.4)
|
||||
ref (1.0.5)
|
||||
rich (1.4.6)
|
||||
jquery-rails
|
||||
kaminari
|
||||
mime-types
|
||||
paperclip
|
||||
rack-raw-upload
|
||||
rails (>= 3.2.0)
|
||||
sass-rails
|
||||
rspec (2.13.0)
|
||||
rspec-core (~> 2.13.0)
|
||||
rspec-expectations (~> 2.13.0)
|
||||
rspec-mocks (~> 2.13.0)
|
||||
rspec-core (2.13.1)
|
||||
rspec-expectations (2.13.0)
|
||||
diff-lcs (>= 1.1.3, < 2.0)
|
||||
rspec-mocks (2.13.1)
|
||||
rspec-rails (2.13.1)
|
||||
actionpack (>= 3.0)
|
||||
activesupport (>= 3.0)
|
||||
railties (>= 3.0)
|
||||
rspec-core (~> 2.13.0)
|
||||
rspec-expectations (~> 2.13.0)
|
||||
rspec-mocks (~> 2.13.0)
|
||||
ruby-ole (1.2.11.7)
|
||||
ruby-openid (2.1.8)
|
||||
rubyzip (1.1.6)
|
||||
sass (3.3.10)
|
||||
sass-rails (3.2.6)
|
||||
railties (~> 3.2.0)
|
||||
sass (>= 3.1.10)
|
||||
tilt (~> 1.3)
|
||||
selenium-webdriver (2.42.0)
|
||||
childprocess (>= 0.5.0)
|
||||
multi_json (~> 1.0)
|
||||
rubyzip (~> 1.0)
|
||||
websocket (~> 1.0.4)
|
||||
shellany (0.0.1)
|
||||
shoulda (3.5.0)
|
||||
shoulda-context (~> 1.0, >= 1.0.1)
|
||||
shoulda-matchers (>= 1.4.1, < 3.0)
|
||||
shoulda-context (1.2.1)
|
||||
shoulda-matchers (2.6.1)
|
||||
activesupport (>= 3.0.0)
|
||||
slop (3.6.0)
|
||||
spreadsheet (1.0.0)
|
||||
ruby-ole (>= 1.0)
|
||||
sprockets (2.2.2)
|
||||
hike (~> 1.2)
|
||||
multi_json (~> 1.0)
|
||||
rack (~> 1.0)
|
||||
tilt (~> 1.1, != 1.3.0)
|
||||
therubyracer (0.12.1)
|
||||
libv8 (~> 3.16.14.0)
|
||||
ref
|
||||
thor (0.19.1)
|
||||
thread_safe (0.3.4)
|
||||
tilt (1.4.1)
|
||||
timers (4.0.1)
|
||||
hitimes
|
||||
treetop (1.4.15)
|
||||
polyglot
|
||||
polyglot (>= 0.3.1)
|
||||
tzinfo (0.3.40)
|
||||
uglifier (2.5.1)
|
||||
execjs (>= 0.3.0)
|
||||
json (>= 1.8.0)
|
||||
virtus (1.0.3)
|
||||
axiom-types (~> 0.1)
|
||||
coercible (~> 1.0)
|
||||
descendants_tracker (~> 0.0, >= 0.0.3)
|
||||
equalizer (~> 0.0, >= 0.0.9)
|
||||
websocket (1.0.7)
|
||||
win32console (1.3.2-x86-mingw32)
|
||||
xpath (2.0.0)
|
||||
nokogiri (~> 1.3)
|
||||
|
||||
PLATFORMS
|
||||
ruby
|
||||
x86-mingw32
|
||||
|
||||
DEPENDENCIES
|
||||
activerecord-jdbc-adapter (= 1.2.5)
|
||||
activerecord-jdbcmysql-adapter
|
||||
acts-as-taggable-on (= 2.4.1)
|
||||
better_errors!
|
||||
builder (= 3.0.0)
|
||||
capybara (~> 2.4.1)
|
||||
coderay (~> 1.0.6)
|
||||
coffee-rails (~> 3.2.1)
|
||||
factory_girl (~> 4.4.0)
|
||||
fastercsv (~> 1.5.0)
|
||||
grape (~> 0.9.0)
|
||||
grape-entity
|
||||
grape-swagger
|
||||
grape-swagger-ui!
|
||||
guard-rspec (= 2.5.0)
|
||||
htmlentities
|
||||
i18n (~> 0.6.0)
|
||||
jquery-rails (~> 2.0.2)
|
||||
kaminari
|
||||
mocha (~> 1.1.0)
|
||||
mysql2 (= 0.3.11)
|
||||
net-ldap (~> 0.3.1)
|
||||
nokogiri (~> 1.6.3)
|
||||
paperclip (~> 3.5.4)
|
||||
rack-mini-profiler!
|
||||
rack-openid
|
||||
rails (= 3.2.13)
|
||||
rich (= 1.4.6)
|
||||
rspec-rails (= 2.13.1)
|
||||
ruby-ole
|
||||
ruby-openid (~> 2.1.4)
|
||||
sass-rails (~> 3.2.3)
|
||||
seems_rateable!
|
||||
selenium-webdriver (~> 2.42.0)
|
||||
shoulda (~> 3.5.0)
|
||||
spreadsheet
|
||||
therubyracer
|
||||
uglifier (>= 1.0.3)
|
@ -0,0 +1,41 @@
|
||||
module Mobile
|
||||
|
||||
class API < Grape::API
|
||||
version 'v1', using: :path
|
||||
format :json
|
||||
content_type :json, "application/json;charset=UTF-8"
|
||||
use Mobile::Middleware::ErrorHandler
|
||||
|
||||
helpers do
|
||||
def logger
|
||||
API.logger
|
||||
end
|
||||
|
||||
def authenticate!
|
||||
raise('Unauthorized. Invalid or expired token.') unless current_user
|
||||
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
|
||||
mount Apis::Watches
|
||||
mount Apis::Upgrade
|
||||
mount Apis::Homeworks
|
||||
|
||||
#add_swagger_documentation ({api_version: 'v1', base_path: 'http://u06.shellinfo.cn/trustie/api'})
|
||||
add_swagger_documentation ({api_version: 'v1', base_path: '/api'})
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -0,0 +1,48 @@
|
||||
#coding=utf-8
|
||||
|
||||
module Mobile
|
||||
|
||||
module Entities
|
||||
class Auth < Grape::Entity
|
||||
expose :token
|
||||
expose :user, using: User
|
||||
end
|
||||
end
|
||||
|
||||
module Apis
|
||||
class Auth < Grape::API
|
||||
resource :auth do
|
||||
desc "用户登录"
|
||||
params do
|
||||
requires :login, type: String, desc: 'Username or email'
|
||||
requires :password, type: String, desc: 'Password'
|
||||
end
|
||||
post do
|
||||
user,last_logon = ::User.try_to_login(params[:login], params[:password])
|
||||
if user
|
||||
::ApiKey.delete_all(user_id: user.id)
|
||||
key = ::ApiKey.create!(user_id: user.id)
|
||||
api_user = ::UsersService.new.show_user({id:user.id})
|
||||
present :data, {token: key.access_token, user: api_user}, using: Entities::Auth
|
||||
present :status, 0
|
||||
else
|
||||
raise 'Unauthorized.'
|
||||
end
|
||||
end
|
||||
|
||||
desc "用户登出"
|
||||
params do
|
||||
requires :token, type: String
|
||||
end
|
||||
delete do
|
||||
authenticate!
|
||||
::ApiKey.delete_all(user_id: current_user.id)
|
||||
{status: 0}
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -0,0 +1,96 @@
|
||||
#coding=utf-8
|
||||
module Mobile
|
||||
module Apis
|
||||
class Users < Grape::API
|
||||
resource :users do
|
||||
|
||||
desc "注册用户"
|
||||
params do
|
||||
requires :login, type: String, desc: 'username'
|
||||
requires :mail, type: String, desc: 'mail'
|
||||
requires :password, type: String, desc: 'password'
|
||||
end
|
||||
post do
|
||||
us = UsersService.new
|
||||
user = us.register params.merge(:password_confirmation => params[:password],
|
||||
:should_confirmation_password => true)
|
||||
raise "register failed #{user.errors.full_messages}" if user.new_record?
|
||||
|
||||
present :data, user, with: Mobile::Entities::User
|
||||
present :status, 0
|
||||
end
|
||||
|
||||
desc "显示用户"
|
||||
params do
|
||||
|
||||
end
|
||||
get ':id' do
|
||||
us = UsersService.new
|
||||
ue = us.show_user params
|
||||
present :data, ue,with: Mobile::Entities::User
|
||||
present :status, 0
|
||||
end
|
||||
|
||||
desc "修改用户"
|
||||
params do
|
||||
requires :token, type: String
|
||||
#optional :file, type: File, desc: 'avatar'
|
||||
optional :occupation, type: String
|
||||
optional :brief_introduction, type: String
|
||||
optional :province, type: String
|
||||
optional :city, type: String
|
||||
optional :gender, type: Integer
|
||||
end
|
||||
put ':id' do
|
||||
authenticate!
|
||||
us = UsersService.new
|
||||
ue = us.edit_user params.merge(id: current_user.id)
|
||||
present :data, ue,with: Mobile::Entities::User
|
||||
present :status, 0
|
||||
end
|
||||
|
||||
desc '获取用户课程'
|
||||
params do
|
||||
optional :token, type: String
|
||||
end
|
||||
|
||||
get ':id/courses' do
|
||||
us = UsersService.new
|
||||
ue = us.user_courses_list params,current_user.nil? ? User.find(2):current_user
|
||||
present :data, ue,with: Mobile::Entities::Course
|
||||
present :status, 0
|
||||
end
|
||||
|
||||
|
||||
desc '修改密码'
|
||||
params do
|
||||
requires :token, type: String
|
||||
requires :password, type:String , desc: '原密码'
|
||||
requires :new_password, type: String, desc: '新密码'
|
||||
end
|
||||
post 'password' do
|
||||
authenticate!
|
||||
us = UsersService.new
|
||||
user = us.change_password params.merge(current_user_id: current_user.id,
|
||||
new_password_confirmation: params[:new_password])
|
||||
present :data, user, with: Mobile::Entities::User
|
||||
present :status, 0
|
||||
end
|
||||
|
||||
desc "用户搜索"
|
||||
params do
|
||||
requires :name, type: String, desc: '用户名关键字'
|
||||
end
|
||||
get 'search' do
|
||||
us = UsersService.new
|
||||
user = us.search_user params
|
||||
present :data, user, with: Mobile::Entities::User
|
||||
present :status, 0
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -0,0 +1,49 @@
|
||||
#coding=utf-8
|
||||
module Mobile
|
||||
module Apis
|
||||
class Watches < Grape::API
|
||||
resource :watches do
|
||||
|
||||
desc "获取所有关注"
|
||||
params do
|
||||
requires :token, type: String
|
||||
end
|
||||
get do
|
||||
authenticate!
|
||||
us = UsersService.new
|
||||
ws = us.user_watcher(id: current_user.id)
|
||||
present :data, ws, with: Mobile::Entities::User
|
||||
present :status, 0
|
||||
end
|
||||
|
||||
|
||||
desc "关注某人"
|
||||
params do
|
||||
requires :token, type: String
|
||||
requires :object_id, type: Integer, desc: '关注的用户的id'
|
||||
end
|
||||
post do
|
||||
authenticate!
|
||||
ws = WatchesService.new
|
||||
o = ws.watch(params.merge({current_user_id:current_user.id, object_type:'user' }) )
|
||||
present :data, o, with: Mobile::Entities::User
|
||||
present :status, 0
|
||||
end
|
||||
|
||||
|
||||
desc "取消关注"
|
||||
params do
|
||||
requires :token, type: String
|
||||
requires :object_id, type: Integer, desc: '取消关注的用户的id'
|
||||
end
|
||||
delete do
|
||||
authenticate!
|
||||
ws = WatchesService.new
|
||||
ws.unwatch(params.merge({current_user_id:current_user.id, object_type:'user' }) )
|
||||
{status: 0}
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,35 @@
|
||||
module Mobile
|
||||
module Entities
|
||||
#匿评作品页面相关参数
|
||||
class AnonymousWorksParams < Grape::Entity
|
||||
def self.anonymous_works_params_expose(field)
|
||||
expose field do |f,opt|
|
||||
if f.is_a?(Hash) && f.key?(field)
|
||||
f[field]
|
||||
elsif f.is_a?(Hash) && !f.key?(field)
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
anonymous_works_params_expose :is_teacher
|
||||
anonymous_works_params_expose :m_score
|
||||
anonymous_works_params_expose :is_anonymous_comments
|
||||
anonymous_works_params_expose :cur_type
|
||||
expose :jours ,using: Mobile::Entities::Jours do |f, opt|
|
||||
if f.is_a?(Hash) && f.key?(:jours)
|
||||
f[:jours]
|
||||
end
|
||||
end
|
||||
expose :teacher_stars,using: Mobile::Entities::HomeworkJours do |f, opt|
|
||||
if f.is_a?(Hash) && f.key?(:teacher_stars)
|
||||
f[:teacher_stars]
|
||||
end
|
||||
end
|
||||
expose :student_stars , using: Mobile::Entities::HomeworkJours do |f, opt|
|
||||
if f.is_a?(Hash) && f.key?(:student_stars)
|
||||
f[:student_stars]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,23 @@
|
||||
module Mobile
|
||||
module Entities
|
||||
class Attachment < Grape::Entity
|
||||
def self.attachment_expose(field)
|
||||
expose field do |f,opt|
|
||||
if f.is_a?(Hash) && f.key?(field)
|
||||
f[field]
|
||||
elsif f.is_a?(::Attachment)
|
||||
if f.respond_to?(field)
|
||||
f.send(field)
|
||||
else
|
||||
#case field
|
||||
# when ""
|
||||
#end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
attachment_expose :filename
|
||||
attachment_expose :description
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,58 @@
|
||||
module Mobile
|
||||
module Entities
|
||||
class Course < Grape::Entity
|
||||
def self.course_expose(field)
|
||||
expose field do |f,opt|
|
||||
c = nil
|
||||
if f.is_a? ::Course
|
||||
c = f
|
||||
else
|
||||
c = f[:course]
|
||||
end
|
||||
if field == :img_url
|
||||
f[field] if f.is_a?(Hash) && f.key?(field)
|
||||
#f.img_url if f.respond_to?(:img_url)
|
||||
else
|
||||
(c[field] if (c.is_a?(Hash) && c.key?(field))) || (c.send(field) if c.respond_to?(field))
|
||||
end
|
||||
end
|
||||
end
|
||||
course_expose :img_url
|
||||
course_expose :attachmenttype
|
||||
course_expose :class_period
|
||||
course_expose :code
|
||||
course_expose :created_at
|
||||
course_expose :description
|
||||
course_expose :endup_time
|
||||
course_expose :extra
|
||||
course_expose :id
|
||||
course_expose :inherit_members
|
||||
course_expose :is_public
|
||||
course_expose :lft
|
||||
course_expose :location
|
||||
course_expose :name
|
||||
course_expose :open_student
|
||||
# course_expose :password
|
||||
course_expose :rgt
|
||||
course_expose :school_id
|
||||
course_expose :setup_time
|
||||
course_expose :state
|
||||
course_expose :status
|
||||
course_expose :string
|
||||
course_expose :tea_id
|
||||
course_expose :term
|
||||
course_expose :time
|
||||
course_expose :updated_at
|
||||
expose :teacher, using: Mobile::Entities::User do |c, opt|
|
||||
if c.is_a? ::Course
|
||||
c.teacher
|
||||
else
|
||||
c[:course].teacher
|
||||
end
|
||||
end
|
||||
expose :my_homework,using: Mobile::Entities::HomeworkAttach do |f, opt|
|
||||
f[:my_homework] if f.is_a?(Hash) && f.key?(:my_homework)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,39 @@
|
||||
module Mobile
|
||||
module Entities
|
||||
class HomeworkAttach < Grape::Entity
|
||||
include Redmine::I18n
|
||||
def self.homework_attach_expose(field)
|
||||
expose field do |f,opt|
|
||||
if f.is_a?(Hash) && f.key?(field)
|
||||
f[field]
|
||||
elsif f.is_a?(::HomeworkAttach)
|
||||
if f.respond_to?(field)
|
||||
if field == :created_at
|
||||
format_time(f.send(:created_at))
|
||||
else
|
||||
f.send(field)
|
||||
end
|
||||
else
|
||||
case field
|
||||
when :homework_times
|
||||
f.bid.courses.first.homeworks.index(f.bid) + 1 unless (f.bid.nil? || f.bid.courses.nil? || f.bid.courses.first.nil?)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
homework_attach_expose :id
|
||||
homework_attach_expose :name
|
||||
homework_attach_expose :homework_times
|
||||
homework_attach_expose :description
|
||||
homework_attach_expose :created_at
|
||||
expose :attachments,using: Mobile::Entities::Attachment do |f, opt|
|
||||
if f.respond_to?(:attachments)
|
||||
f.send(:attachments)
|
||||
end
|
||||
end
|
||||
#homework_attach_expose :user
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,25 @@
|
||||
module Mobile
|
||||
module Entities
|
||||
#带评分的留言(教师评论、学生匿名评分都属于此类)
|
||||
class HomeworkJours < Grape::Entity
|
||||
include Redmine::I18n
|
||||
def self.homework_jours_expose(field)
|
||||
expose field do |f,opt|
|
||||
if f.is_a?(Hash) && f.key?(field)
|
||||
f[field]
|
||||
elsif f.is_a?(::SeemsRateableRates)
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
homework_jours_expose :rater_id
|
||||
homework_jours_expose :rater_name
|
||||
homework_jours_expose :created_at
|
||||
homework_jours_expose :stars
|
||||
expose :comment,using: Mobile::Entities::Jours do |f,opt|
|
||||
f[:comment]
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,37 @@
|
||||
module Mobile
|
||||
module Entities
|
||||
#普通留言
|
||||
class Jours < Grape::Entity
|
||||
include Redmine::I18n
|
||||
include WordsHelper
|
||||
def self.jours_expose(field)
|
||||
expose field do |f,opt|
|
||||
if f.is_a?(Hash) && f.key?(field)
|
||||
f[field]
|
||||
elsif f.is_a?(::JournalsForMessage) && f.respond_to?(field)
|
||||
if field == :created_on
|
||||
format_time(f.send(field))
|
||||
else
|
||||
f.send(field)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
jours_expose :id
|
||||
expose :user,using: Mobile::Entities::User do |f, opt|
|
||||
f.user
|
||||
end
|
||||
jours_expose :created_on
|
||||
jours_expose :notes
|
||||
jours_expose :m_reply_id
|
||||
expose :reply_user,using: Mobile::Entities::User do |f, opt|
|
||||
f.at_user
|
||||
end
|
||||
expose :child_reply,using: Mobile::Entities::Jours do |f, opt|
|
||||
if f.is_a?(::JournalsForMessage)
|
||||
fetch_user_leaveWord_reply(f)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,42 @@
|
||||
module Mobile
|
||||
module Entities
|
||||
class News < Grape::Entity
|
||||
def self.news_expose(field)
|
||||
expose field do |f,opt|
|
||||
if f.is_a?(Hash) && f.key?(field)
|
||||
f[field]
|
||||
elsif f.is_a?(Hash) && !f.key?(field)
|
||||
n = f[:news]
|
||||
comments = f[:comments]
|
||||
if n.is_a?(::News)
|
||||
n.send(field) if n.respond_to?(field)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
#新闻标题
|
||||
news_expose :title
|
||||
|
||||
expose :author,using: Mobile::Entities::User do |f, opt|
|
||||
n = f[:news]
|
||||
n.author if n.respond_to?(:author)
|
||||
end
|
||||
#作者id
|
||||
news_expose :author_id
|
||||
#作者名
|
||||
news_expose :author_name
|
||||
#新闻内容
|
||||
news_expose :description
|
||||
#发布时间
|
||||
news_expose :created_on
|
||||
#评论数量
|
||||
news_expose :comments_count
|
||||
#评论
|
||||
news_expose :comments
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,51 @@
|
||||
module Mobile
|
||||
module Entities
|
||||
class User < Grape::Entity
|
||||
include ApplicationHelper
|
||||
include ApiHelper
|
||||
def self.user_expose(f)
|
||||
expose f do |u,opt|
|
||||
if u.is_a?(Hash) && u.key?(f)
|
||||
u[f]
|
||||
elsif u.is_a?(::User)
|
||||
if u.respond_to?(f)
|
||||
u.send(f)
|
||||
else
|
||||
case f
|
||||
when :img_url
|
||||
url_to_avatar(u)
|
||||
when :gender
|
||||
u.user_extensions.gender.nil? ? 0 : u.user_extensions.gender
|
||||
when :work_unit
|
||||
get_user_work_unit u
|
||||
when :location
|
||||
get_user_location u
|
||||
when :brief_introduction
|
||||
u.user_extensions.brief_introduction
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
expose :id
|
||||
#头像
|
||||
user_expose :img_url
|
||||
#昵称
|
||||
expose :nickname
|
||||
#性别
|
||||
user_expose :gender
|
||||
#我的二维码
|
||||
#工作单位
|
||||
user_expose :work_unit
|
||||
#邮箱地址
|
||||
user_expose :mail
|
||||
#地区
|
||||
user_expose :location
|
||||
#签名
|
||||
user_expose :brief_introduction
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,19 @@
|
||||
module Mobile
|
||||
module Middleware
|
||||
class ErrorHandler < Grape::Middleware::Base
|
||||
def call!(env)
|
||||
@env = env
|
||||
begin
|
||||
@app.call(@env)
|
||||
rescue =>e
|
||||
message = {status: 1, message: e.message }.to_json
|
||||
puts(e.backtrace.join("\n")) if Rails.env.development?
|
||||
status = 200
|
||||
headers = { 'Content-Type' => content_type }
|
||||
Rack::Response.new([message], status, headers).finish
|
||||
# throw :error, :message => e.message || options[:default_message], :status => 500
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,40 @@
|
||||
module ApiHelper
|
||||
#获取用户的工作单位
|
||||
def get_user_work_unit user
|
||||
work_unit = ""
|
||||
if user.user_extensions.identity == 0 || user.user_extensions.identity == 1
|
||||
work_unit = user.user_extensions.school.name unless user.user_extensions.school.nil?
|
||||
elsif user.user_extensions.identity == 3
|
||||
work_unit = user.user_extensions.occupation
|
||||
elsif user.user_extensions.identity == 2
|
||||
work_unit = user.firstname
|
||||
end
|
||||
work_unit
|
||||
end
|
||||
|
||||
#获取用户地区
|
||||
def get_user_location user
|
||||
location = ""
|
||||
location << (user.user_extensions.location || '')
|
||||
location << (user.user_extensions.location_city || '')
|
||||
location
|
||||
end
|
||||
|
||||
|
||||
def get_assigned_homeworks(homeworks, n, index)
|
||||
homeworks += homeworks
|
||||
homeworks[index + 1 .. index + n]
|
||||
end
|
||||
|
||||
|
||||
def stars_to_json_like starts,show_jour,homework,show_name
|
||||
result = []
|
||||
starts.each do |s|
|
||||
comment = get_homework_review homework,show_jour,s.rater
|
||||
rater_name = show_name ? s.rater.login : l(:label_anonymous)
|
||||
rater_id = show_name ? s.rater.id : ''
|
||||
result << {:rater_id =>rater_id ,:rater_name => rater_name,:created_at => format_time(s.created_at),:stars => s.stars,:comment => comment}
|
||||
end
|
||||
result
|
||||
end
|
||||
end
|
@ -0,0 +1,21 @@
|
||||
class ApiKey < ActiveRecord::Base
|
||||
attr_accessible :access_token, :active, :expires_at, :user_id
|
||||
before_create :generate_access_token
|
||||
before_create :set_experation
|
||||
|
||||
# validates_presence_of :user_id, :access_token
|
||||
|
||||
def expired?
|
||||
DateTime.now >= self.expires_at
|
||||
end
|
||||
|
||||
private
|
||||
def generate_access_token
|
||||
self.access_token = SecureRandom.hex
|
||||
end
|
||||
|
||||
def set_experation
|
||||
self.expires_at = DateTime.now + 30
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,9 @@
|
||||
<% if @status == 0%>
|
||||
alert("您申请的项目不存在");
|
||||
<% elsif @status == 1%>
|
||||
alert("请勿重复申请加入该项目");
|
||||
<% elsif @status == 2%>
|
||||
alert("申请成功");
|
||||
<%else%>
|
||||
alert("申请失败");
|
||||
<%end%>
|
@ -0,0 +1,2 @@
|
||||
$("#polls_<%= @poll.id %>").html("<%= escape_javascript(render :partial => 'poll',:locals => {:poll => @poll}) %>");
|
||||
alert("关闭成功");
|
@ -0,0 +1,26 @@
|
||||
if Rails.env.development?
|
||||
lib_ruby_files = Dir.glob(File.join("app/api/**", "*.rb"))
|
||||
lib_reloader ||= ActiveSupport::FileUpdateChecker.new(lib_ruby_files) do
|
||||
# lib_ruby_files.each do |lib_file|
|
||||
# puts "start require #{lib_file}"
|
||||
# require_dependency(lib_file)
|
||||
# end
|
||||
#
|
||||
|
||||
# binding.pry
|
||||
# if Object.const_defined?(:Mobile)
|
||||
# Object.send(:remove_const, :Mobile)
|
||||
# end
|
||||
#
|
||||
# $".delete_if {|s| s.include?('api/mobile') }
|
||||
# require File.join(Rails.root,"app/api/mobile/api.rb")
|
||||
Rails.application.reload_routes!
|
||||
end
|
||||
|
||||
ActionDispatch::Callbacks.to_prepare do
|
||||
lib_reloader.execute_if_updated
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
@ -0,0 +1,14 @@
|
||||
class CreateApiKeys < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :api_keys do |t|
|
||||
t.string :access_token
|
||||
t.datetime :expires_at
|
||||
t.integer :user_id
|
||||
t.boolean :active, default: true
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
add_index :api_keys, :user_id
|
||||
add_index :api_keys, :access_token
|
||||
end
|
||||
end
|
Binary file not shown.
@ -0,0 +1,24 @@
|
||||
require File.expand_path('../../test_helper.rb',__FILE__)
|
||||
|
||||
class AuthTest < ActionDispatch::IntegrationTest
|
||||
def setup
|
||||
ApiKey.delete_all
|
||||
end
|
||||
|
||||
test "login success when use correct password" do
|
||||
post('/api/v1/auth/login.json', {login: 'guange', password: '123456'})
|
||||
o = ActiveSupport::JSON.decode response.body
|
||||
puts o
|
||||
puts o["token"]
|
||||
assert_not_nil o["token"]
|
||||
end
|
||||
|
||||
test "login failure when incorrect password" do
|
||||
post('/api/v1/auth/login.json', {login: 'guange', password: 'wrongpass'})
|
||||
o = ActiveSupport::JSON.decode response.body
|
||||
assert_nil o["token"]
|
||||
assert_equal o["error"], 'Unauthorized.'
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -0,0 +1,12 @@
|
||||
require File.expand_path('../../test_helper.rb',__FILE__)
|
||||
|
||||
class UsersTest < ActionDispatch::IntegrationTest
|
||||
test "register a user" do
|
||||
end
|
||||
test "get all users" do
|
||||
get '/api/v1/users.json'
|
||||
assert_response :success
|
||||
end
|
||||
end
|
||||
|
||||
|
Loading…
Reference in new issue