openkvk 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/lib/openkvk/api.rb CHANGED
@@ -9,26 +9,34 @@ module OpenKVK
9
9
  class API
10
10
  class << self
11
11
  def search(keywords)
12
- JSON.parse(get(keywords, "sphinx")).first["RESULT"]
12
+ begin
13
+ JSON.parse(get(keywords, "sphinx")).first["RESULT"]
14
+ rescue Exception => e
15
+ raise_exception
16
+ end
13
17
  end
14
18
 
15
19
  def query(query)
16
- result = JSON.parse(get(query)).first["RESULT"]
17
- result["ROWS"].map { |row| Hashie::Mash.new(Hash[*result["HEADER"].zip(row).flatten]) }
20
+ begin
21
+ result = JSON.parse(get(query)).first["RESULT"]
22
+ result["ROWS"].map { |row| Hashie::Mash.new(Hash[*result["HEADER"].zip(row).flatten]) }
23
+ rescue Exception => e
24
+ raise_exception
25
+ end
18
26
  end
19
27
 
20
28
  private
21
29
 
22
30
  def get(query, service="api")
23
- begin
24
- response = Net::HTTP.get_response(URI.parse("http://#{service}.#{OpenKVK.host}/json/#{URI.escape(query)}"))
25
- if response.kind_of?(Net::HTTPRedirection)
26
- response = Net::HTTP.get_response(URI.parse("http://#{service}.#{OpenKVK.host}/#{URI.escape(response["Location"])}"))
27
- end
28
- response.body
29
- rescue Exception => e
30
- raise InvalidResponseException.new("Couldn't get the response: #{$!}")
31
+ response = Net::HTTP.get_response(URI.parse("http://#{service}.#{OpenKVK.host}/json/#{URI.escape(query)}"))
32
+ if response.kind_of?(Net::HTTPRedirection)
33
+ response = Net::HTTP.get_response(URI.parse("http://#{service}.#{OpenKVK.host}/#{URI.escape(response["Location"])}"))
31
34
  end
35
+ response.body
36
+ end
37
+
38
+ def raise_exception
39
+ raise InvalidResponseException.new("Couldn't get the response: #{$!}")
32
40
  end
33
41
 
34
42
  end
@@ -1,3 +1,3 @@
1
1
  module OpenKVK
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -28,6 +28,16 @@ describe OpenKVK do
28
28
  }.should raise_error OpenKVK::InvalidResponseException
29
29
  end
30
30
 
31
+ it "should raise an exception when an blank result is returned by the host" do
32
+ response = stub(:body => "")
33
+ response.stubs(:kind_of?).returns(false)
34
+ Net::HTTP.stubs(:get_response).returns(response)
35
+
36
+ lambda {
37
+ OpenKVK::API.search("zilverline")
38
+ }.should raise_error OpenKVK::InvalidResponseException
39
+ end
40
+
31
41
  end
32
42
 
33
43
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: openkvk
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.6
5
+ version: 0.0.7
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: 2012-04-24 00:00:00 +02:00
13
+ date: 2012-05-10 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency