hondana 0.0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +6 -0
  2. data/lib/hondana.rb +97 -38
  3. data/test/test_hondana.rb +28 -4
  4. metadata +4 -4
@@ -13,6 +13,12 @@ Access hondana.org DB from Ruby
13
13
  == SYNOPSIS:
14
14
 
15
15
  require "hondana"
16
+ include Hondana
17
+ shelf = Shelf.new('増井')
18
+ shelf.books.each { |book|
19
+ puts book.isbn
20
+ puts book.title
21
+ }
16
22
 
17
23
  == REQUIREMENTS:
18
24
 
@@ -4,12 +4,13 @@
4
4
  #
5
5
  require 'net/http'
6
6
  require 'json'
7
+ require 'cgi'
7
8
 
8
9
  $:.unshift(File.dirname(__FILE__)) unless
9
10
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
10
11
 
11
- class Hondana
12
- VERSION = '0.0.1'
12
+ module Hondana
13
+ VERSION = '0.1.1'
13
14
 
14
15
  def Hondana.http_get(addr)
15
16
  ret = nil
@@ -28,66 +29,124 @@ class Hondana
28
29
 
29
30
  class Book
30
31
  def initialize(isbn)
31
- data = JSON.parse(Hondana.http_get("/bookinfo?isbn=#{isbn}"))
32
32
  @isbn = isbn
33
+ @title = nil
34
+ @authors = nil
35
+ @publisher = nil
36
+ end
37
+ attr_reader :isbn
38
+
39
+ def getdata
40
+ data = JSON.parse(Hondana.http_get("/bookinfo?isbn=#{CGI.escape(isbn)}"))
33
41
  @title = data['title']
34
42
  @authors = data['authors']
35
43
  @publisher = data['publisher']
36
44
  end
37
- attr_reader :title
38
- attr_reader :isbn
39
- attr_reader :authors
40
- attr_reader :publisher
45
+
46
+ def title
47
+ getdata unless @title
48
+ @title
49
+ end
50
+
51
+ def authors
52
+ getdata unless @authors
53
+ @authors
54
+ end
55
+
56
+ def publisher
57
+ getdata unless @publisher
58
+ @publisher
59
+ end
60
+
61
+ def shelves
62
+ shelfnames = JSON.parse(Hondana.http_get("/shelves?isbn=#{isbn}"))
63
+ shelfnames.collect { |name|
64
+ Shelf.new(name)
65
+ }
66
+ end
41
67
  end
42
68
 
43
69
  class Shelf
44
70
  def initialize(name)
45
- data = JSON.parse(Hondana.http_get("/shelfinfo?shelf=#{name}"))
46
71
  @name = name
72
+ @url = nil
73
+ @description = nil
74
+ end
75
+ attr_reader :name
76
+
77
+ def getdata
78
+ data = JSON.parse(Hondana.http_get("/shelfinfo?shelf=#{CGI.escape(name)}"))
47
79
  @url = data['url']
48
80
  @description = data['description']
49
81
  end
50
- attr_reader :name
51
- attr_reader :description
52
- attr_reader :url
53
- end
54
82
 
55
- def initialize
83
+ def url
84
+ getdata unless @url
85
+ @url
86
+ end
87
+
88
+ def description
89
+ getdata unless @description
90
+ @description
91
+ end
92
+
93
+ def books
94
+ isbns = JSON.parse(Hondana.http_get("/books?shelf=#{@name}"))
95
+ isbns.collect { |isbn|
96
+ Book.new(isbn)
97
+ }
98
+ end
56
99
  end
57
100
 
