module OpenKVK

This module represents a wrapper for the Openkvk.nl API.

Public Class Methods

get(query, protocol="http", host="officieel.openkvk.nl") click to toggle source

Magic happens here. This function requests a JSON document, and returns it.

  • Args :

    • query -> keywords to search with in the OpenKVK API

    • protocol -> protocol to use, currently only support for HTTP

    • host -> hostname of our API, might change once in a while

  • Returns :

    • String with searchresults in JSON

  • Raises :

    • SocketError -> if OpenKVK changed their hostname without telling people

    • HTTPClientError -> if HTTP STATUS CODE equals or is higher than 400

# File lib/openkvk/gem.rb, line 43
def get(query, protocol="http", host="officieel.openkvk.nl")
  # Use 'net/http' to request. Avoid as many depencies as possible.
  url = URI.parse(
     protocol +
     "://" +
     host +
     "/" +
     CGI::escape(query)
   )

   # HTTP magic happens here.
   req = Net::HTTP::Get.new(url.to_s)
   res = Net::HTTP.start(url.host, url.port) do |http|
     http.request(req)
   end

  # If HTTP STATUS not OK (STATUS >= 400), raise error.
  raise(HTTPClientError, "Status code " + res.code) if res.code.to_i >= 400

   res.body
end
version() click to toggle source
# File lib/openkvk/version.rb, line 2
def version
  major = 0
  minor = 0
  tiny  = 3

  [major, minor, tiny].join('.')
end

Private Instance Methods

get(query, protocol="http", host="officieel.openkvk.nl") click to toggle source

Magic happens here. This function requests a JSON document, and returns it.

  • Args :

    • query -> keywords to search with in the OpenKVK API

    • protocol -> protocol to use, currently only support for HTTP

    • host -> hostname of our API, might change once in a while

  • Returns :

    • String with searchresults in JSON

  • Raises :

    • SocketError -> if OpenKVK changed their hostname without telling people

    • HTTPClientError -> if HTTP STATUS CODE equals or is higher than 400

# File lib/openkvk/gem.rb, line 43
def get(query, protocol="http", host="officieel.openkvk.nl")
  # Use 'net/http' to request. Avoid as many depencies as possible.
  url = URI.parse(
     protocol +
     "://" +
     host +
     "/" +
     CGI::escape(query)
   )

   # HTTP magic happens here.
   req = Net::HTTP::Get.new(url.to_s)
   res = Net::HTTP.start(url.host, url.port) do |http|
     http.request(req)
   end

  # If HTTP STATUS not OK (STATUS >= 400), raise error.
  raise(HTTPClientError, "Status code " + res.code) if res.code.to_i >= 400

   res.body
end
version() click to toggle source
# File lib/openkvk/version.rb, line 2
def version
  major = 0
  minor = 0
  tiny  = 3

  [major, minor, tiny].join('.')
end