class Openbd::Client

The Client class provides a wrapper to the openBD service

@see openbd.jp/ @example

require 'openbd'

client = Openbd::Client.new

# get
client.get('978-4-7808-0204-7')

Public Instance Methods

coverage() click to toggle source

call /coverage

@return [Array<String>] List of ISBN

# File lib/openbd/client.rb, line 40
def coverage
  url = "#{END_POINT}/coverage"
  resp = httpclient.get(url)
  body(resp)
end
get(isbn) click to toggle source

call /get

@param [String|Array] isbn ISBN @raise [RequestError] if the request is wrong @return [Hash] Biblio infomation

# File lib/openbd/client.rb, line 28
def get(isbn)
  url = "#{END_POINT}/get"
  isbns = isbn_param(isbn)
  method = get_method(isbns)
  q = { isbn: isbns.join(',') }
  resp = httpclient.send(method, url, q)
  body(resp)
end
httpclient() click to toggle source

get HTTPClient

@return [HTTPClient] HTTPClient

# File lib/openbd/client.rb, line 57
def httpclient
  @httpclient ||= HTTPClient.new
end
schema() click to toggle source

call /schema

# File lib/openbd/client.rb, line 48
def schema
  url ="#{END_POINT}/schema"
  resp = httpclient.get(url)
  body(resp)
end

Private Instance Methods

body(resp) click to toggle source
# File lib/openbd/client.rb, line 63
def body(resp)
  JSON.parse(resp.body)
end
get_method(isbn_num) click to toggle source
# File lib/openbd/client.rb, line 67
def get_method(isbn_num)
  if isbn_num.size > 10_000
    raise Openbd::RequestError, 'Param limit exceeded.'
  end

  isbn_num.size > 1_000 ? :post : :get
end
isbn_param(value) click to toggle source
# File lib/openbd/client.rb, line 75
def isbn_param(value)
  if value.instance_of?(String)
    value.split(',')
  elsif value.instance_of?(Array)
    value
  else
    raise Openbd::RequestError,
          "Invalid type of param: #{value.class}(#{value})"
  end
end