diff --git a/app/controllers/api/v1/books_controller.rb b/app/controllers/api/v1/books_controller.rb index b493bad..220d94c 100644 --- a/app/controllers/api/v1/books_controller.rb +++ b/app/controllers/api/v1/books_controller.rb @@ -58,7 +58,7 @@ class Api::V1::BooksController < ApplicationController def import_douban_books user = User.find_by_email params[:user_email] user.douban_user params[:name] - render json: {status: 'success', msg: '成功连接豆瓣用户'} + render json: {status: 'success', msg: '成功连接豆瓣用户,稍后您将可以图书列表看到你的图书'} end end diff --git a/app/models/third_party_user_data.rb b/app/models/third_party_user_data.rb index 8e8f159..600ada3 100644 --- a/app/models/third_party_user_data.rb +++ b/app/models/third_party_user_data.rb @@ -1,6 +1,8 @@ class ThirdPartyUserData < ActiveRecord::Base belongs_to :user - attr_accessible :douban_user_name, :user_id - + attr_accessible :douban_user_name, :user_id, :import_douban_done + def import_douban_user_done + update_attributes import_douban_done: true + end end diff --git a/app/models/user.rb b/app/models/user.rb index 83291ad..4703e1e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -134,8 +134,21 @@ class User < ActiveRecord::Base else third_party_user_datas.create douban_user_name: name end - # Temp disabled - DoubanBooksWorker.perform_async id, name + begin + DoubanBooksWorker.perform_async id, name + import_double_user_done + rescue RuntimeError => error + logger.error "绑定豆瓣用户图书失败:#{error}" + true + rescue TypeError => type_error + logger.error "绑定豆瓣用户图书失败,类型错误:\n#{type_error}" + true + rescue RestClient::Forbidden + logger.error "豆瓣禁止访问API了" + true + rescue Exception => ex + logger.error "出错了:#{ex.to_s}" + end end def get_douban_user @@ -146,4 +159,8 @@ class User < ActiveRecord::Base end end + def import_double_user_done + third_party_user_datas.first.import_douban_user_done + end + end diff --git a/app/workers/douban_books_worker.rb b/app/workers/douban_books_worker.rb index f0e87eb..c70585a 100644 --- a/app/workers/douban_books_worker.rb +++ b/app/workers/douban_books_worker.rb @@ -8,9 +8,10 @@ class DoubanBooksWorker total = 0 start = 0 while (start == 0) || (total-start > count) - result = RestClient.get "http://api.douban.com/v2/book/user/#{name}/collections?start=#{start}&count=#{count}" + url = "http://api.douban.com/v2/book/user/#{name}/collections?start=#{start}&count=#{count}" + logger.info "url============#{url}" + result = RestClient.get url json = JSON.parse(result) - logger.info "get books from douban user:==== #{json}" storeBooks(json, user) total = json['total'] start += count @@ -20,15 +21,14 @@ class DoubanBooksWorker def storeBooks(json, user) collections = json['collections'] - logger.error "--------------------#{collections}" - collections.each { |collection| - - logger.warn "!!!!!!!!!!!! #{collection}\n" - + collections.each { |collection, index| book = collection['book'] - Book.create_book_by_isbn book unless Book.find_by_isbn book['isbn13'] - user.create_book_instance book['isbn13'] + if book['isbn13'] != nil + Book.create_book_by_isbn book unless Book.find_by_isbn book['isbn13'] + user.create_book_instance book['isbn13'] + end } + end end \ No newline at end of file diff --git a/config/database.yml b/config/database.yml index 60a13ea..e1710ad 100644 --- a/config/database.yml +++ b/config/database.yml @@ -16,7 +16,7 @@ development: adapter: postgresql encoding: unicode database: libr_development - pool: 5 + pool: 20 host: localhost user: postgres password: @@ -52,7 +52,7 @@ production: adapter: postgresql encoding: unicode database: libr_production - pool: 5 + pool: 20 host: localhost user: postgres password: diff --git a/db/migrate/20140622005440_add_import_douban_book_status_to_third_party_data.rb b/db/migrate/20140622005440_add_import_douban_book_status_to_third_party_data.rb new file mode 100644 index 0000000..b234b19 --- /dev/null +++ b/db/migrate/20140622005440_add_import_douban_book_status_to_third_party_data.rb @@ -0,0 +1,6 @@ +class AddImportDoubanBookStatusToThirdPartyData < ActiveRecord::Migration + def change + + add_column :third_party_user_data, :import_douban_done, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 7d2894d..73dc9c6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140615114344) do +ActiveRecord::Schema.define(version: 20140622005440) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -135,6 +135,7 @@ ActiveRecord::Schema.define(version: 20140615114344) do t.datetime "created_at" t.datetime "updated_at" t.integer "user_id" + t.boolean "import_douban_done", default: false end create_table "user_to_books", force: true do |t| diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index cdf2ca5..c64edd1 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -11,7 +11,7 @@ describe :User do it 'ensure user can borrow a existed book' do instance = BookInstance.create book_id: @book.id, user_id: @user.id @user.borrow instance - @user.borrowed_and_not_returned_books.empty?.should == false + @user.borrowed_and_not_returned_books.empty?.should == false @user.borrowed_and_not_returned_books.count.should == 1 end it 'ensure when there are two books,users can borrow these two books.' do @@ -21,7 +21,7 @@ describe :User do instance_2 = BookInstance.create book_id: book_1.id, user_id: user_1.id user_1.borrowed_and_not_returned_books.count.should == 0 user_1.borrow instance - user_1.borrowed_and_not_returned_books.empty?.should == false + user_1.borrowed_and_not_returned_books.empty?.should == false user_1.borrowed_and_not_returned_books.count.should == 1 end @@ -106,4 +106,12 @@ describe :User do # end # #end + + it 'should update user third party import douban user status' do + + @user.douban_user 'test' + @user.third_party_user_datas.first.import_douban_done.should == false + @user.import_double_user_done + @user.third_party_user_datas.first.import_douban_done.should == true + end end \ No newline at end of file