class Taaze::TaazeCollections

This class get the user personal page as an input return a hash of user's collections information For example, with user's url: www.taaze.tw/container_zekeaclt_view.html?ci=12522728 here's the user's collections of books: www.taaze.tw/container_zekeaclt_view.html?ci=12522728&cp=1 Sampel output (plz remove them after passing the test): [

{"title"=>"村上收音機", "book_url"=>"http://www.taaze.tw/sing.html?pid=11100635522"}
{"title"=>"尋找漩渦貓的方法", "book_url"=>"http://www.taaze.tw/sing.html?pid=11100210251"}
and so on ...

]

Constants

API_URL
BOOK_URL
USERS_URL

Public Class Methods

new(user_id) click to toggle source
# File lib/taaze/collections.rb, line 21
def initialize(user_id)
  parse_html(user_id)
end

Public Instance Methods

collections() click to toggle source

Return a hash of user's collections

# File lib/taaze/collections.rb, line 26
def collections
  @collections ||= extract_books
end

Private Instance Methods

extract_books() click to toggle source

Return the books in the format specified in spec.

# File lib/taaze/collections.rb, line 43
def extract_books
  booklist = []
  if @doc.count != 0
    @doc.each do |book_data|
      book = {}
      book['title'] = book_data['titleMain']
      book['book_url'] = BOOK_URL + book_data['prodId']
      book['crt_time'] = book_data['crtTime'].split(' ')[0]
      booklist << book
    end
  end
  booklist
end
parse_html(user_id) click to toggle source

Parse the html of the url page.

# File lib/taaze/collections.rb, line 33
def parse_html(user_id)
  url = API_URL + user_id
  first_doc = JSON.parse(open(url).read)
  totalsize = first_doc['totalsize']

  url = API_URL + user_id + '&startNum=0&endNum=' + totalsize.to_s
  @doc = JSON.parse(open(url).read)['result1']
end