58
- def isbns(shelf='',pattern='')
59
- if shelf == '' && pattern == ''
60
- JSON.parse(Hondana.http_get("/books"))
61
- elsif pattern == ''
62
- JSON.parse(Hondana.http_get("/books?shelf=#{shelf}"))
63
- elsif shelf == ''
64
- JSON.parse(Hondana.http_get("/books?pattern=#{pattern}"))
65
- else
66
- JSON.parse(Hondana.http_get("/books?pattern=#{shelf},#{pattern}"))
101
+ class Entry
102
+ def initialize(shelf,book)
103
+ @shelf = shelf
104
+ if shelf.class == String
105
+ @shelf = Shelf.new(shelf)
106
+ end
107
+ @book = book
108
+ if book.class == String
109
+ @book = Book.new(book)
110
+ end
111
+ @comment = nil
112
+ @score = nil
113
+ @categories = nil
114
+
115
+ data = JSON.parse(Hondana.http_get("/entry?shelf=#{@shelf.name}&isbn=#{@book.isbn}"))
116
+ @comment = data['comment']
117
+ @score = data['score']
118
+ @categories = data['categories']
67
119
  end
120
+ attr_reader :shelf
121
+ attr_reader :book
122
+ attr_reader :comment
123
+ attr_reader :categories
124
+ attr_reader :score
125
+
68
126
  end
69
127
 
70
- def books(shelf='',pattern='')
71
- isbns(shelf,pattern).collect { |isbn|
128
+ def Hondana.books(pattern=nil)
129
+ isbns =
130
+ if pattern
131
+ JSON.parse(Hondana.http_get("/books?pattern=#{pattern}"))
132
+ else
133
+ JSON.parse(Hondana.http_get("/books"))
134
+ end
135
+ isbns.collect { |isbn|
72
136
  Book.new(isbn)
73
137
  }
74
138
  end
75
139
 
76
- def shelfnames(isbn='',pattern='')
77
- if isbn == '' && pattern == ''
78
- JSON.parse(Hondana.http_get("/shelves"))
79
- elsif pattern == ''
80
- JSON.parse(Hondana.http_get("/shelves?isbn=#{isbn}"))
81
- elsif isbn == ''
82
- JSON.parse(Hondana.http_get("/shelves?pattern=#{pattern}"))
83
- else
84
- JSON.parse(Hondana.http_get("/shelves?pattern=#{isbn},#{pattern}"))
85
- end
86
- end
87
-
88
- def shelves(isbn='',pattern='')
89
- shelfnames(isbn,pattern).collect { |name|
140
+ def Hondana.shelves(pattern=nil)
141
+ shelves =
142
+ if pattern
143
+ JSON.parse(Hondana.http_get("/shelves?pattern=#{pattern}"))
144
+ else
145
+ JSON.parse(Hondana.http_get("/shelves"))
146
+ end
147
+ shelves.collect { |name|
90
148
  Shelf.new(name)
91
149
  }
92
150
  end
93
151
  end
152
+
@@ -1,13 +1,37 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require File.dirname(__FILE__) + '/test_helper.rb'
2
3
 
3
4
  class TestHondana < Test::Unit::TestCase
5
+ include Hondana
4
6
 
5
7
  def setup
6
8
  end
7
9
 
8
- def test_books
9
- h = Hondana.new
10
- assert h.isbns('yuco').member?('4102113010')
11
- # assert h.isbns(nil,'yuco').member?('4102113010')
10
+ def test_book
11
+ book = Book.new('4102113010')
12
+ assert book.title.class == String
13
+ assert book.title =~ /赤毛のアン/
14
+ end
15
+
16
+ def test_shelf
17
+ shelf = Shelf.new('yuco')
18
+ assert shelf.name.class == String
19
+ end
20
+
21
+ def test_2
22
+ shelf = Shelf.new('yuco')
23
+ isbns = shelf.books.collect { |book|
24
+ book.isbn
25
+ }
26
+ assert isbns.member?('4102113010')
27
+ end
28
+
29
+ def test_3
30
+ allshelves = Hondana.shelves
31
+ allnames = allshelves.collect { |shelf|
32
+ shelf.name
33
+ }
34
+ assert allnames.member?('yuco')
35
+ assert allnames.member?('増井')
12
36
  end
13
37
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hondana
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Toshiyuki Masui
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-01-06 00:00:00 Z
18
+ date: 2013-01-07 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rdoc