Module: Douban::Cite

Defined in:
lib/douban/cite.rb,
lib/douban/cite/version.rb

Constant Summary collapse

Douban_API_base =
'http://api.douban.com'
Douban_Book_info =
Douban_API_base + '/v2/book/'
VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.convert_to_ref(book_info, book_type = 'M') ⇒ String

Format: author. YYYY-MM. title. other_author. location: publisher. ISBN isbn-number This format is based on:

Parameters:

  • (Hash)

Returns:

  • (String)


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/douban/cite.rb', line 40

def convert_to_ref(book_info, book_type='M')
  author = book_info['author'].join(', ')
  year_month = book_info['pubdate']
  title = book_info['title']
  other_author = book_info['translator'].join(', ')
  publisher = book_info['publisher']
  # This is a silly guess.
  # TODO email douban to complain about this.
  location = publisher[0..1]
  isbn = book_info['isbn13']
  # `other_author` is handled specially, because often it is not needed.
  "#{author}. #{year_month}. #{title}[#{book_type}]. #{other_author == '' ? '' : other_author + '. ' }#{location}: #{publisher}. ISBN #{isbn}"
end

.get_book_info(id) ⇒ Hash, String

Parameters:

  • id (String)

    isbn or douban subject id

Returns:

  • (Hash)

    book info if success

  • (String)

    response http code if something is wrong



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/douban/cite.rb', line 18

def get_book_info(id)
  # Currently douban subject id's length is 7.
  if StdNum::ISBN.valid?(id)
    id = 'isbn/' + id
  end
  res = RestClient.get(Douban_Book_info + id)
  if res.code == 200
    JSON.parse(res.body)
  else
    res.code
  end
end