web_client 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,16 +28,14 @@ module WebClient
28
28
  WebClient.logger.debug "[WebClient] #{request.type.to_s.upcase} Url: http://#{host}#{(port != 80) ? ":#{port}" : ''}#{request.url} | Body: #{request.body} | Headers: #{request.headers}"
29
29
  response = Response.new @http.request(request.to_http)
30
30
  WebClient.logger.debug "[WebClient] RESPONSE Status: #{response.code} | Content: #{response.body}"
31
- if block_given?
32
- if response.success?
33
- yield(response)
34
- else
35
- WebClient.logger.error "[WebClient] #{response.code} - Unexpected error\n#{response.body}"
36
- nil
37
- end
38
- else
39
- response
31
+
32
+ unless response.success?
33
+ WebClient.logger.error "[WebClient] #{response.code} - Unexpected error\n#{response.body}"
34
+ raise Error, response.body ? response.body.force_encoding('utf-8') : 'Unexpected error'
40
35
  end
36
+
37
+ block_given? ? yield(response) : response
38
+
41
39
  rescue Timeout::Error,
42
40
  Errno::EHOSTUNREACH,
43
41
  Errno::EINVAL,
@@ -49,7 +47,7 @@ module WebClient
49
47
  SocketError,
50
48
  Errno::ECONNREFUSED => e
51
49
  WebClient.logger.error "[WebClient] #{e.class}: #{e.message}\nServer: #{host}:#{port}\nRequest: #{request.to_json}"
52
- raise Error, e
50
+ raise ConnectionFail, e
53
51
  end
54
52
  end
55
53
 
@@ -1,6 +1,9 @@
1
1
  module WebClient
2
2
 
3
3
  class Error < StandardError
4
+ end
5
+
6
+ class ConnectionFail < Error
4
7
  def initialize(inner_error)
5
8
  @inner_error = inner_error
6
9
  end
@@ -1,3 +1,3 @@
1
1
  module WebClient
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -172,18 +172,14 @@ describe WebClient::Connection do
172
172
  json.should eq 'id' => 1, 'name' => 'John'
173
173
  end
174
174
 
175
- it 'Invalid response' do
175
+ it 'Client error' do
176
176
  stub_request(:get, "#{HOST}/get_stub").to_return(status: 404)
177
- json = connection.get!('/get_stub') do |response|
178
- JSON.parse response.body
179
- end
180
-
181
- json.should be_nil
177
+ expect { connection.get!('/get_stub') }.to raise_error WebClient::Error
182
178
  end
183
179
 
184
- it 'Request error' do
180
+ it 'Server error' do
185
181
  stub_request(:get, "#{HOST}/get_stub").to_timeout
186
- expect { connection.get!('/get_stub') { |r| JSON.parse r.body } }.to raise_error WebClient::Error
182
+ expect { connection.get!('/get_stub') }.to raise_error WebClient::ConnectionFail
187
183
  end
188
184
 
189
185
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-04 00:00:00.000000000 Z
12
+ date: 2013-04-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec