openbd 0.2.1 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0bc90f5c6b50c5c21e33735636eb7367e2a0c34b
4
- data.tar.gz: fece3e8792a65303e00f737145be4b1edd6de1b6
3
+ metadata.gz: e4def2d8526429b3dc53b183c07ca1eb78f294b8
4
+ data.tar.gz: 6c5bb89084890943d6a3e81adc1b4793ff177a1e
5
5
  SHA512:
6
- metadata.gz: a3c7b7fa4deb1ad21bb59750cd091e75678bd9c5e1502c1fceff79f3a2db08d3fba72d79a865de0dacbd2b3507fd30d7a0c2df6d5c03db5d8bf5b2ac98787619
7
- data.tar.gz: ab75845f73460bf9e9ef0de488b57e25c08f764ed0be0d0ec46c55c2206c4b8343b5b55f28fc2f5bfbdf1261be83c08fed195d50b774338aaa689e4c39aa20a6
6
+ metadata.gz: d5c93563a76c5d93e2a096fb9fcf7eb40da328c0ec25a82487a7066917d8b226a6f2c8bdd2d0d6c950954d8ceb2f880dc10b0c2da7f96e1d681332a36eda0c79
7
+ data.tar.gz: e2737f2de02240fc4fc61f805148fe6992b86b89e2fad5b306750929a6a53dd43da5f3db53286c184d671a01af7729a1dd30f62f73ec2cccb1e9bc4cbfb3f59c
@@ -1,7 +1,10 @@
1
+ AllCops:
2
+ Excludes:
3
+ - spec/*.rb
4
+ - 'vendor/**/*'
5
+
1
6
  Style/MutableConstant:
2
7
  Enabled: false
3
- Style/Documentation:
4
- Enabled: false
5
8
  Style/EmptyLinesAroundModuleBody:
6
9
  Enabled: false
7
10
  Style/EmptyLinesAroundClassBody:
data/README.md CHANGED
@@ -31,22 +31,64 @@ client = Openbd::Client.new
31
31
 
32
32
  # get
33
33
  client.get('978-4-7808-0204-7')
34
+ client.get('4-06-2630869,978-4-06-2144490')
35
+ client.get(['4-06-2630869', '978-4-06-2144490'])
36
+
37
+ # get less than 10,000 ISBNs
38
+ isbns.size # => 9,999
39
+ client.get(isbns)
40
+ # raise Error if over 10,000 ISBNs
41
+ isbns.size # => 10,001
42
+ client.get(isbns) # => Param limit exceeded.
34
43
 
35
44
  # coverage
36
45
  client.coverage
37
46
  ```
38
47
 
39
- Set `HTTP_SERVER` or `http_server` as Environment Variable if you'd like to access via proxy server.
48
+ You can ccess [HTTPClient](https://github.com/nahi/httpclient)([doc](http://www.rubydoc.info/gems/httpclient/HTTPClient)). For example:
49
+
50
+ ```rb
51
+ client.httpclient.class # => HTTPClient
52
+ # set debug output device
53
+ client.httplicent.debug_dev = STDOUT
54
+ # set timeout param
55
+ client.connect_timeout = 100
56
+ client.send_timeout = 100
57
+ client.receive_timeout = 100
58
+ ```
59
+
60
+ ## Using proxy
61
+
62
+ To access resources through HTTP proxy, following methods are available
63
+
64
+ 1. Set Environment Variable
65
+ 1. Set `HTTPClient#proxy=(proxy)`
66
+
67
+ ### Set Environment Variable
68
+
69
+ Set `HTTP_SERVER` or `http_server` as Environment Variable.
40
70
 
