Conflicts:
	Gemfile.lock
This commit is contained in:
z9hang 2014-12-09 16:43:06 +08:00
commit dbb46b07a6
9 changed files with 188 additions and 86 deletions

View File

@ -9,6 +9,7 @@ unless RUBY_PLATFORM =~ /w32/
end end
gem 'grape', '~> 0.9.0' gem 'grape', '~> 0.9.0'
gem 'grape-entity'
gem 'seems_rateable', path: 'lib/seems_rateable' gem 'seems_rateable', path: 'lib/seems_rateable'
gem "rails", "3.2.13" gem "rails", "3.2.13"
gem "jquery-rails", "~> 2.0.2" gem "jquery-rails", "~> 2.0.2"
@ -49,9 +50,9 @@ group :development, :test do
gem 'guard-spork', '~> 1.5.1' gem 'guard-spork', '~> 1.5.1'
gem 'guard-test', '~> 1.0.0' gem 'guard-test', '~> 1.0.0'
gem 'ruby-prof', '~> 0.15.1' unless RUBY_PLATFORM =~ /w32/ gem 'ruby-prof', '~> 0.15.1' unless RUBY_PLATFORM =~ /w32/
gem 'pry' gem 'pry-rails'
gem 'pry-nav' gem 'pry-byebug'
gem 'pry-remote'
end end

View File

@ -57,6 +57,9 @@ GEM
ice_nine (~> 0.11.0) ice_nine (~> 0.11.0)
thread_safe (~> 0.3, >= 0.3.1) thread_safe (~> 0.3, >= 0.3.1)
builder (3.0.0) builder (3.0.0)
byebug (2.7.0)
columnize (~> 0.3)
debugger-linecache (~> 1.2)
capybara (2.4.1) capybara (2.4.1)
mime-types (>= 1.16) mime-types (>= 1.16)
nokogiri (>= 1.3.3) nokogiri (>= 1.3.3)
@ -81,6 +84,8 @@ GEM
coffee-script-source coffee-script-source
execjs execjs
coffee-script-source (1.7.1) coffee-script-source (1.7.1)
columnize (0.9.0)
debugger-linecache (1.2.0)
descendants_tracker (0.0.4) descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1) thread_safe (~> 0.3, >= 0.3.1)
equalizer (0.0.9) equalizer (0.0.9)
@ -102,6 +107,9 @@ GEM
rack-accept rack-accept
rack-mount rack-mount
virtus (>= 1.0.0) virtus (>= 1.0.0)
grape-entity (0.4.4)
activesupport
multi_json (>= 1.3.2)
guard (2.6.1) guard (2.6.1)
formatador (>= 0.2.4) formatador (>= 0.2.4)
listen (~> 2.7) listen (~> 2.7)
@ -169,8 +177,14 @@ GEM
method_source (~> 0.8) method_source (~> 0.8)
slop (~> 3.4) slop (~> 3.4)
win32console (~> 1.3) win32console (~> 1.3)
pry-nav (0.2.3) pry-byebug (1.3.2)
pry (~> 0.9.10) byebug (~> 2.7)
pry (~> 0.9.12)
pry-rails (0.3.2)
pry (>= 0.9.10)
pry-remote (0.1.8)
pry (~> 0.9)
slop (~> 3.0)
rack (1.4.5) rack (1.4.5)
rack-accept (0.4.5) rack-accept (0.4.5)
rack (>= 0.4) rack (>= 0.4)
@ -286,6 +300,7 @@ DEPENDENCIES
factory_girl (~> 4.4.0) factory_girl (~> 4.4.0)
fastercsv (~> 1.5.0) fastercsv (~> 1.5.0)
grape (~> 0.9.0) grape (~> 0.9.0)
grape-entity
guard-rails (~> 0.5.3) guard-rails (~> 0.5.3)
guard-spork (~> 1.5.1) guard-spork (~> 1.5.1)
guard-test (~> 1.0.0) guard-test (~> 1.0.0)
@ -298,8 +313,9 @@ DEPENDENCIES
net-ldap (~> 0.3.1) net-ldap (~> 0.3.1)
nokogiri (~> 1.6.3) nokogiri (~> 1.6.3)
paperclip (~> 3.5.4) paperclip (~> 3.5.4)
pry pry-byebug
pry-nav pry-rails
pry-remote
rack-mini-profiler! rack-mini-profiler!
rack-openid rack-openid
rails (= 3.2.13) rails (= 3.2.13)

