openbd_api 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c7dcb3ed0c130d9e222e573f54417bc45f3b1984
4
- data.tar.gz: b3825860950b217baba0a77114d82b4fe3da86d5
3
+ metadata.gz: 54f3cce323aa77efd433d9884d6951cb6fc47ef5
4
+ data.tar.gz: b64a41747e5fc80aa7cb5f41bc13849b51471a4a
5
5
  SHA512:
6
- metadata.gz: fff1f41c8f60e408d14564d1f13f9f2965ee347ca0c3f311e71a2d684cd44a3743de87ebf70ddb4a6a6b96e1a3c7aabfb93790878f6754dd7220220523061412
7
- data.tar.gz: '078141c4baa463e663ad60c758431cee524f56723f3f121dbc0e83a7b7dbd0740ae141eb44f1a6483cc998b0c65adec093e629a1b42b7c4d43a756afac92ece3'
6
+ metadata.gz: '08a4edb19a321c6864a1d5297c782b9ed8644edcccc8fea0488d05ebc38d9bbdc22995bff13ca8620eeb5286f1f1d907543cdc8cd248f6bc1ff2caa98e1fec03'
7
+ data.tar.gz: 19d69cae86102f6fcce6f40b770f8a7b026dc4632287db8a09b44fd90acdea55b41496ede1f857abe3570d2ed212b063bddda03e5c8111d0f2c8e4428d3c77b8
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # OpenBD
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/openbd_api.svg)](https://badge.fury.io/rb/openbd_api)
3
4
  [![Build Status](https://travis-ci.org/nasum/openbd.svg?branch=master)](https://travis-ci.org/nasum/openbd)
4
5
 
5
6
  This gem wraps openBD Web API.
@@ -22,6 +23,8 @@ Or install it yourself as:
22
23
 
23
24
  ## Usage
24
25
 
26
+ ### Library
27
+
25
28
  Like this.
26
29
 
27
30
  ```rb
@@ -44,6 +47,17 @@ OpenBD.coverage
44
47
  OpenBD.schema
45
48
  ```
46
49
 
50
+ ### CLI
51
+
52
+ `openbd` executable calls `OpenBD` `s methods.
53
+
54
+ ```
55
+ $ openbd get 9784309226712
56
+ $ openbd bulk_get 9784309226712 9784309226729
57
+ $ openbd coverage
58
+ $ openbd schema
59
+ ```
60
+
47
61
  ## Contributing
48
62
 
49
63
  Bug reports and pull requests are welcome on GitHub at https://github.com/nasum/openbd. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
data/bin/openbd ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
3
+
4
+ require 'openbd_api'
5
+
6
+ openbd_cli = OpenbdCLI.new(ARGV)
7
+ openbd_cli.call
@@ -0,0 +1,62 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ class OpenBD
5
+ API_BASE_URL = 'http://api.openbd.jp/v1/'.freeze
6
+
7
+ class << self
8
+ def get(isbns)
9
+ request_url = prepare_url('get', isbns)
10
+ response = Net::HTTP.get_response(URI::parse(request_url))
11
+ create_body(response).select { |item| item != nil }
12
+ end
13
+
14
+ def bulk_get(isbns)
15
+ request_url = prepare_url('get')
16
+ response = Net::HTTP.post_form(URI::parse(request_url), isbn: normalize_isbns(isbns))
17
+ create_body(response).select { |item| item != nil }
18
+ end
19
+
20
+ def coverage
21
+ request_url = prepare_url('coverage')
22
+ response = Net::HTTP.get_response(URI::parse(request_url))
23
+ create_body(response)
24
+ end
25
+
26
+ def schema
27
+ request_url = prepare_url('schema')
28
+ response = Net::HTTP.get_response(URI::parse(request_url))
29
+ create_body(response)
30
+ end
31
+
32
+ def create_body(response)
33
+ JSON.parse response.body
34
+ end
35
+
36
+ def prepare_url(method, isbns = nil)
37
+ if isbns.kind_of? String
38
+ params = normalize_isbns(isbns)
39
+ "#{API_BASE_URL}#{method}?isbn=#{params}"
40
+ elsif isbns.kind_of? Array
41
+ params = normalize_isbns(isbns)
42
+ "#{API_BASE_URL}#{method}?isbn=#{params}"
43
+ else
44
+ "#{API_BASE_URL}#{method}"
45
+ end
46
+ end
47
+
48
+ def normalize_isbns(isbns)
49
+ params = ""
50
+ if isbns.kind_of? String
51
+ params << isbns
52
+ elsif isbns.kind_of? Array
53
+ isbns.each do |isbn|
54
+ params << "#{isbn}, "
55
+ end
56
+ params.strip!.gsub!(/,$/,'')
57
+ end
58
+
59
+ params
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,23 @@
1
+ class OpenbdCLI
2
+ def initialize(argv)
3
+ @argv = argv
4
+ end
5
+
6
+ def call
7
+ if params.empty?
8
+ puts OpenBD.send(method_name)
9
+ else
10
+ puts OpenBD.send(method_name, params)
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def method_name
17
+ @argv[0]
18
+ end
19
+
20
+ def params
21
+ @argv[1..-1]
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Openbd
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/openbd_api.rb CHANGED
@@ -1,53 +1,3 @@
1
1
  require 'openbd/version'
2
- require 'net/http'
3
- require 'json'
4
-
5
- class OpenBD
6
- API_BASE_URL = 'http://api.openbd.jp/v1/'.freeze
7
-
8
- class << self
9
- def get(isbns)
10
- request_url = prepare_url('get', isbns)
11
- response = Net::HTTP.get_response(URI::parse(request_url))
12
- create_body(response).select { |item| item != nil }
13
- end
14
-
15
- def bulk_get(isbns)
16
- request_url = prepare_url('get')
17
- response = Net::HTTP.post_form(URI::parse(request_url), isbn: isbns)
18
- create_body(response).select { |item| item != nil }
19
- end
20
-
21
- def coverage
22
- request_url = prepare_url('coverage')
23
- response = Net::HTTP.get_response(URI::parse(request_url))
24
- create_body(response)
25
- end
26
-
27
- def schema
28
- request_url = prepare_url('schema')
29
- response = Net::HTTP.get_response(URI::parse(request_url))
30
- create_body(response)
31
- end
32
-
33
- def create_body(response)
34
- JSON.parse response.body
35
- end
36
-
37
- def prepare_url(method, isbns = nil)
38
- if isbns.kind_of? String
39
- params = "isbn=#{isbns}"
40
- "#{API_BASE_URL}#{method}?#{params}"
41
- elsif isbns.kind_of? Array
42
- params = "isbn="
43
- isbns.each do |isbn|
44
- params << "#{isbn}, "
45
- end
46
- params.strip!.gsub!(/,$/,'')
47
- "#{API_BASE_URL}#{method}?#{params}"
48
- else
49
- "#{API_BASE_URL}#{method}"
50
- end
51
- end
52
- end
53
- end
2
+ require 'openbd/client'
3
+ require 'openbd/openbd_cli'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openbd_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nasum
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-06 00:00:00.000000000 Z
11
+ date: 2017-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -68,6 +68,9 @@ files:
68
68
  - LICENSE.txt
69
69
  - README.md
70
70
  - Rakefile
71
+ - bin/openbd
72
+ - lib/openbd/client.rb
73
+ - lib/openbd/openbd_cli.rb
71
74
  - lib/openbd/version.rb
72
75
  - lib/openbd_api.rb
73
76
  - openbd_api.gemspec