openkvk 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/openkvk/api.rb CHANGED
@@ -8,6 +8,10 @@ module OpenKVK
8
8
 
9
9
  class API
10
10
  class << self
11
+ def search(keywords)
12
+ JSON.parse(get(keywords, "sphinx")).first["RESULT"]
13
+ end
14
+
11
15
  def query(query)
12
16
  result = JSON.parse(get(query)).first["RESULT"]
13
17
  result["ROWS"].map { |row| Hashie::Mash.new(Hash[*result["HEADER"].zip(row).flatten]) }
@@ -15,11 +19,11 @@ module OpenKVK
15
19
 
16
20
  private
17
21
 
18
- def get(query)
22
+ def get(query, service="api")
19
23
  begin
20
- response = Net::HTTP.get_response(URI.parse("#{OpenKVK.host}json/#{URI.escape(query)}"))
24
+ response = Net::HTTP.get_response(URI.parse("http://#{service}.#{OpenKVK.host}/json/#{URI.escape(query)}"))
21
25
  if response.kind_of?(Net::HTTPRedirection)
22
- response = Net::HTTP.get_response(URI.parse("#{OpenKVK.host}#{URI.escape(response["Location"])}"))
26
+ response = Net::HTTP.get_response(URI.parse("http://#{service}.#{OpenKVK.host}/#{URI.escape(response["Location"])}"))
23
27
  end
24
28
  response.body
25
29
  rescue Exception => e
@@ -4,8 +4,8 @@ module OpenKVK
4
4
  # An array of valid keys in the options hash when configuring an {OpenKVK::API}
5
5
  VALID_OPTIONS_KEYS = [:host].freeze
6
6
 
7
- # By default, set http://api.openkvk.nl/ as the server
8
- DEFAULT_HOST = "http://api.openkvk.nl/".freeze
7
+ # By default, set openkvk.nl as the server
8
+ DEFAULT_HOST = "openkvk.nl".freeze
9
9
 
10
10
  # @private
11
11
  attr_accessor *VALID_OPTIONS_KEYS
@@ -1,3 +1,3 @@
1
1
  module OpenKVK
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/openkvk.rb CHANGED
@@ -5,6 +5,12 @@ module OpenKVK
5
5
  extend Configuration
6
6
 
7
7
  class << self
8
+
9
+ def search(keywords)
10
+ numbers = API.search(keywords)
11
+ numbers.size > 0 ? API.query("SELECT * FROM kvk WHERE kvk IN (#{numbers.join(", ")})") : []
12
+ end
13
+
8
14
  def find(options={})
9
15
  if options.is_a?(String)
10
16
  options = {:conditions => ["bedrijfsnaam LIKE '%#{options}%'", "bedrijfsnaam LIKE '%#{options.to_s.upcase}%'", "bedrijfsnaam LIKE '%#{capitalize_and_format_each_word(options)}%'"], :match_condition => :any}
data/spec/openkvk_spec.rb CHANGED
@@ -59,7 +59,24 @@ describe OpenKVK do
59
59
  company.kvk.should == "343774520000"
60
60
  end
61
61
  end
62
-
62
+
63
+ describe ".search" do
64
+ it "should search for a company with full text search" do
65
+ expect_search("ZiLvErLiNe", '[{"RESULT":[342838240000,343774520000]}]')
66
+ expect_query("SELECT * FROM kvk WHERE kvk IN (342838240000, 343774520000)", '[{"RESULT":{"TYPES":["bigint","varchar","int","int","varchar","varchar","varchar","varchar","varchar","varchar","bigint","varchar","decimal","decimal","date"],"HEADER":["kvk","bedrijfsnaam","kvks","sub","adres","postcode","plaats","type","status","website","vestiging","rechtsvorm","lat_rad","lon_rad","anbi"],"ROWS":[["343774520000","Zilverline B.V.","34377452",null,"Science Park 400","1098XH","Amsterdam","Hoofdvestiging",null,null,"19993846",null,"0.913791014","0.086494384",null],["342838240000","Zilverline Beheer B.V.","34283824",null,"Prins Hendriklaan 9","1404AR","Bussum","Hoofdvestiging",null,null,"5062055",null,"0.912472656","0.090085531",null]]}}]')
67
+
68
+ companies = OpenKVK.search("ZiLvErLiNe")
69
+ companies.size.should == 2
70
+ company = companies.first
71
+ company.bedrijfsnaam.should == "Zilverline B.V."
72
+ company.kvk.should == "343774520000"
73
+ company.adres.should == "Science Park 400"
74
+ company.postcode.should == "1098XH"
75
+ company.plaats.should == "Amsterdam"
76
+ company.type.should == "Hoofdvestiging"
77
+ company.website.should be nil
78
+ end
79
+ end
63
80
 
64
81
  describe ".find_by_bedrijfsnaam" do
65
82
  it "should find a company" do
data/spec/spec_helper.rb CHANGED
@@ -14,4 +14,8 @@ def expect_query(sql, response)
14
14
  OpenKVK::API.expects(:get).with(sql).returns(response)
15
15
  end
16
16
 
17
+ def expect_search(keywords, response)
18
+ OpenKVK::API.expects(:get).with(keywords, "sphinx").returns(response)
19
+ end
20
+
17
21
  load File.expand_path('../../lib/openkvk.rb', __FILE__)
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: openkvk
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Daniel van Hoesel
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-27 00:00:00 +02:00
13
+ date: 2012-04-05 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency