mini-douban 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module Mini
2
2
  module Douban
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
data/lib/mini/douban.rb CHANGED
@@ -7,42 +7,45 @@ module Mini
7
7
 
8
8
  BOOK_API_WITH_ID = ' http://api.douban.com/book/subject/'
9
9
  BOOK_API_WITH_ISBN = 'http://api.douban.com/book/subject/isbn/'
10
- def self.book_api(opts = {})
11
- if !opts.respond_to? :keys or opts.empty?
12
- return 'please input a hash for api options. like {:id=>2023013,:isbn=>9787543639133,:original=>true}'
13
- end
14
- opts = opts.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
15
- if opts[:id]
16
- book_with_xml = Net::HTTP.get_response(URI.parse(BOOK_API_WITH_ID + opts[:id].to_s)).body
17
- elsif opts[:isbn]
18
- book_with_xml = Net::HTTP.get_response(URI.parse(BOOK_API_WITH_ISBN + opts[:isbn].to_s)).body
19
- else
20
- return 'input options lack id or isbn'
21
- end
22
10
 
11
+ def self.book_api(opts = {})
23
12
  begin
13
+ if !opts.respond_to? :keys or opts.empty?
14
+ return 'please input a hash for api options. like {:id=>2023013,:isbn=>9787543639133,:original=>true}'
15
+ end
16
+
17
+ opts = opts.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
18
+
19
+ if opts[:id]
20
+ book_with_xml = Net::HTTP.get_response(URI.parse(BOOK_API_WITH_ID + opts[:id].to_s)).body
21
+ elsif opts[:isbn]
22
+ book_with_xml = Net::HTTP.get_response(URI.parse(BOOK_API_WITH_ISBN + opts[:isbn].to_s)).body
23
+ else
24
+ return 'input options lack id or isbn'
25
+ end
26
+
24
27
  hash_book = XmlSimple.xml_in(book_with_xml)
25
- rescue Exception => e
26
- return 'Not found book in douban api with id or isbn'
27
- end
28
28
 
29
- if hash_book.include?('attribute') and opts[:original] != true
30
- sample_book = {}
31
- hash_book['attribute'].map{|attr| sample_book[attr['name']] = attr['content']}
32
- sample_book['tags'] = hash_book['tag'].map{ |tag| tag['name'] } if hash_book['tag']
33
- sample_book['summary'] = hash_book['summary'].join if hash_book['summary']
34
- sample_book['links'] = hash_book['link'].map{ |m| m['href']} if hash_book['link']
35
- sample_book['rating'] = hash_book['rating'].first['average'] if hash_book['rating']
36
- sample_book['rater_number'] = hash_book['rating'].first['numRaters'] if hash_book['rating']
37
- if sample_book['links']
38
- sample_book['image_url'] = sample_book['links'].select{ |s| s.start_with?('http://img')}.join
39
- sample_book['api_url'] = sample_book['links'].select{ |s| s.start_with?('http://api')}.join
40
- sample_book['mobile_url'] = sample_book['links'].select{ |s| s.start_with?('http://m')}.join
41
- sample_book['website_url'] = sample_book['links'].select{ |s| s.start_with?('http://book')}.join
29
+ if hash_book.include?('attribute') and opts[:original] != true
30
+ sample_book = {}
31
+ hash_book['attribute'].map{|attr| sample_book[attr['name']] = attr['content']}
32
+ sample_book['tags'] = hash_book['tag'].map{ |tag| tag['name'] } if hash_book['tag']
33
+ sample_book['summary'] = hash_book['summary'].join if hash_book['summary']
34
+ sample_book['links'] = hash_book['link'].map{ |m| m['href']} if hash_book['link']
35
+ sample_book['rating'] = hash_book['rating'].first['average'] if hash_book['rating']
36
+ sample_book['rater_number'] = hash_book['rating'].first['numRaters'] if hash_book['rating']
37
+ if sample_book['links']
38
+ sample_book['image_url'] = sample_book['links'].select{ |s| s.start_with?('http://img')}.join
39
+ sample_book['api_url'] = sample_book['links'].select{ |s| s.start_with?('http://api')}.join
40
+ sample_book['mobile_url'] = sample_book['links'].select{ |s| s.start_with?('http://m')}.join
41
+ sample_book['website_url'] = sample_book['links'].select{ |s| s.start_with?('http://book')}.join
42
+ end
43
+ return sample_book.inject({}){|book_info,(k,v)| book_info[k.to_sym] = v; book_info}
42
44
  end
43
- return sample_book.inject({}){|book_info,(k,v)| book_info[k.to_sym] = v; book_info}
45
+ hash_book
46
+ rescue Exception => e
47
+ return 'Not found book in douban api with id or isbn'
44
48
  end
45
- hash_book
46
49
  end
47
50
 
48
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini-douban
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: