openkvk-wrapper 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/openkvk.rb +71 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6d980713a9ba9556e9f19c60b1966c38602a8068
4
+ data.tar.gz: 1337f301113f259b57596f07e9913591fa93d151
5
+ SHA512:
6
+ metadata.gz: 6343dc38cd6582fb75f17b06b3034e613431dd203706edda103becc8d732147135f6151210482df84d595d9099af080d732b78f580c69265686febfd22533020
7
+ data.tar.gz: 20fa1a9550598e423b0007effbcf0f755c3d6e229b9a25389d14e7d328d3ea8ae1dcc7b538d33e9dc0833ef0e445b052edc46a43faed86c844a2e7b03eb937fc
data/lib/openkvk.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'cgi'
4
+
5
+ ##
6
+ # This module represents a wrapper for the Openkvk.nl API.
7
+ module OpenKVK
8
+
9
+ ##
10
+ # Custom HTTP Client Error.
11
+ #
12
+ # Throw this if HTTP STATUS CODE indicates an error. (>= 400)
13
+ class HTTPClientError < StandardError; end
14
+
15
+ ##
16
+ # Request a list with companies using the string +query+.
17
+ # +query+ contains the keywords to search with.
18
+ #
19
+ # This function requests all companies according to the keywords in the strings +query+,
20
+ # and returns a hash.
21
+ #
22
+ # * *Args* :
23
+ # - +query+ -> keywords to search with in the OpenKVK API
24
+ # * *Returns* :
25
+ # - search results (with type Hash)
26
+ # * *Raises* :
27
+ # - +SocketError+ -> if OpenKVK changed their hostname without telling people
28
+ # - +HTTPClientError+ -> if HTTP STATUS CODE equals or is higher than 400
29
+ def search(query)
30
+ JSON.parse(get(query))
31
+ end
32
+
33
+ private
34
+
35
+ ##
36
+ # Magic happens here. This function requests a JSON document, and returns it.
37
+ #
38
+ # * *Args* :
39
+ # - +query+ -> keywords to search with in the OpenKVK API
40
+ # - +protocol+ -> protocol to use, currently only support for HTTP
41
+ # - +host+ -> hostname of our API, might change once in a while
42
+ # * *Returns* :
43
+ # - String with searchresults in JSON
44
+ # * *Raises* :
45
+ # - +SocketError+ -> if OpenKVK changed their hostname without telling people
46
+ # - +HTTPClientError+ -> if HTTP STATUS CODE equals or is higher than 400
47
+ def get(query, protocol="http", host="officieel.openkvk.nl")
48
+ # Use 'net/http' to request. Avoid as many depencies as possible.
49
+ url = URI.parse(
50
+ protocol +
51
+ "://" +
52
+ host +
53
+ "/" +
54
+ CGI::escape(query)
55
+ )
56
+
57
+ # HTTP magic happens here.
58
+ req = Net::HTTP::Get.new(url.to_s)
59
+ res = Net::HTTP.start(url.host, url.port) do |http|
60
+ http.request(req)
61
+ end
62
+
63
+ # If HTTP STATUS not OK (STATUS >= 400), raise error.
64
+ raise(HTTPClientError, "Status code " + res.code) if res.code.to_i >= 400
65
+
66
+ res.body
67
+ end
68
+
69
+ module_function :search, :get
70
+
71
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: openkvk-wrapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - StephanMeijer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple wrapper for the OpenKVK API.
14
+ email: me@stephanmeijer.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/openkvk.rb
20
+ homepage: http://rubygems.org/gems/openkvk-wrapper
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.4.6
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Wrapper for openkvk.nl
44
+ test_files: []