Class: BookscanClient
- Inherits:
-
Object
- Object
- BookscanClient
- Defined in:
- lib/bookscan_client.rb,
lib/bookscan_client/version.rb,
lib/bookscan_client/model/book.rb,
lib/bookscan_client/model/optimized_book.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Method Summary collapse
- #books ⇒ Object
- #cookies ⇒ Object
-
#initialize(logger: Logger.new(STDOUT), sleep: 0.5) ⇒ BookscanClient
constructor
A new instance of BookscanClient.
- #login(email, password) ⇒ Object
- #login? ⇒ Boolean
- #optimized_books ⇒ Object
Constructor Details
#initialize(logger: Logger.new(STDOUT), sleep: 0.5) ⇒ BookscanClient
Returns a new instance of BookscanClient.
18 19 20 21 22 |
# File 'lib/bookscan_client.rb', line 18 def initialize(logger: Logger.new(STDOUT), sleep: 0.5) @mechanize = Mechanize.new @logger = logger @sleep = sleep end |
Instance Method Details
#books ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/bookscan_client.rb', line 33 def books page = fetch(URL::MYPAGE) page.search("#sortable_box .showbook").map{|book_link| url = "#{BookscanClient::URL::ROOT}#{book_link.attr("href")}" params = CGI.parse(URI.parse(url).query) next unless %w[f h d].all?{|key| params.keys.include?(key) } image = book_link.search("img") book = BookscanClient::Model::Book.new(filename: params["f"][0], hash: params["h"][0], digest: params["d"][0]) book.image_url = image[0].attr("data-original") unless image.empty? book } end |
#cookies ⇒ Object
59 60 61 |
# File 'lib/bookscan_client.rb', line 59 def @mechanize. end |
#login(email, password) ⇒ Object
24 25 26 |
# File 'lib/bookscan_client.rb', line 24 def login(email, password) @mechanize.post(URL::LOGIN, "email" => email, "password" => password) end |
#login? ⇒ Boolean
28 29 30 31 |
# File 'lib/bookscan_client.rb', line 28 def login? names = .map(&:name) names.include?("email") && names.include?("password") end |
#optimized_books ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/bookscan_client.rb', line 48 def optimized_books page = fetch(URL::OPTIMIZED_BOOKS) page.search("a.download").map{|book_link| url = "#{BookscanClient::URL::ROOT}#{book_link.attr("href")}" params = CGI.parse(URI.parse(url).query) next unless %w[f d].all?{|key| params.keys.include?(key) } BookscanClient::Model::OptimizedBook.new(filename: params["f"][0], digest: params["d"][0]) } end |