Sha256: 66c4a9e6dc0bb97eebb4ec2f1ebeeb112661ec8fd6815fd3330147830920a35d

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require 'faraday'
require 'faraday_middleware'
require 'json'

class OpenBD
  API_BASE_URL = 'http://api.openbd.jp/'.freeze
  PATH_TO_GET = 'v1/get'
  PATH_TO_COVERAGE = 'v1/coverage'
  PATH_TO_SCHEMA = 'v1/schema'

  class << self
    def get(isbns)
      response = get_request(PATH_TO_GET, { isbn: normalize_isbns(isbns) })
      response.body
    end

    def bulk_get(isbns)
      response = post_request(PATH_TO_GET, { isbn: isbns })
      response.body
    end

    def coverage
      response = get_request(PATH_TO_COVERAGE)
      response.body
    end

    def schema
      response = get_request(PATH_TO_SCHEMA)
      response.body
    end

    def connection
      Faraday::Connection.new(url: API_BASE_URL) do |connection|
        connection.adapter :net_http
        connection.response :json
      end
    end

    def get_request(method, params = nil)
      faraday_response = connection.get(method, params)
    end

    def post_request(method, params)
      faraday_response = connection.post do |req|
        req.url method
        req.body = "isbn=#{normalize_isbns(params[:isbn])}"
      end
    end

    def normalize_isbns(isbns)
      case isbns
      when String
        isbns
      when Numeric
        isbns
      when Array
        params = ""
        isbns.each do |isbn|
          params << "#{isbn}, "
        end
        params.strip!.gsub(/,$/,'')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
openbd_api-0.3.3 lib/openbd/client.rb