View File

@ -1,11 +1,5 @@
module Mobile module Mobile
$LOAD_PATH << File.expand_path('..',__FILE__)
autoload :Auth, 'apis/auth'
autoload :Users, 'apis/users'
autoload :Courses, 'apis/courses'
class API < Grape::API class API < Grape::API
version 'v1', using: :path version 'v1', using: :path
format :json format :json
@ -30,9 +24,9 @@ module Mobile
end end
end end
mount Auth mount Apis::Auth
mount Users mount Apis::Users
mount Courses mount Apis::Courses
end end
end end

View File

@ -1,4 +1,13 @@
module Mobile module Mobile
module Entities
class Auth < Grape::Entity
expose :token
expose :user, using: User
end
end
module Apis
class Auth < Grape::API class Auth < Grape::API
resource :auth do resource :auth do
desc "Creates and returns access_token if valid login" desc "Creates and returns access_token if valid login"
@ -11,8 +20,9 @@ module Mobile
if user if user
::ApiKey.delete_all(user_id: user.id) ::ApiKey.delete_all(user_id: user.id)
key = ::ApiKey.create!(user_id: user.id) key = ::ApiKey.create!(user_id: user.id)
data = {token: key.access_token}.merge(user.as_json).merge(user.extensions.as_json) api_user = UserService.new.show_user(user.id)
{status: 0, data: data} present :data, {token: key, user: api_user}, using: Mobile:Entities::Auth
present :status, 0
else else
{status: 1, message: 'Unauthorized.'} {status: 1, message: 'Unauthorized.'}
end end
@ -20,5 +30,6 @@ module Mobile
end end
end end
end end
end

View File

@ -1,27 +1,18 @@
module Mobile module Mobile
module Apis
class Courses < Grape::API class Courses < Grape::API
resource :courses do resource :courses do
desc "get all courses" desc "get all courses"
params do params do
optional :school_id, type: Integer, desc: 'school number'
requires :per_page_count, type: Integer requires :per_page_count, type: Integer
requires :page, type: Integer, desc: 'current page no' requires :page, type: Integer, desc: 'current page no'
end end
get do get do
per_page_option = params[:per_page_count] || 10 cs = CoursesService.new
page_no = params[:page] || 1 courses = cs.course_list(params)
@courses_all = Course.active.visible. present :data, courses, with: Mobile::Entities::Course
joins("LEFT JOIN #{CourseStatus.table_name} ON #{Course.table_name}.id = #{CourseStatus.table_name}.course_id") present :status, 0
@course_count = @courses_all.count
@course_pages = Redmine::Pagination::Paginator.new @course_count, per_page_option,page_no
@course_activity_count=Hash.new
@courses_all.each do |course|
@course_activity_count[course.id]=0
end
@courses = @courses_all.order("created_at desc")
@s_type = 0
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
{status: 0, data: @courses}
# @course_activity_count=get_course_activity @courses, @course_activity_count
end end
desc "Return a course" desc "Return a course"
@ -38,4 +29,5 @@ module Mobile
end end
end end
end end
end

View File

@ -1,4 +1,5 @@
module Mobile module Mobile
module Apis
class Users < Grape::API class Users < Grape::API
resource :users do resource :users do
desc "get all users" desc "get all users"
@ -17,12 +18,15 @@ module Mobile
begin begin
user = us.register params.merge(:password_confirmation => params[:password], user = us.register params.merge(:password_confirmation => params[:password],
:should_confirmation_password => true) :should_confirmation_password => true)
raise "register failed." if user.new_record? raise "register failed #{user.errors.full_messages}" if user.new_record?
{status: 0, data: user} present :data, user, with: Mobile::Entities::User
present :status, 0
rescue => e rescue => e
{status: 1, message: e.message} {status: 1, message: e.message}
end end
end end
end
end end
end end
end end

View File

@ -0,0 +1,38 @@
module Mobile
module Entities
class Course < Grape::Entity
def self.course_expose(field)
expose field do |f,opt|
f[:course][field] || f[:course].__send__(field)
end
end
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
course_expose :teacher
end
end
end

View File

@ -0,0 +1,20 @@
module Mobile
module Entities
class User < Grape::Entity
#头像
#昵称
expose :login
#性别
expose :gender do |user, opt|
user.user_extentions.gender
end
#我的二维码
#工作单位
#邮箱地址
expose :mail
#地区
#签名
end
end
end

View File

@ -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