Sha256: 8a1d285c7902042e95f5321b159c6fa2e7d6068fd81903e852d749425e891788

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

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

    def initialize(adapter: :net_http, response_parser: :json)
      @adapter = adapter
      @response_parser = response_parser
    end

    def get(isbns)
      get_request(
        method: PATH_TO_GET,
        params: { isbn: normalize_isbns(isbns) },
        response_class: ::OpenBD::Responses::Get
      )
    end

    def bulk_get(isbns)
      post_request(
        method: PATH_TO_GET,
        params: { isbn: normalize_isbns(isbns) },
        response_class: ::OpenBD::Responses::Get
      )
    end

    def coverage
      get_request(
        method: PATH_TO_COVERAGE,
        params: nil,
        response_class: ::OpenBD::Responses::Coverage
      )
    end

    def schema
      get_request(
        method: PATH_TO_SCHEMA,
        params: nil,
        response_class: ::OpenBD::Responses::Schema
      )
    end

    def connection
      @connection ||= ::Faraday::Connection.new(url: API_BASE_URL) do |connection|
        connection.adapter @adapter
        connection.response @response_parser
      end
    end

    def get_request(method:, params:, response_class:)
      faraday_response = connection.get(method, params)
      response_class.new(faraday_response)
    end

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

    def normalize_isbns(isbns)
      case isbns
      when String
        isbns
      when Numeric
        isbns.to_s
      when Array
        isbns.join(",")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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