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

View File

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

View File

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

View File

@ -1,20 +1,31 @@
module Mobile
class Auth < Grape::API
resource :auth do
desc "Creates and returns access_token if valid login"
params do
requires :login, type: String, desc: 'Username or email'
requires :password, type: String, desc: 'Password'
end
post :login 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)
data = {token: key.access_token}.merge(user.as_json).merge(user.extensions.as_json)
{status: 0, data: data}
else
{status: 1, message: 'Unauthorized.'}
module Entities
class Auth < Grape::Entity
expose :token
expose :user, using: User
end
end
module Apis
class Auth < Grape::API
resource :auth do
desc "Creates and returns access_token if valid login"
params do
requires :login, type: String, desc: 'Username or email'
requires :password, type: String, desc: 'Password'
end
post :login 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 = UserService.new.show_user(user.id)
present :data, {token: key, user: api_user}, using: Mobile:Entities::Auth
present :status, 0
else
{status: 1, message: 'Unauthorized.'}
end
end
end
end

View File

@ -1,40 +1,32 @@
module Mobile
class Courses < Grape::API
resource :courses do
desc "get all courses"
params do
requires :per_page_count, type: Integer
requires :page, type: Integer, desc: 'current page no'
end
get do
per_page_option = params[:per_page_count] || 10
page_no = params[:page] || 1
@courses_all = Course.active.visible.
joins("LEFT JOIN #{CourseStatus.table_name} ON #{Course.table_name}.id = #{CourseStatus.table_name}.course_id")
@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
module Apis
class Courses < Grape::API
resource :courses do
desc "get all courses"
params do
optional :school_id, type: Integer, desc: 'school number'
requires :per_page_count, type: Integer
requires :page, type: Integer, desc: 'current page no'
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
desc "Return a course"
params do
requires :id, type: Integer
end
route_param :id do
get do
course = Course.find(params[:id])
{status: 0, data: course}
cs = CoursesService.new
courses = cs.course_list(params)
present :data, courses, with: Mobile::Entities::Course
present :status, 0
end
end
desc "Return a course"
params do
requires :id, type: Integer
end
route_param :id do
get do
course = Course.find(params[:id])
{status: 0, data: course}
end
end
end
end
end
end

View File

@ -1,27 +1,31 @@
module Mobile
class Users < Grape::API
resource :users do
desc "get all users"
get do
['hello']
end
desc "add a user"
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
begin
user = us.register params.merge(:password_confirmation => params[:password],
:should_confirmation_password => true)
raise "register failed." if user.new_record?
{status: 0, data: user}
rescue => e
{status: 1, message: e.message}
module Apis
class Users < Grape::API
resource :users do
desc "get all users"
get do
['hello']
end
desc "add a user"
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
begin
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
rescue => e
{status: 1, message: e.message}
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