Sha256: fc30863e34ea7ab9b2ca2edc18938906210d3603f4bb45e9ce98a1ec08c0f772

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

require 'net/http'
require 'json'

class OpenBD
  API_BASE_URL = 'http://api.openbd.jp/v1/'.freeze

  class << self
    def get(isbns)
      request_url = prepare_url('get', isbns)
      response = Net::HTTP.get_response(URI::parse(request_url))
      create_body(response).select { |item| item != nil }
    end

    def bulk_get(isbns)
      request_url = prepare_url('get')
      response = Net::HTTP.post_form(URI::parse(request_url), isbn: normalize_isbns(isbns))
      create_body(response).select { |item| item != nil }
    end

    def coverage
      request_url = prepare_url('coverage')
      response = Net::HTTP.get_response(URI::parse(request_url))
      create_body(response)
    end

    def schema
      request_url = prepare_url('schema')
      response = Net::HTTP.get_response(URI::parse(request_url))
      create_body(response)
    end

    def create_body(response)
      JSON.parse response.body
    end

    def prepare_url(method, isbns = nil)
      case isbns
      when String
        params = normalize_isbns(isbns)
        "#{API_BASE_URL}#{method}?isbn=#{params}"
      when Numeric
        params = normalize_isbns(isbns)
        "#{API_BASE_URL}#{method}?isbn=#{params}"
      when Array
        params = normalize_isbns(isbns)
        "#{API_BASE_URL}#{method}?isbn=#{params}"
      else
        "#{API_BASE_URL}#{method}"
      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.2 lib/openbd/client.rb