41
71
  ```
42
72
  export HTTP_PROXY=http://user:pass@host:port
43
73
  # or
44
- export http_proxy=http://user:pass@host:port
74
+ #export http_proxy=http://user:pass@host:port
75
+ ```
76
+
77
+ ### Set `HTTPClient#proxy=(proxy)`
78
+
79
+ `#httpclient` returns [HTTPClient](http://www.rubydoc.info/gems/httpclient/HTTPClient) instance.
80
+
81
+ ```rb
82
+ require 'openbd'
83
+
84
+ client = Openbd::Client.new
85
+ client.httpclient.class # => HTTPClient
86
+ client.httpclient.proxy = 'http://user:pass@host:port'
45
87
  ```
46
88
 
47
89
  ## Requirements
48
90
 
49
- - Ruby(MRI) 2.3.0 or higher
91
+ - Ruby(MRI) 2.2.0 or higher
50
92
 
51
93
  ## License
52
94
 
@@ -1,58 +1,2 @@
1
1
  require 'openbd/version'
2
- require 'httpclient'
3
- require 'json'
4
-
5
- module Openbd
6
-
7
- class RequestError < StandardError; end
8
-
9
- END_POINT = 'https://api.openbd.jp/v1'
10
-
11
- class Client
12
-
13
- def get(isbn)
14
- _get(isbn, :get)
15
- end
16
-
17
- def get_big(isbn)
18
- _get(isbn, :post)
19
- end
20
-
21
- def coverage
22
- url = "#{END_POINT}/coverage"
23
- resp = client.get(url)
24
- body(resp)
25
- end
26
-
27
- private
28
-
29
- def client
30
- @client ||= HTTPClient.new
31
- end
32
-
33
- def body(resp)
34
- JSON.parse(resp.body)
35
- end
36
-
37
- def _get(isbn, method)
38
- return unless [:get, :post].include?(method)
39
-
40
- url = "#{END_POINT}/get"
41
- q = { isbn: isbn_param(isbn) }
42
- resp = client.send(method, url, q)
43
- body(resp)
44
- end
45
-
46
- def isbn_param(value)
47
- isbns = if value.instance_of?(String)
48
- value.split(',')
49
- elsif value.instance_of?(Array)
50
- value
51
- else
52
- raise Openbd::RequestError,
53
- "Invalid type of param: #{value.class}(#{value})"
54
- end
55
- isbns.join(',')
56
- end
57
- end
58
- end
2
+ require 'openbd/client'
@@ -0,0 +1,86 @@
1
+ require 'httpclient'
2
+ require 'json'
3
+
4
+ module Openbd
5
+
6
+ # Raised for request error.
7
+ class RequestError < StandardError; end
8
+
9
+ END_POINT = 'https://api.openbd.jp/v1'
10
+
11
+ # The Client class provides a wrapper to the openBD service
12
+ #
13
+ # @see https://openbd.jp/
14
+ # @example
15
+ # require 'openbd'
16
+ #
17
+ # client = Openbd::Client.new
18
+ #
19
+ # # get
20
+ # client.get('978-4-7808-0204-7')
21
+ class Client
22
+
23
+ # call /get
24
+ #
25
+ # @param [String|Array] isbn ISBN
26
+ # @raise [RequestError] if the request is wrong
27
+ # @return [Hash] Biblio infomation
28
+ def get(isbn)
29
+ url = "#{END_POINT}/get"
30
+ isbns = isbn_param(isbn)
31
+ method = get_method(isbns)
32
+ q = { isbn: isbns.join(',') }
33
+ resp = httpclient.send(method, url, q)
34
+ body(resp)
35
+ end
36
+
37
+ # call /coverage
38
+ #
39
+ # @return [Array<String>] List of ISBN
40
+ def coverage
41
+ url = "#{END_POINT}/coverage"
42
+ resp = httpclient.get(url)
43
+ body(resp)
44
+ end
45
+
46
+ # call /schema
47
+ #
48
+ def schema
49
+ url ="#{END_POINT}/schema"
50
+ resp = httpclient.get(url)
51
+ body(resp)
52
+ end
53
+
54
+ # get HTTPClient
55
+ #
56
+ # @return [HTTPClient] HTTPClient
57
+ def httpclient
58
+ @httpclient ||= HTTPClient.new
59
+ end
60
+
61
+ private
62
+
63
+ def body(resp)
64
+ JSON.parse(resp.body)
65
+ end
66
+
67
+ def get_method(isbn_num)
68
+ if isbn_num.size > 10_000
69
+ raise Openbd::RequestError, 'Param limit exceeded.'
70
+ end
71
+
72
+ isbn_num.size > 1_000 ? :post : :get
73
+ end
74
+
75
+ def isbn_param(value)
76
+ if value.instance_of?(String)
77
+ value.split(',')
78
+ elsif value.instance_of?(Array)
79
+ value
80
+ else
81
+ raise Openbd::RequestError,
82
+ "Invalid type of param: #{value.class}(#{value})"
83
+ end
84
+ end
85
+ end
86
+ end
@@ -1,3 +1,3 @@
1
1
  module Openbd
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -24,5 +24,6 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency 'rake', '~> 10.0'
25
25
  spec.add_development_dependency 'rspec', '~> 3.0'
26
26
  spec.add_development_dependency 'webmock', '~> 2.3.0'
27
+ spec.add_development_dependency 'sinatra'
27
28
  spec.add_runtime_dependency 'httpclient', '~> 2.8.3'
28
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openbd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kyoshidajp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-09 00:00:00.000000000 Z
11
+ date: 2017-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 2.3.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: sinatra
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: httpclient
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -96,6 +110,7 @@ files:
96
110
  - README.md
97
111
  - Rakefile
98
112
  - lib/openbd.rb
113
+ - lib/openbd/client.rb
99
114
  - lib/openbd/version.rb
100
115
  - openbd.gemspec
101
116
  homepage: http://github.com/kyoshidajp/openbd