From c7c25edf17508e4d71fbf67bca4ef13e4dea2016 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Mon, 8 Dec 2014 15:27:18 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=8A=A0=E4=B8=8Apry=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 6 +++--- Gemfile.lock | 20 ++++++++++++++++---- app/api/mobile/apis/users.rb | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index 5288edd52..ab7a41a16 100644 --- a/Gemfile +++ b/Gemfile @@ -49,9 +49,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 diff --git a/Gemfile.lock b/Gemfile.lock index 58f069ac1..d2ae03453 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -170,8 +175,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) puma (2.10.1) rack (>= 1.1, < 2.0) rack (1.4.5) @@ -305,8 +316,9 @@ DEPENDENCIES net-ldap (~> 0.3.1) nokogiri (~> 1.6.3) paperclip (~> 3.5.4) - pry - pry-nav + pry-byebug + pry-rails + pry-remote puma rack-mini-profiler! rack-openid diff --git a/app/api/mobile/apis/users.rb b/app/api/mobile/apis/users.rb index c9ecdd84f..489b3e419 100644 --- a/app/api/mobile/apis/users.rb +++ b/app/api/mobile/apis/users.rb @@ -17,7 +17,7 @@ module Mobile begin user = us.register params.merge(:password_confirmation => params[:password], :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} rescue => e {status: 1, message: e.message} From ee3d998873326453d3c6141056e749a029e459d4 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Mon, 8 Dec 2014 16:51:21 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E8=AF=BE=E7=A8=8B=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=8A=A0=E5=85=A5=E8=80=81=E5=B8=88=E5=AD=97?= =?UTF-8?q?=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 1 + Gemfile.lock | 4 +++ app/api/mobile/apis/courses.rb | 60 +++++++++++++++++++++++++--------- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/Gemfile b/Gemfile index ab7a41a16..34a1a7956 100644 --- a/Gemfile +++ b/Gemfile @@ -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" diff --git a/Gemfile.lock b/Gemfile.lock index d2ae03453..377ded53e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -107,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) @@ -303,6 +306,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) diff --git a/app/api/mobile/apis/courses.rb b/app/api/mobile/apis/courses.rb index bd146e235..9409fddd7 100644 --- a/app/api/mobile/apis/courses.rb +++ b/app/api/mobile/apis/courses.rb @@ -1,27 +1,57 @@ 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 + 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 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 - 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 + cs = CoursesService.new + courses = cs.course_list(params) + present :data, courses, with: Entities::Course + present :status, 0 end desc "Return a course" From 41d9af1ab9e954331446a0f38316ce12a5b0331e Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Tue, 9 Dec 2014 15:16:47 +0800 Subject: [PATCH 3/4] auto reload api folder in development mode. --- app/api/mobile/api.rb | 12 +++------ app/api/mobile/apis/auth.rb | 34 +++++++++++++------------ app/api/mobile/apis/courses.rb | 3 +++ app/api/mobile/apis/users.rb | 8 ++++++ config/initializers/autoreload_grape.rb | 26 +++++++++++++++++++ 5 files changed, 58 insertions(+), 25 deletions(-) create mode 100644 config/initializers/autoreload_grape.rb diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index 918c5d73c..4add19248 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -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 diff --git a/app/api/mobile/apis/auth.rb b/app/api/mobile/apis/auth.rb index acd5fe1e2..8281c4aa9 100644 --- a/app/api/mobile/apis/auth.rb +++ b/app/api/mobile/apis/auth.rb @@ -1,20 +1,22 @@ 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 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) + data = {token: key.access_token}.merge(user.as_json).merge(user.extensions.as_json) + {status: 0, data: data} + else + {status: 1, message: 'Unauthorized.'} + end end end end diff --git a/app/api/mobile/apis/courses.rb b/app/api/mobile/apis/courses.rb index 9409fddd7..2dca9c220 100644 --- a/app/api/mobile/apis/courses.rb +++ b/app/api/mobile/apis/courses.rb @@ -1,5 +1,7 @@ module Mobile + module Apis + module Entities class Course < Grape::Entity @@ -67,5 +69,6 @@ module Mobile end end + end end diff --git a/app/api/mobile/apis/users.rb b/app/api/mobile/apis/users.rb index 489b3e419..e4544a09e 100644 --- a/app/api/mobile/apis/users.rb +++ b/app/api/mobile/apis/users.rb @@ -1,4 +1,5 @@ module Mobile + module Apis class Users < Grape::API resource :users do desc "get all users" @@ -23,8 +24,15 @@ module Mobile {status: 1, message: e.message} end end + + desc "test upload" + post "upload" do + puts "haha11" + end + end end + end end diff --git a/config/initializers/autoreload_grape.rb b/config/initializers/autoreload_grape.rb new file mode 100644 index 000000000..ceffb9425 --- /dev/null +++ b/config/initializers/autoreload_grape.rb @@ -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 + + From 4a6c26d499844c6cc4cdb086c01c1fa032745be6 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Tue, 9 Dec 2014 16:36:43 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=B0=86entities=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/mobile/apis/auth.rb | 13 ++++- app/api/mobile/apis/courses.rb | 87 ++++++++----------------------- app/api/mobile/apis/users.rb | 52 +++++++++--------- app/api/mobile/entities/course.rb | 38 ++++++++++++++ app/api/mobile/entities/user.rb | 20 +++++++ 5 files changed, 116 insertions(+), 94 deletions(-) create mode 100644 app/api/mobile/entities/course.rb create mode 100644 app/api/mobile/entities/user.rb diff --git a/app/api/mobile/apis/auth.rb b/app/api/mobile/apis/auth.rb index 8281c4aa9..33eff26ad 100644 --- a/app/api/mobile/apis/auth.rb +++ b/app/api/mobile/apis/auth.rb @@ -1,4 +1,12 @@ 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 @@ -12,8 +20,9 @@ module Mobile 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} + 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 diff --git a/app/api/mobile/apis/courses.rb b/app/api/mobile/apis/courses.rb index 2dca9c220..1084e8cbc 100644 --- a/app/api/mobile/apis/courses.rb +++ b/app/api/mobile/apis/courses.rb @@ -1,74 +1,33 @@ module Mobile - module Apis - - module Entities - - class Course < Grape::Entity - def self.course_expose(field) - expose field do |f,opt| - f[:course][field] || f[:course].__send__(field) + 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 - 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 - - 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 - get do - cs = CoursesService.new - courses = cs.course_list(params) - present :data, courses, with: Entities::Course - present :status, 0 - 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 end diff --git a/app/api/mobile/apis/users.rb b/app/api/mobile/apis/users.rb index e4544a09e..df17782f2 100644 --- a/app/api/mobile/apis/users.rb +++ b/app/api/mobile/apis/users.rb @@ -1,38 +1,34 @@ module Mobile 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? - {status: 0, data: user} - rescue => e - {status: 1, message: e.message} + class Users < Grape::API + resource :users do + desc "get all users" + get do + ['hello'] end - end - desc "test upload" - post "upload" do - puts "haha11" - 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 diff --git a/app/api/mobile/entities/course.rb b/app/api/mobile/entities/course.rb new file mode 100644 index 000000000..b9823d9ac --- /dev/null +++ b/app/api/mobile/entities/course.rb @@ -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 diff --git a/app/api/mobile/entities/user.rb b/app/api/mobile/entities/user.rb new file mode 100644 index 000000000..33ae3f049 --- /dev/null +++ b/app/api/mobile/entities/user.rb @@ -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