calil 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ba0e5bc304523134cf084e10c0a598403c0957ac
4
+ data.tar.gz: 532f9b87c21d154983d2e3907cf1749ab6d0aa01
5
+ SHA512:
6
+ metadata.gz: 70797efdf3b3baf08b2a9b254e906420d1b984d98b20acf645abe1d565b30819514c953235fdd8279605835ffd8f9434a7eb8c1c0a0b4a9f2ae2c4bd266a52ab
7
+ data.tar.gz: d0a1b5208e1c12177386de37b14aadcbfbab4f33223490f4f34e373180f608f3b95e3907c9841691d4761d0633ee7d1daa1630d182bb0d59cf4cf3dac2335a34
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in calil.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 2celeb
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,68 @@
1
+ # Calil
2
+
3
+ 図書館検索のカーリルのapiのruby用のラッパーです
4
+
5
+ #### できること
6
+
7
+ * 図書館の所在検索
8
+ * 指定した書籍の存在確認
9
+ * 指定した書籍の予約urlの取得
10
+
11
+ #### 詳しくはカーリル様のサイトで確認してください。
12
+ http://calil.jp/
13
+
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ gem 'calil'
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install calil
28
+
29
+ ## Usage
30
+
31
+ ### 図書館を検索する
32
+ ```ruby
33
+ Calil::Library.find(pref: "東京都").first
34
+ ```
35
+
36
+ ```
37
+ # 図書館の情報を検索できる
38
+ => #<Library systemid: 'Tokyo_Akishima', systemname: '東京都昭島市', libkey: 'BM', libid: '103852', short: 'もくせい号', formal: '昭島市動く図書館「もくせい号」', url_pc: 'http://www.library.akishima.tokyo.jp/index.html', address: '東京都昭島市東町2-6-33', pref: '東京都', city: '昭島市', post: '196-0033', tel: '042-543-1523', geocode: '139.3851232,35.7053229', category: 'BM', image: ''>
39
+ ```
40
+
41
+ ### 蔵書情報を検索する
42
+
43
+ ```ruby
44
+ Calil::Book.find(["4844330845", "4798125415"], %w(Tokyo_Minato Tokyo_Chiyoda))
45
+ ```
46
+
47
+ ```
48
+ # 書籍の貸出状況が取得できる
49
+ => [#<Book isbn: '4844330845', calilurl: 'http://calil.jp/book/4844330845', reservable?: 'true' systems: [#<System systemid: 'Tokyo_Chiyoda', status: 'Cache', reserveurl: 'http://www.library.chiyoda.tokyo.jp/wo/opc_srh/srh_detail?detail[sid]=151345917', reservable?: 'true'>, #<System systemid: 'Tokyo_Minato', status: 'Cache', reserveurl: '', reservable?: 'false'>]>, #<Book isbn: '4798125415', calilurl: 'http://calil.jp/book/4798125415', reservable?: 'false' systems: [#<System systemid: 'Tokyo_Chiyoda', status: 'Cache', reserveurl: '', reservable?: 'false'>, #<System systemid: 'Tokyo_Minato', status: 'Cache', reserveurl: '', reservable?: 'false'>]>]
50
+ ```
51
+
52
+ ```ruby
53
+ book = Calil::Book.find(["4844330845", "4798125415"], %w(Tokyo_Minato Tokyo_Chiyoda)).first
54
+ book.reservable?
55
+ ```
56
+
57
+ ```
58
+ # 借りられる
59
+ => true
60
+ ```
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'calil/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "calil"
8
+ spec.version = Calil::VERSION
9
+ spec.authors = ["2celeb"]
10
+ spec.email = ["oooooorz@gmail.com"]
11
+ spec.description = %q{calil api library ruby binding}
12
+ spec.summary = %q{wrapping calil api libray}
13
+ spec.homepage = "http://github.com/2celeb/calil"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,12 @@
1
+ module Calil
2
+ end
3
+
4
+ # load calil components
5
+ require 'rexml/document'
6
+ require 'cgi'
7
+ require 'open-uri'
8
+ require 'calil/version'
9
+ require 'calil/library'
10
+ require 'calil/book'
11
+ require 'calil/system'
12
+
@@ -0,0 +1,110 @@
1
+ module Calil
2
+
3
+ class Book
4
+
5
+ FIND_ENABLE_LIMIT = 100
6
+ RETRY_LIMIT = 10
7
+ RETRY_WAIT_TIME = 5
8
+
9
+ module ContinueStatus
10
+ CONTINUE = "1"
11
+ CLOSE = "0"
12
+ end
13
+
14
+ def self.find(isbns, systemids, params = {})
15
+
16
+ params.merge!({
17
+ isbn: isbns.join(","),
18
+ systemid: systemids.join(","),
19
+ format: "xml"
20
+ })
21
+
22
+ params.merge!(appkey: ENV["CALIL_APP_KEY"]) unless params[:appkey]
23
+ raise "calil appkey notfound" unless params[:appkey]
24
+
25
+ session_id = nil
26
+ continue_status = ContinueStatus::CONTINUE
27
+ retry_count = 0
28
+ http_response_body = nil
29
+
30
+ RETRY_LIMIT.times do |count|
31
+
32
+ retry_count = count
33
+
34
+ http_response_body = request(params, session_id)
35
+
36
+ REXML::Document.new(http_response_body).elements.each("result/continue") {|e| continue_status = e.text }
37
+ REXML::Document.new(http_response_body).elements.each("result/session") {|e| session_id = e.text }
38
+
39
+ break if continue_status == ContinueStatus::CLOSE
40
+
41
+ sleep RETRY_WAIT_TIME
42
+
43
+ end
44
+
45
+ # retry limit error
46
+ raise "retry error" if continue_status == ContinueStatus::CONTINUE && retry_count == (RETRY_LIMIT - 1)
47
+
48
+ books = []
49
+ REXML::Document.new(http_response_body).elements.each("result/books/book") do |element|
50
+ books << Book.new(element)
51
+ end
52
+ books
53
+
54
+ end
55
+
56
+ def self.request(params, session = nil)
57
+
58
+ params.merge!(session: session) if session
59
+ query = params.map {|k,v| "#{k}=#{CGI.escape(v.to_s)}" }.join("&").to_s
60
+ url = "http://api.calil.jp/check?#{query}"
61
+ open(url).read
62
+
63
+ end
64
+
65
+ def initialize(element)
66
+
67
+ @element = element
68
+ @systems = []
69
+ element.elements.each('system') do |system|
70
+ @systems << Calil::System.new(system)
71
+ end
72
+
73
+ end
74
+
75
+ def isbn
76
+ @element.attributes["isbn"]
77
+ end
78
+
79
+ def calilurl
80
+ @element.attributes["calilurl"]
81
+ end
82
+
83
+ def systems
84
+ @systems
85
+ end
86
+
87
+ def reservable?
88
+ # exsit reserveurl in current systems
89
+ !!(@systems.find {|system| system.reservable? })
90
+ end
91
+
92
+ def reservables
93
+ @systems.select {|system| system.reservable? }
94
+ end
95
+
96
+ def inspect
97
+ attr_body = %w(isbn calilurl reservable?).map do |method_name|
98
+ eval("\"#{method_name}: '#{send(method_name)}'\"")
99
+ end.join(", ")
100
+
101
+ systems_body = @systems.map do |sytem|
102
+ sytem.inspect
103
+ end.join(", ")
104
+
105
+ "#<Book #{attr_body} systems: [#{systems_body}]>"
106
+ end
107
+
108
+ end
109
+
110
+ end
@@ -0,0 +1,43 @@
1
+ module Calil
2
+
3
+ class Library
4
+
5
+ def self.find(params = {})
6
+
7
+ params.merge!(appkey: ENV["CALIL_APP_KEY"]) unless params[:appkey]
8
+ raise "calil appkey notfound" unless params[:appkey]
9
+
10
+ query = params.map {|k,v| "#{k}=#{CGI.escape(v.to_s)}" }.join("&").to_s
11
+ url = "http://api.calil.jp/library?#{query}"
12
+
13
+ libraries = []
14
+ REXML::Document.new(open(url)).elements.each('Libraries/Library') do |element|
15
+ libraries << Library.new(element)
16
+ end
17
+
18
+ libraries
19
+ end
20
+
21
+ def initialize(element)
22
+ @element = element
23
+ end
24
+
25
+ def inspect
26
+ attr_body = %w(systemid systemname libkey libid short formal url_pc address pref city post tel geocode category image).map do |method_name|
27
+ eval("\"#{method_name}: '#{send(method_name)}'\"")
28
+ end.join(", ")
29
+ "#<Library #{attr_body}>"
30
+ end
31
+
32
+ def method_missing(action, *args)
33
+
34
+ if %w(systemid systemname libkey libid short formal url_pc address pref city post tel geocode category image).include? action.to_s
35
+ @element.elements[action.to_s].text
36
+ else
37
+ super
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,34 @@
1
+ module Calil
2
+ class System
3
+
4
+ def initialize(element)
5
+ @element = element
6
+ end
7
+
8
+ def systemid
9
+ @element.attributes["systemid"]
10
+ end
11
+
12
+ def reservable?
13
+ !!(@element.elements["reserveurl"] && !@element.elements["reserveurl"].text.to_s.empty?)
14
+ end
15
+
16
+ def method_missing(action, *args)
17
+
18
+ if %w(status reserveurl).include? action.to_s
19
+ @element.elements[action.to_s].text
20
+ else
21
+ super
22
+ end
23
+
24
+ end
25
+
26
+ def inspect
27
+ attr_body = %w(systemid status reserveurl reservable?).map do |method_name|
28
+ eval("\"#{method_name}: '#{send(method_name)}'\"")
29
+ end.join(", ")
30
+ "#<System #{attr_body}>"
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,3 @@
1
+ module Calil
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,30 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Calil::Book do
5
+ describe 'find' do
6
+
7
+ let(:reservable_book) { "4844330845" }
8
+ let(:unreservable_book) { "4798125415" }
9
+ let(:find_book_ids) { [reservable_book, unreservable_book] }
10
+ let(:find_library_ids) { %w(Tokyo_Minato Tokyo_Chiyoda) }
11
+
12
+ subject(:books) { @books = Calil::Book.find(find_book_ids, find_library_ids)
13
+ @books
14
+ }
15
+ it 'should reservable_book reservable' do
16
+ books.first.reservable?.should be_true
17
+ books.first.isbn.should eql(reservable_book)
18
+ end
19
+
20
+ it 'should unreservable_book unreservable' do
21
+ books.last.reservable?.should be_false
22
+ books.last.isbn.should eql(unreservable_book)
23
+ end
24
+
25
+ it 'should reservable_book hava inspect string' do
26
+ books.first.inspect.should eql("#<Book isbn: '4844330845', calilurl: 'http://calil.jp/book/4844330845', reservable?: 'true' systems: [#<System systemid: 'Tokyo_Chiyoda', status: 'Cache', reserveurl: 'http://www.library.chiyoda.tokyo.jp/wo/opc_srh/srh_detail?detail[sid]=151345917', reservable?: 'true'>, #<System systemid: 'Tokyo_Minato', status: 'Cache', reserveurl: '', reservable?: 'false'>]>")
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,18 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Calil::Library do
5
+ describe 'find' do
6
+
7
+ subject(:library) { Calil::Library.find(pref: "東京都").first }
8
+
9
+ it 'first library systemid should "Tokyo_Akishima"' do
10
+ library.systemid.should eql("Tokyo_Akishima")
11
+ end
12
+
13
+ it 'first library should hava inspect string' do
14
+ library.inspect.should eql("#<Library systemid: 'Tokyo_Akishima', systemname: '東京都昭島市', libkey: 'BM', libid: '103852', short: 'もくせい号', formal: '昭島市動く図書館「もくせい号」', url_pc: 'http://www.library.akishima.tokyo.jp/index.html', address: '東京都昭島市東町2-6-33', pref: '東京都', city: '昭島市', post: '196-0033', tel: '042-543-1523', geocode: '139.3851232,35.7053229', category: 'BM', image: ''>")
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'calil'
5
+
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: calil
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - 2celeb
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: calil api library ruby binding
56
+ email:
57
+ - oooooorz@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - calil.gemspec
68
+ - lib/calil.rb
69
+ - lib/calil/book.rb
70
+ - lib/calil/library.rb
71
+ - lib/calil/system.rb
72
+ - lib/calil/version.rb
73
+ - spec/book_spec.rb
74
+ - spec/library_spec.rb
75
+ - spec/spec_helper.rb
76
+ homepage: http://github.com/2celeb/calil
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.0.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: wrapping calil api libray
100
+ test_files:
101
+ - spec/book_spec.rb
102
+ - spec/library_spec.rb
103
+ - spec/spec_helper.rb
104
+ has_rdoc: