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

View File

@ -1,40 +1,32 @@
module Mobile module Mobile
class Courses < Grape::API module Apis
resource :courses do class Courses < Grape::API
desc "get all courses" resource :courses do
params do desc "get all courses"
requires :per_page_count, type: Integer params do
requires :page, type: Integer, desc: 'current page no' optional :school_id, type: Integer, desc: 'school number'
end requires :per_page_count, type: Integer
get do requires :page, type: Integer, desc: 'current page no'
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
end 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 get do
course = Course.find(params[:id]) cs = CoursesService.new
{status: 0, data: course} courses = cs.course_list(params)
present :data, courses, with: Mobile::Entities::Course
present :status, 0
end 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 end
end end

View File

@ -1,27 +1,31 @@
module Mobile module Mobile
class Users < Grape::API module Apis
resource :users do class Users < Grape::API
desc "get all users" resource :users do
get do desc "get all users"
['hello'] get do
end ['hello']
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}
end 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 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